MCPcopy Create free account
hub / github.com/OpenSourcePhysics/osp / Projectile

Class Projectile

src/csm/ch03/Projectile.java:19–95  ·  view source on GitHub ↗

Projectile models the dynamics of a projectile and forms a template for other simulations. @author Wolfgang Christian, Jan Tobochnik, Harvey Gould @version 1.0 05/16/05

Source from the content-addressed store, hash-verified

17 * @version 1.0 05/16/05
18 */
19public class Projectile implements Drawable, ODE {
20 static final double g = 9.8;
21 double[] state = new double[5]; // {x,vx,y,vy,t}
22 int pixRadius = 6; // pixel radius for drawing of projectile
23 EulerRichardson odeSolver = new EulerRichardson(this);
24
25 public void setStepSize(double dt) {
26 odeSolver.setStepSize(dt);
27 }
28
29 /**
30 * Steps (advances) the time.
31 *
32 * @param dt the time step.
33 */
34 public void step() {
35 odeSolver.step(); // do one time step using selected algorithm
36 }
37
38 /**
39 * Sets the state.
40 *
41 * @param x
42 * @param vx
43 * @param y
44 * @param vy
45 */
46 public void setState(double x, double vx, double y, double vy) {
47 state[0] = x;
48 state[1] = vx;
49 state[2] = y;
50 state[3] = vy;
51 state[4] = 0;
52 }
53
54 /**
55 * Gets the state. Required for ODE interface.
56 * @return double[] the state
57 */
58 @Override
59public double[] getState() {
60 return state;
61 }
62
63 /**
64 * Gets the rate. Required for ODE inteface
65 * @param state double[] the state
66 * @param rate double[] the computed rate
67 */
68 @Override
69public void getRate(double[] state, double[] rate) {
70 rate[0] = state[1]; // rate of change of x
71 rate[1] = 0; // rate of change of vx
72 rate[2] = state[3]; // rate of change of y
73 rate[3] = -g; // rate of change of vy
74 rate[4] = 1; // dt/dt = 1
75 }
76

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected