-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.pde
59 lines (48 loc) · 1.15 KB
/
Ball.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
class Ball {
PVector s; // Position
PVector v; // Velocity
PVector a; // Accelaration
PVector f; // Force
float m = 10.0; // Mass
float r = 30.0; // Radius
float d = 0.002; // Density
float vS; // Exit velocity
float Kd1 = 0.10; // Friction constant in air
float Kd2 = 0.30; // Friction constant in water
Ball(PVector s0) {
s = s0.get();
v = new PVector(0, 0);
a = new PVector(0, 0);
}
void run() {
update();
display();
}
// Update values using Symplectic Euler
void update() {
updateForce();
v = PVector.add(PVector.mult(a, SIM_STEP), v);
s = PVector.add(PVector.mult(v, SIM_STEP), s);
a.set(0.0, 0.0);
}
// Calculate all the forces
void updateForce() {
PVector fG = PVector.mult(G, m);
applyForce(fG);
}
// Apply forces
void applyForce(PVector force) {
PVector f = force.get();
f.div(m);
a.add(f);
}
// Method to display
void display() {
noStroke();
fill(0, 196, 255, 100);
if ((s.y > height/2) || (s.y > height/2 && s.y < height/2)){
fill(0, 196, 255, 0);
}
ellipse(s.x, s.y, r, r);
}
}