package nl.tudelft.simulation.examples.dsol.animation; import java.rmi.RemoteException; import javax.naming.NamingException; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface; import nl.tudelft.simulation.jstats.distributions.DistNormal; import nl.tudelft.simulation.jstats.streams.MersenneTwister; import nl.tudelft.simulation.jstats.streams.StreamInterface; import nl.tudelft.simulation.language.d3.CartesianPoint; import nl.tudelft.simulation.language.d3.DirectedPoint; /** *
* Copyright (c) 2002-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights * reserved. *
* See for project information www.simulation.tudelft.nl. *
* @version Aug 15, 2014
* @author Alexander Verbraeck
*/
public class DiscreteBall extends Ball
{
/** the origin. */
private CartesianPoint origin = new CartesianPoint(0, 0, 0);
/** the destination. */
private CartesianPoint destination = new CartesianPoint(0, 0, 0);
/** the simulator. */
private DEVSSimulatorInterface.TimeDouble simulator = null;
/** the start time. */
private double startTime = Double.NaN;
/** the stop time. */
private double stopTime = Double.NaN;
/** the stream -- ugly but works. */
private static StreamInterface stream = new MersenneTwister();
/**
* constructs a new Ball.
* @param simulator the simulator
* @throws RemoteException on remote failure
* @throws SimRuntimeException on schedule failure
*/
public DiscreteBall(final DEVSSimulatorInterface.TimeDouble simulator) throws RemoteException, SimRuntimeException
{
super();
this.simulator = simulator;
// URL image = URLResource.getResource("/nl/tudelft/simulation/examples/dsol/animation/images/customer.jpg");
// new SingleImageRenderable(this, simulator, image);
try
{
new BallAnimation(this, simulator);
}
catch (NamingException exception)
{
exception.printStackTrace();
}
this.next();
}
/**
* next movement.
* @throws RemoteException on network failure
* @throws SimRuntimeException on simulation failure
*/
private void next() throws RemoteException, SimRuntimeException
{
this.origin = this.destination;
this.destination = new CartesianPoint(-100 + stream.nextInt(0, 200), -100 + stream.nextInt(0, 200), 0);
this.startTime = this.simulator.getSimulatorTime();
this.stopTime = this.startTime + Math.abs(new DistNormal(stream, 9, 1.8).draw());
this.simulator.scheduleEventAbs(this.stopTime, this, this, "next", null);
}
/** {@inheritDoc} */
@Override
public DirectedPoint getLocation() throws RemoteException
{
double fraction = (this.simulator.getSimulatorTime() - this.startTime) / (this.stopTime - this.startTime);
double x = this.origin.x + (this.destination.x - this.origin.x) * fraction;
double y = this.origin.y + (this.destination.y - this.origin.y) * fraction;
return new DirectedPoint(x, y, 0, 0.0, 0.0, this.theta);
}
}