-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.js
162 lines (153 loc) · 4.11 KB
/
Ball.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// rule defines how the physics work (Sticky, Bouncy etc...)
let rule = 0;
let airRes = 0.997;
let diameter = 50;
let radius = diameter/2;
// isEdge detects whether the ball touches the edge or not
let isEdge = false;
class Ball
{
constructor(x,y, ax, ay, r)
{
this.position = new createVector(x, y);
this.velocity = new createVector(0, 0);
this.acceleration = new createVector(ax, ay);
this.mass = 1;
this.rule = r;
this.isEdge = false;
}
setRule(r)
{
this.rule = r;
}
update()
{
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
// Limits the velocity to 100
this.velocity.limit(50);
}
applyForce(force)
{
// Static Vector that creates a new one based on .div method
// Non-static methods only update the current vector, not create a new one
let f = p5.Vector.div(force, this.mass);
// If bouncy, add acceleration by force
if(this.rule === 0)
{
this.acceleration.mult(0);
this.acceleration.add(f);
}
// If sticky, set acceleration to 0
else if(this.rule === 1)
{
this.acceleration.x = 0;
this.acceleration.y = 0;
}
}
display()
{
push();
stroke(255);
fill(255);
ellipse(this.position.x, this.position.y, 50, 50);
}
bounceEdges()
{
// Rule 0: Bouncy
if(this.rule === 0)
{
if(this.position.x > width-radius)
{
this.position.x = width-radius;
this.velocity.x *= -1;
}
if(this.position.x < radius)
{
this.position.x = radius;
this.velocity.x *= -1;
}
if(this.position.y > height-radius)
{
this.position.y = height-radius;
this.velocity.y *= -1;
}
if(this.position.y < radius)
{
this.position.y = radius;
this.velocity.y *= -1;
}
}
//!! For the rubbery effect to work, we have to communicate between border and the ball meeting the edge
// Rule 1: Sticky
if(this.rule === 1)
{
// All the if and else if statements check if the ball goes out of the border, then sticks the ball entirely.
if(this.position.x > width-radius/2)
{
// Offsets position so it doesn't go overbound
this.position.x = width-radius/2;
this.position.y = this.position.y;
this.velocity.x = 0;
this.velocity.y = 0;
this.acceleration.x = 0;
this.acceleration.x = 0;
// Sets isEdge to true so sketch knows when to draw other stuff
this.isEdge = true;
}
else if(this.position.x < radius/2)
{
this.position.x = radius/2;
this.position.y = this.position.y;
this.velocity.x = 0;
this.velocity.y = 0;
this.acceleration.x = 0;
this.acceleration.y = 0;
this.isEdge = true;
}
else if(this.position.y > height-radius/2)
{
this.position.y = height-radius/2;
this.position.x = this.position.x;
this.velocity.x = 0;
this.velocity.y = 0;
this.acceleration.x = 0;
this.acceleration.y = 0;
this.isEdge = true;
}
else if(this.position.y < radius/2)
{
this.position.y = radius/2;
this.position.x = this.position.x;
this.velocity.x = 0;
this.velocity.y = 0;
this.acceleration.x = 0;
this.acceleration.y = 0;
this.isEdge = true;
}
else
{
this.isEdge = false;
}
}
}
//!! Add collision between the balls
checkCollision(other)
{
let distance = p5.Vector.sub(other.position, this.position);
// Calculate magnitude of the vector separating the balls
let distanceMag = distance.mag();
// Minimum distance between the balls before they touch, added 15 so
// it's less likely to get stuck together
let minDistance = diameter+15;
// If the distance between the two balls is shorter than the minimum distance
// The balls go to different directions
if (distanceMag < minDistance)
{
print('Time for collision');
this.velocity.x *= -1;
this.velocity.y *= -1;
this.velocity.limit(50);
}
}
}