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
16
17
18
19
20
21
22 public class Ball extends nl.tudelft.simulation.examples.dsol.animation.Ball
23 {
24
25 private Positioner positioner = null;
26
27
28 private CartesianPoint origin = new CartesianPoint(0, 0, 0);
29
30
31 private CartesianPoint destination = new CartesianPoint(0, 0, 0);
32
33
34 private double angle = 0.0;
35
36
37 private DessSimulatorInterface<Double> simulator = null;
38
39
40
41
42
43
44
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
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
78
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 }