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
18
19
20
21
22
23
24 public class DiscreteBall extends Ball
25 {
26
27 private CartesianPoint origin = new CartesianPoint(0, 0, 0);
28
29
30 private CartesianPoint destination = new CartesianPoint(0, 0, 0);
31
32
33 private DevsSimulatorInterface<Double> simulator = null;
34
35
36 private double startTime = Double.NaN;
37
38
39 private double stopTime = Double.NaN;
40
41
42 private static StreamInterface stream = new MersenneTwister();
43
44
45
46
47
48
49
50
51 public DiscreteBall(final int nr, final DevsSimulatorInterface<Double> simulator)
52 throws RemoteException, SimRuntimeException
53 {
54 super(nr);
55 this.simulator = simulator;
56
57
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
71
72
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
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 }