-
Notifications
You must be signed in to change notification settings - Fork 2
/
OrbitSimulator.pde
163 lines (131 loc) · 3.15 KB
/
OrbitSimulator.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import processing.core.*;
import controlP5.*;
// global variables
// ------------------------------------------------------------------
/** The gravitational constant. */
float gravitationalConstant = 1.0f;
/** The simulation time step. */
float timeStep = .1f;
/** The mass of the fixed body. */
float b1Mass = 50000f;
/** The radius of the fixed body. */
int b1Radius;
/** The radius of the satellite. */
int b2Radius;
/** The recalculate. */
boolean startStop = false, recalculate = false;
/** The font. */
PFont font;
/** temp vector object for misc calculations. */
PVector temp = new PVector();
/** The position of the satellite. */
Slider2D b2Position;
/** The velocity of the satellite. */
Slider2D b2Velocity;
/** The cp5 object for the ui. */
ControlP5 cp5;
/** The fixed body. */
Body b1;
/** The satellite body. */
Body b2;
/** The orbit. */
Orbit orbit;
/** The ui. */
UI ui;
// App Core
// --------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see processing.core.PApplet#settings()
*/
public void settings()
{
fullScreen(P2D, 2);
smooth(0);
}
/*
* (non-Javadoc)
* @see processing.core.PApplet#setup()
*/
public void setup()
{
// setup screen and graphics
orientation(LANDSCAPE);
background(255);
// create orbital bodies
b1 = new Body(new PVector(.5f * displayWidth, .5f * displayHeight), new PVector(0, 0, 0), new PVector(0, 0, 0),
50000.0f, displayWidth / 150, new int[] { 0, 255, 0 }, new int[] { 0, 0, 0 });
b2 = new Body(new PVector(.6f * displayWidth, 0.5f * displayHeight - 50),
new PVector(displayWidth / 100, displayWidth / 100, 0), new PVector(0, 0, 0), 1.0f, displayWidth / 150,
new int[] { 255, 0, 0 }, new int[] { 0, 0, 0 });
// create orbit
orbit = new Orbit(b1, b2);
// initialize ui
cp5 = new ControlP5(this);
Label.setUpperCaseDefault(false);
font = createFont("Arial", 18, true);
ui = new UI();
}
/*
* (non-Javadoc)
* @see processing.core.PApplet#draw()
*/
// run app
public void draw()
{
// refresh screen
background(255);
// simulate or change the orbit
if (startStop)
updateOrbit();
else
updateOrbitalStateValues();
// draw the orbit
orbit.draw();
// overlay orbit info
ui.draw();
// update simulation run-time characteristics
updateRunTimeValues();
}
/**
* Control event.
* @param e the e
*/
void controlEvent(ControlEvent e)
{
if (ui != null)
ui.correctSliderDisplayValues();
}
// Helper Methods
// ---------------------------------------------------
/**
* Update run time values.
*/
void updateRunTimeValues()
{
b1.radius = b1Radius;
b2.radius = b2Radius;
}
/**
* Update orbital state values.
*/
void updateOrbitalStateValues()
{
b1.mass = b1Mass * 1000;
b2.position.set(b2Position.getArrayValue()[0], b2Position.getArrayValue()[1]);
b2.velocity.set(b2Velocity.getArrayValue()[0], b2Velocity.getArrayValue()[1]);
if (recalculate)
{
orbit.calculateInitialOrbitalElements();
ui.generateConstantOrbitData();
}
}
/**
* Update orbit.
*/
void updateOrbit()
{
orbit.update(timeStep, 5);
b2Position.setValue(b2.position.x, b2.position.y);
b2Velocity.setValue(b2.velocity.x, b2.velocity.y);
}