View Javadoc

1   /*
2    * @(#) ContinuousBall.java May 10, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.tutorial.section44;
11  
12  import java.awt.geom.Point2D;
13  import java.rmi.RemoteException;
14  
15  import nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface;
16  import nl.tudelft.simulation.jstats.streams.StreamInterface;
17  import nl.tudelft.simulation.language.d3.DirectedPoint;
18  import nl.tudelft.simulation.logger.Logger;
19  
20  /***
21   * An extension of Ball
22   * 
23   * @author peter
24   */
25  public class ContinuousBall extends Ball
26  {
27  	/*** the positioner */
28  	private Positioner positioner = null;
29  
30  	/*** the simulator to use */
31  	private DESSSimulatorInterface simulator = null;
32  
33  	/***
34  	 * constructs a new Ball
35  	 * 
36  	 * @param simulator the simulator
37  	 * @throws RemoteException on network exception
38  	 */
39  	public ContinuousBall(final DESSSimulatorInterface simulator)
40  			throws RemoteException
41  	{
42  		super();
43  		this.simulator = simulator;
44  		this.positioner = new Positioner(simulator);
45  		new BallAnimation2D(this, simulator);
46  		new BallAnimation3D(this, simulator);
47  		try
48  		{
49  			this.next();
50  		} catch (RemoteException exception)
51  		{
52  			Logger.warning(this, "Ball", exception);
53  		}
54  	}
55  
56  	/***
57  	 * @see nl.tudelft.simulation.dsol.animation.
58  	 *      LocatableInterface#getLocation()
59  	 */
60  	public DirectedPoint getLocation() throws RemoteException
61  	{
62  		double distance = this.positioner.y(this.simulator.getSimulatorTime())[0];
63  		double x = Math.cos(this.rotZ) * distance + this.origin.x;
64  		double y = Math.sin(this.rotZ) * distance + this.origin.y;
65  		if (Math.abs(x - this.origin.x) > Math.abs(this.destination.x
66  				- this.origin.x)
67  				|| Math.abs(y - this.origin.y) > Math.abs(this.destination.y
68  						- this.origin.y))
69  		{
70  			this.next();
71  		}
72  		return new DirectedPoint(new Point2D.Double(x, y), this.rotZ);
73  	}
74  
75  	/***
76  	 * next move
77  	 * 
78  	 * @throws RemoteException on network failure
79  	 */
80  	public void next() throws RemoteException
81  	{
82  		StreamInterface stream = this.simulator.getReplication().getStream(
83  				"default");
84  		this.origin = this.destination;
85  		this.positioner.setValue(0);
86  		this.destination = new DirectedPoint(-100 + stream.nextInt(0, 200),
87  				-100 + stream.nextInt(0, 200), 0);
88  		this.rotZ = (this.destination.y - this.origin.y)
89  				/ (this.destination.x - this.origin.x);
90  	}
91  }