-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlanet.pde
118 lines (105 loc) · 2.94 KB
/
Planet.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
class Planet {
PVector pos;
PVector vel;
PVector acc;
ArrayList<PVector> trail;
float radius;
float mass;
color col;
boolean onScreen = true;
boolean explodeMe = false;
Planet(PVector p, PVector v, float m, float r, color c) {
pos = p;
vel = v;
acc = new PVector(0, 0);
mass = m;
radius = r;
col = c;
trail = new ArrayList<PVector>();
for (int i = 0; i < TRAIL_LENGTH; i++) {
trail.add(pos.copy());
}
}
void applyForce(PVector f) {
f.div(mass); //Do take the mass into account in F = m * a ==> a = F / m
acc.add(f);
}
void update(float fac) {
//Newtonian Physics Calculation -->
vel.add(PVector.mult(acc, fac));
pos.add(PVector.mult(vel, fac));
acc.mult(0);
}
void render() {
onScreen = pos.x > -width/2 && pos.x < width*1.5 && pos.y > -height/2 && pos.y < height*1.5;
//Trail (Should always be rendered, even if planet is off screen) -->
if (trailTracking) {
if (simRunning && !simHalted)
trailUpdate();
system.strokeCap(SQUARE);
system.noFill();
system.beginShape();
for (int i = 0; i < trail.size(); i++) {
PVector pb = trail.get(i);
system.stroke(col, map(i, 0, trail.size(), 0, 200));
system.strokeWeight(map(i, 0, trail.size(), 0, radius /3));
system.curveVertex(pb.x, pb.y);
}
system.endShape();
}
if (onScreen) {
//Planet -->
system.noStroke();
system.fill(col);
system.circle(pos.x, pos.y, radius *2);
if (showUI) {
//Heading Vector Line -->
if (showHeadingLine) {
gizmos.stroke(255);
gizmos.strokeWeight(3);
gizmos.strokeCap(ROUND);
gizmos.line(pos.x, pos.y, pos.x + vel.x, pos.y + vel.y);
}
//Velocity Text -->
if (showProperties) {
gizmos.textAlign(LEFT, BOTTOM);
gizmos.text("v:" + nfc(vel.mag(), 1) + "\nm:" + nfc(mass, 1) + "\nr:" + nfc(radius, 1), pos.x, pos.y);
}
}
}
}
void trailUpdate() {
//Trail Variable Sampling -->
if (trailTracking) {
if (onScreen) {
if (trailHQ) {
trail.add(pos.copy());
} else {
if (vel.mag() > 20) {
trail.add(pos.copy());
} else if (vel.mag() < 20 && vel.mag() > 15) {
if (frameCount % 4 == 0) {
trail.add(pos.copy());
}
} else if (vel.mag() < 15 && vel.mag() > 10) {
if (frameCount % 6 == 0) {
trail.add(pos.copy());
}
} else if (vel.mag() < 10 && vel.mag() > 5) {
if (frameCount % 7 == 0) {
trail.add(pos.copy());
}
} else if (vel.mag() < 5) {
if (frameCount % 8 == 0) {
trail.add(pos.copy());
}
}
}
}
//Trail Cleanup -->
while (trail.size() > TRAIL_LENGTH) {
trail.remove(0);
}
}
}
}