-
Notifications
You must be signed in to change notification settings - Fork 2
/
mover.pde
47 lines (40 loc) · 1.11 KB
/
mover.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
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
float G; // Universal Gravitational Constant
Mover(float mass_, PVector location_, PVector velocity_) {
mass = mass_;
location = location_;
velocity = velocity_;
acceleration = new PVector(0, 0);
G = 10;
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0); // Remember to clear acceleration so it is not accumulated across frames.
}
PVector attract(Mover m) {
PVector force = PVector.sub(location, m.location);
float distance = force.mag();
// Constrain force if distance is zero
if (distance == 0) {
force.mult(0);
} else {
force.normalize();
float strength = (G * mass * m.mass) / (distance * distance);
force.mult(strength);
}
return force;
}
void display() {
fill(175);
ellipse(location.x, location.y, mass, mass);
}
}