-
Notifications
You must be signed in to change notification settings - Fork 1
/
Physics.pde
72 lines (59 loc) · 1.27 KB
/
Physics.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
class Physics extends Component {
PVector vel;//velocity
PVector accel;//acceleration
Physics(GameObject prnt) {
super(prnt, "Physics");
this.vel = new PVector(0, 0);
this.accel = new PVector(0, 0);
}
void updateEarly() {
this.vel.add(accel);
this.moveObject(vel);
}
//this function is wrong, applyforce should not replace accel
public void applyForce(PVector force) {
this.accel.add(force);
}
public void applyVelocity(PVector speedBy) {
this.vel.add(speedBy);
}
public void halt() {
vel = new PVector(0, 0);
accel = new PVector(0,0);
}
public void haltRight() {
if (vel.x > 0) {
vel.x = 0;
accel.x = 0;
}
}
public void haltLeft() {
if (vel.x < 0) {
vel.x = 0;
accel.x = 0;
}
}
public void haltUp() {
if (vel.y < 0) {
vel.y = 0;
accel.y = 0;
}
}
public void haltDown() {
if (vel.y > 0) {
vel.y = 0;
accel.y = 0;
}
}
public void moveObject(PVector distance) {
this.parent.worldPos.add(distance);
}
public void moveObject(String direction, float distance) {
if (direction == "y") {
this.parent.worldPos.y += distance;
}
if (direction == "x") {
this.parent.worldPos.x += distance;
}
}
}