View Javadoc
1   package nl.tudelft.simulation.examples.dsol.animation;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import org.djutils.draw.point.OrientedPoint3d;
8   
9   import nl.tudelft.simulation.dsol.SimRuntimeException;
10  import nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface;
11  import nl.tudelft.simulation.jstats.distributions.DistUniform;
12  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
13  import nl.tudelft.simulation.jstats.streams.StreamInterface;
14  import nl.tudelft.simulation.language.d3.CartesianPoint;
15  
16  /**
17   * <p>
18   * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
19   * <p>
20   * See for project information <a href="https://simulation.tudelft.nl/" target="_blank"> www.simulation.tudelft.nl</a>.
21   * <p>
22   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   */
24  public class DiscreteBall extends Ball
25  {
26      /** the origin. */
27      private CartesianPoint origin = new CartesianPoint(0, 0, 0);
28  
29      /** the destination. */
30      private CartesianPoint destination = new CartesianPoint(0, 0, 0);
31  
32      /** the simulator. */
33      private DevsSimulatorInterface<Double> simulator = null;
34  
35      /** the start time. */
36      private double startTime = Double.NaN;
37  
38      /** the stop time. */
39      private double stopTime = Double.NaN;
40  
41      /** the stream -- ugly but works. */
42      private static StreamInterface stream = new MersenneTwister();
43  
44      /**
45       * constructs a new Ball.
46       * @param nr int; the ball number
47       * @param simulator DevsSimulatorInterface&lt;Double&gt;; the simulator
48       * @throws RemoteException on remote failure
49       * @throws SimRuntimeException on schedule failure
50       */
51      public DiscreteBall(final int nr, final DevsSimulatorInterface<Double> simulator)
52              throws RemoteException, SimRuntimeException
53      {
54          super(nr);
55          this.simulator = simulator;
56          // URL image = URLResource.getResource("/nl/tudelft/simulation/examples/dsol/animation/images/customer.jpg");
57          // new SingleImageRenderable(this, simulator, image);
58          try
59          {
60              new BallAnimation(this, simulator);
61          }
62          catch (NamingException exception)
63          {
64              this.simulator.getLogger().always().error(exception);
65          }
66          this.next();
67      }
68  
69      /**
70       * next movement.
71       * @throws RemoteException on network failure
72       * @throws SimRuntimeException on simulation failure
73       */
74      private void next() throws RemoteException, SimRuntimeException
75      {
76          this.origin = this.destination;
77          this.destination = new CartesianPoint(-100 + stream.nextInt(0, 200), -100 + stream.nextInt(0, 200), 0);
78          this.startTime = this.simulator.getSimulatorTime();
79          this.stopTime = this.startTime + Math.abs(new DistUniform(stream, 2.0, 20.0).draw());
80          this.simulator.scheduleEventAbs(this.stopTime, this, "next", null);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public OrientedPoint3d getLocation() throws RemoteException
86      {
87          double fraction = (this.simulator.getSimulatorTime() - this.startTime) / (this.stopTime - this.startTime);
88          double x = this.origin.getX() + (this.destination.getX() - this.origin.getX()) * fraction;
89          double y = this.origin.getY() + (this.destination.getY() - this.origin.getY()) * fraction;
90          return new OrientedPoint3d(x, y, 0, 0.0, 0.0, this.theta);
91      }
92  }