-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrbit.pde
136 lines (111 loc) · 4.33 KB
/
Orbit.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
import java.util.*;
import java.awt.*;
Orbiter sun; // The root orbiter
int lastMillis;
double[][] originM = {{0},{0},{1}};
Matrix origin = new Matrix(originM);
// A few fun parameters
boolean clearBackground = true;
double speedModifier = 1.0; // Set in some of the scenes to change the speed.
// One default scene (selected in the setup() function
void setupScene1() {
sun = new Orbiter(null, 0, 0, 0, Orbiter.Type.CIRCLE, Color.yellow);
Orbiter earth = new Orbiter(sun, 50, 0, 1, Orbiter.Type.CIRCLE, Color.blue);
Orbiter moon = new Orbiter(earth, 30, 0, 1, Orbiter.Type.CIRCLE, Color.gray);
Orbiter moonSatellite = new Orbiter(moon, 20, 0, 1, Orbiter.Type.CIRCLE, Color.gray);
Orbiter jupiter = new Orbiter(sun, 200, 0, 0.5, Orbiter.Type.CIRCLE, Color.red);
Orbiter jupiterMoon = new Orbiter(jupiter, 75, 0, 2, Orbiter.Type.CIRCLE, Color.green);
Orbiter jupiterExplorer = new Orbiter(jupiterMoon, 40, 0, -1, Orbiter.Type.SQUARE, Color.orange);
Orbiter jupiterExplorerRobot = new Orbiter(jupiterExplorer, 20, 0, -3, Orbiter.Type.TRIANGLE, Color.magenta);
}
// A second default scene
void setupScene2() {
speedModifier = 0.25;
sun = new Orbiter(null, 0, 0, 0, Orbiter.Type.TRIANGLE, Color.yellow);
Orbiter earth = new Orbiter(sun, 50, 0, 10, Orbiter.Type.TRIANGLE, Color.blue);
Orbiter moon = new Orbiter(earth, 30, 0, 20, Orbiter.Type.TRIANGLE, Color.gray);
Orbiter moonSatellite = new Orbiter(moon, 20, 0, 30, Orbiter.Type.TRIANGLE, Color.gray);
Orbiter jupiter = new Orbiter(sun, 200, 0, 5, Orbiter.Type.TRIANGLE, Color.red);
Orbiter jupiterMoon = new Orbiter(jupiter, 75, 0, 2, Orbiter.Type.TRIANGLE, Color.green);
Orbiter jupiterExplorer = new Orbiter(jupiterMoon, 40, 0, 8, Orbiter.Type.TRIANGLE, Color.orange);
Orbiter jupiterExplorerRobot = new Orbiter(jupiterExplorer, 20, 0, -6, Orbiter.Type.TRIANGLE, Color.magenta);
}
// The setup. You don't need to edit this other than to switch scenes by commenting out
// the setupScene1() and uncommenting setupScene2().
void setup() {
size(800, 800);
background(0);
setupScene1();
//setupScene2(); // Run this one with clearBackground set to false
lastMillis = millis();
}
// The draw function
// DO NOT EDIT
void draw() {
if (clearBackground) background(0); // Make the background black.
int currentMillis = millis(); // Get the current number of milliseconds
int elapsedMillis = currentMillis - lastMillis; // Get the number of milliseconds elapsed since last call
double timeDelta = elapsedMillis / 1000.0;
updateOrbiters(timeDelta * speedModifier);
pushMatrix();
scale(1, -1);
translate(width / 2, - height / 2);
drawOrbiters();
popMatrix();
lastMillis = currentMillis;
}
void updateOrbiters(double timeDelta) {
Queue<Orbiter> queue = new LinkedList<>();
queue.add(sun);
while (!queue.isEmpty()) {
Orbiter currentOrbiter = queue.poll();
currentOrbiter.updateRotation(timeDelta);
for (Orbiter child : currentOrbiter.getChildren()) {
queue.add(child);
}
}
}
void drawOrbiters() {
Queue<Orbiter> queue = new LinkedList<>();
queue.add(sun);
while (!queue.isEmpty()) {
Orbiter currentOrbiter = queue.poll();
drawOrbiter(currentOrbiter);
for (Orbiter child : currentOrbiter.getChildren()) {
queue.add(child);
}
}
}
// The code for drawing an orbiter. This is called from your drawOrbiters() method
// but you should not have to edit it.
void drawOrbiter(Orbiter orbiter) {
try {
Matrix position = orbiter.getMatrix().dot(origin);
int px = (int) Math.round(position.entry(0,0) / position.entry(2,0));
int py = (int) Math.round(position.entry(1,0) / position.entry(2,0));
// Draw the orbiter
noStroke();
fill(orbiter.getFillColor().getRed(), orbiter.getFillColor().getGreen(), orbiter.getFillColor().getBlue());
switch (orbiter.getType()) {
case CIRCLE:
ellipse(px, py, 16, 16);
break;
case SQUARE:
rect(px-4, py-4, 8, 8);
break;
case TRIANGLE:
triangle(px, py+3, px-2, py-1, px+2, py-1);
break;
}
noFill();
// Draw the orbit path
if (clearBackground) {
stroke(60);
for (Orbiter child : orbiter.getChildren()) {
int radius = (int) (2*child.getOrbitRadius());
ellipse(px, py, radius, radius);
}
}
} catch (UndefinedMatrixOpException umoe) {
}
}