-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rocket.pde
116 lines (101 loc) · 3.2 KB
/
Rocket.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Rocket {
Earth earth; // reference to Earth
boolean active; // rocket active
boolean engine_on; // engine firing
boolean orbit_good; // orbit plot is correct
Vector2 location; // location in Polar coords
double orientation; // Zenith Angle
Vector2 velocity; // velocity in Polar coords
Vector2 Pe; // Periapsis in Polar coords
Vector2 Ap; // Apoapsis in Polar coords
double e; // Eccentricity
double v; // True anomaly
// Construct Rocket
Rocket( Earth e ) {
// TODO: Variable mass thrust?
earth = e;
active = true;
engine_on = false;
orbit_good = false;
Pe = new Vector2();
Ap = new Vector2();
orientation = Math.PI/2.0; // zenith angle
location = new Vector2( earth.radius + 400000, 0 );
double velmag = 7668.55704;
double vr = velmag * Math.cos( orientation );
double vo = ( velmag * Math.sin( orientation ) ) / location.r;
velocity = new Vector2( vr, vo );
}
// Draw Rocket
void draw() {
if ( location.r > earth.radius) {
fill( 255, 255, 255 ); noStroke();
rectMode( CENTER );
double x = location.r * cos( -(float)location.o );
double y = location.r * sin( -(float)location.o );
rect( (float)(x/scale), (float)(y/scale), 4, 4);
orbit( this );
compass( orientation );
} else {
// TODO: Account for Escape Velocity
println( "Boom" );
active = false;
}
}
// Calculate next values
void tick() {
if ( !active ) { return; }
// Create Vectors
Vector2 vel = new Vector2();
Vector2 pos = new Vector2();
Vector2 acc = new Vector2();
Vector2 a_r = thrust();
// Acceleration
acc.r = r_acc() + a_r.r;
acc.o = o_acc() + a_r.o;
// Velocity
vel.r = velocity.r + deltaT * acc.r;
vel.o = velocity.o + deltaT * acc.o;
// Position
pos.r = location.r + deltaT * vel.r;
pos.o = location.o + deltaT * vel.o;
// Modulo Angles
if ( pos.o > 2*Math.PI ) { pos.o -= 2*Math.PI; }
if ( pos.o < -2*Math.PI ) { pos.o += 2*Math.PI; }
if ( orientation > 2*Math.PI ) { orientation -= 2*Math.PI; }
if ( orientation < -2*Math.PI ) { orientation += 2*Math.PI; }
// Update
location = pos;
velocity = vel;
}
// Calculate thrust vector
Vector2 thrust() {
Vector2 t = new Vector2( 0, 0 );
if ( engine_on ) {
double amag = 10; // m/s^s
t.r = amag * Math.cos( orientation );
t.o = ( amag * Math.sin( orientation ) ) / location.r;
orbit_good = false;
}
return t;
}
// Rotate rocket
void rotate( int dir ) {
// Processing angles go in opposite direction
double amount = Math.PI/24;
if ( dir < 0 ) {
orientation += amount;
} else {
orientation -= amount;
}
}
// Calculate radial acceleration
double r_acc() {
double g = -earth.GM() / (location.r * location.r);
return g + (location.r * velocity.o * velocity.o);
}
// Calculate angular acceleration
double o_acc() {
return (-2.0D * velocity.r * velocity.o) / location.r;
}
}