View Javadoc
1   package nl.tudelft.simulation.examples.dsol.animation.continuous;
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.simulators.DessSimulatorInterface;
10  import nl.tudelft.simulation.examples.dsol.animation.BallAnimation;
11  import nl.tudelft.simulation.jstats.streams.StreamInterface;
12  import nl.tudelft.simulation.language.d3.CartesianPoint;
13  
14  /**
15   * <p>
16   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
17   * <p>
18   * See for project information <a href="https://simulation.tudelft.nl/" target="_blank"> www.simulation.tudelft.nl</a>.
19   * <p>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   */
22  public class Ball extends nl.tudelft.simulation.examples.dsol.animation.Ball
23  {
24      /** the positioner. */
25      private Positioner positioner = null;
26  
27      /** the origin. */
28      private CartesianPoint origin = new CartesianPoint(0, 0, 0);
29  
30      /** the destination. */
31      private CartesianPoint destination = new CartesianPoint(0, 0, 0);
32  
33      /** the rotation. */
34      private double angle = 0.0;
35  
36      /** the simulator to use. */
37      private DessSimulatorInterface<Double> simulator = null;
38  
39      /**
40       * constructs a new Ball.
41       * @param nr int; the ball number
42       * @param simulator DessSimulatorInterface&lt;Double&gt;; the simulator
43       * @throws RemoteException on network exception
44       * @throws NamingException on animation error
45       */
46      public Ball(final int nr, final DessSimulatorInterface<Double> simulator) throws RemoteException, NamingException
47      {
48          super(nr);
49          this.simulator = simulator;
50          this.positioner = new Positioner(simulator);
51          new BallAnimation(this, simulator);
52          try
53          {
54              this.next();
55          }
56          catch (RemoteException exception)
57          {
58              simulator.getLogger().always().error(exception);
59          }
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public OrientedPoint3d getLocation() throws RemoteException
65      {
66          double x = Math.cos(this.angle) * this.positioner.y(this.simulator.getSimulatorTime())[1] + this.origin.getX();
67          double y = Math.sin(this.angle) * this.positioner.y(this.simulator.getSimulatorTime())[1] + this.origin.getY();
68          if (Math.abs(x - this.origin.getX()) > Math.abs(this.destination.getX() - this.origin.getX())
69                  || Math.abs(y - this.origin.getY()) > Math.abs(this.destination.getY() - this.origin.getY()))
70          {
71              this.next();
72          }
73          return new OrientedPoint3d(x, y, 0, 0.0, 0.0, this.theta);
74      }
75  
76      /**
77       * next move.
78       * @throws RemoteException on network failure
79       */
80      public void next() throws RemoteException
81      {
82          StreamInterface stream = this.simulator.getModel().getStream("default");
83          this.origin = this.destination;
84          this.positioner.setValue(0);
85          this.destination = new CartesianPoint(-100 + stream.nextInt(0, 200), -100 + stream.nextInt(0, 200), 0);
86          this.angle = (this.destination.getY() - this.origin.getY()) / (this.destination.getX() - this.origin.getX());
87      }
88  }