-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.pde
116 lines (102 loc) · 3.7 KB
/
Sphere.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
class Mover {
PVector location;
PVector velocity;
PVector gravityForce;
//Rotation angles of the sphere
float sphere_rotX = 0;
float sphere_rotZ = 0;
//Add textures
private PImage img;
private PShape globe;
//Related to the geometry of the object and have an impact on its moment inertia
final float K_INERTIA = 2/3;
Mover() {
location = new PVector(0, -(radius_sphere + boxThickness/2), 0);
velocity = new PVector(0, 0, 0);
gravityForce = new PVector(0, 0, 0);
//Adding texture of a pool ball
img = loadImage("PoolBall.jpeg");
globe = createShape();
globe = createShape(SPHERE, radius_sphere);
globe.setStroke(false);
globe.setTexture(img);
}
//#############################################
// Update the position of the ball so that it has a real movement
//#############################################
void update() {
gravityForce = new PVector(sin(thetaZ) * gravityConstant, 0, -sin(thetaX) * gravityConstant);
velocity.add(gravityForce);
PVector friction = velocity.copy();
friction.mult(-1);
friction.normalize();
friction.mult(frictionMagnitude);
velocity.add(friction);
location.add(velocity);
//Update rotation angles of the sphere
sphere_rotX = (-velocity.z)/radius_sphere;
sphere_rotZ = (velocity.x)/radius_sphere;
checkCylinderCollision();
}
//#############################################
//Displays the ball and makes it move with real rolling effect
//#############################################
void display() {
gameSurface.noStroke();
gameSurface.pushMatrix();
gameSurface.translate(location.x, location.y, location.z);
//Add rolling effect to the ball
if (!gamePaused) {
gameSurface.rotateX(PI/2);
globe.rotateX(sphere_rotX);
globe.rotateY(sphere_rotZ);
}
gameSurface.shape(globe);
gameSurface.popMatrix();
}
//#############################################
//---AVOID THE BALL TO GO OUT OF THE BOARD-----
//#############################################
void checkEdges() {
if (location.x + radius_sphere > boxLength/2) {
velocity.x *= -1;
location.x = boxLength/2 - radius_sphere;
} else if (location.x - radius_sphere < -boxLength/2) {
velocity.x *= -1;
location.x = -boxLength/2 + radius_sphere;
}
if (location.z + radius_sphere > boxLength/2) {
velocity.z *= -1;
location.z = boxLength/2 - radius_sphere;
} else if (location.z - radius_sphere < -boxLength/2) {
velocity.z *= -1;
location.z = -boxLength/2 + radius_sphere;
}
}
//#############################################
//-------CHECK COLLISIONS WITH CYLINDERS-------
//#############################################
void checkCylinderCollision() {
for (int i = 0; i < particle_system.particles.size(); ++i) {
PVector cylinderPosition = particle_system.particles.get(i);
PVector ballCylVect = new PVector(location.x - cylinderPosition.x, 0, location.z - cylinderPosition.z);
if (ballCylVect.mag() <= (radius_sphere + cylinderBaseSize)) {
if (cylinderPosition == particle_system.origin) {
particle_system.particles.clear();
particle_system.origin = null;
} else {
//remove the cylinder if the ball hits it
particle_system.particles.remove(i);
PVector normal = ballCylVect.normalize();
float veloNorm = PVector.dot(velocity, normal)*2;
PVector vector = PVector.mult(normal, veloNorm);
velocity = PVector.sub(velocity, vector);
particle_system.points += defaultGain * playable_sphere.velocity.mag();;
}
}
}
}
PVector location() {
return location.copy();
}
}