-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mover.pde
84 lines (68 loc) · 2.1 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
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
class Mover {
PVector location;
PVector velocity;
PVector gravity;
Mover() {
location = new PVector(0, -4.65, 0);
velocity = new PVector(0, 0, 0);
gravity = new PVector(0, 0.04, 0);
}
void update() {
location.add(velocity);
}
void display() {
pushMatrix();
fill(0, 255, 0);
translate(location.x, location.y, location.z);
sphere(ballSize);
popMatrix();
}
void checkEdges() {
PVector gravityForce = new PVector(sin(rotationZ) * gravity.y, 0, -sin(rotationX) * gravity.y);
float normalForce = 1;
float mu = 0.01;
float frictionMagnitude = normalForce * mu;
PVector friction = velocity.get();
friction.mult(-1);
friction.normalize();
friction.mult(frictionMagnitude);
velocity.add(gravityForce);
velocity.add(friction);
location.add(velocity);
if (location.x > boardSize/2){
velocity.x = velocity.x * -1;
location.x = boardSize/2;
}
if (location.x < -boardSize/2){
velocity.x = velocity.x * -1;
location.x = -boardSize/2;
}
if (location.z > boardSize/2){
velocity.z = velocity.z * -1;
location.z = boardSize/2;
}
if (location.z < -boardSize/2){
velocity.z = velocity.z * -1;
location.z = -boardSize/2;
}
}
void checkCylinderCollision(){
for (int i=0; i<cylinderList.size(); i++){
PVector n = new PVector(location.x, 0, location.z);
n.sub(new PVector(cylinderList.get(i).x, 0, cylinderList.get(i).y));
if (n.mag() < cylinderBaseSize + ballSize){ // collision
n.normalize();
PVector temp = n.get();
temp.mult(2);
temp.mult(velocity.dot(n));
velocity.sub(temp);
PVector n2;
do {
location.add(velocity); // detach the ball
n2 = new PVector(location.x, 0, location.z);
n2.sub(new PVector(cylinderList.get(i).x, 0, cylinderList.get(i).y));
} while (n2.mag() < cylinderBaseSize + ballSize); // if still in cylinder, detach the ball
}
}
}
}