-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.js
216 lines (206 loc) · 5.49 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function Ball(x,y,number)
{
this.r = 25;
this.col = color(random(0,255),random(0,255),random(0,255));
this.pos = createVector(x,y);
this.vel = createVector(0,0);
this.isReleased = false;
this.gotUnstuck = false;
this.releaseCounter = 5;
this.number = number;
if (options.randomSpeed.value) // --Random Velocity--
{
this.isReleased = true;
this.vel.set(random(-2,2),random(-2,2));
}
this.display = function()
{
noStroke();
fill(this.col);
ellipse(this.pos.x,this.pos.y,this.r*2,this.r*2);
// fill(255-red(this.col),255-green(this.col),255-blue(this.col));
// text(number, this.pos.x-this.r/4, this.pos.y+this.r/4);
}
this.move = function()
{
if (this.isReleased)
{
if (options.gravity.value)
{
this.vel.y = this.vel.y + gravityValue;
}
this.pos.add(this.vel);
if (options.wall.value && this.vel.mag()>=10) //if speed exceed 10 and the ball bounce on the walls, slows down the speeding balls
{
this.vel.mult(0.9975);
}
}
else
{
var mouse = createVector(mouseX,mouseY);
var pMouse = createVector(pmouseX, pmouseY);
var vel = p5.Vector.sub(mouse, pMouse);
this.pos = mouse;
this.vel = vel.mult(1);
if (this.releaseCounter > 0)
{
this.releaseCounter--;
}
else
{
if (!this.vel.equals(0,0) && !options.oneBall.value || options.unstuck.value && !options.oneBall.value)
{
this.isReleased = true;
}
}
}
}
this.release = function()
{
this.isReleased = true;
}
this.bounce = function()
{
if (this.pos.x >= width - this.r)
{
this.pos.x = width - this.r
this.vel.x = -this.vel.x
}
if (this.pos.x <= 0 + this.r )
{
this.pos.x = 0 + this.r
this.vel.x = -this.vel.x
}
if (this.pos.y >= height - this.r)
{
this.pos.y = height - this.r
this.vel.y = -this.vel.y
}
if (this.pos.y <= 0 + this.r )
{
this.pos.y = 0 + this.r
this.vel.y = -this.vel.y
}
}
this.isOutOfBound = function()
{
if (!options.wall.value)
{
return (this.pos.x >= width + this.r || this.pos.x <= -this.r || this.pos.y >= height+this.r);
}
else
{
return (this.pos.x >= width + this.r || this.pos.x <= -this.r || this.pos.y >= height+this.r || this.pos.y <= -this.r);
}
}
this.doesIntersect = function(other)
{
var d = this.pos.dist(other.pos);
if (d < this.r + other.r)
{
console.log("ball " + this.number + " intersects ball " + other.number);
return true;
}
else
{
return false;
}
}
this.bumpsInto = function(other)
{
var direction = p5.vector;
if (options.unstuck.value)
{
if (this.pos.equals(other.pos)) // if the 2 balls are in the exact same spot, move this one in a random position next to the other
{
direction = p5.Vector.random2D();
direction.setMag(this.r+other.r);
if (!this.gotUnstuck)
{
this.pos.add(direction);
this.gotUnstuck = true;
}
else if (!other.gotUnstuck)
{
other.pos.add(direction);
other.gotUnstuck = true;
}
else
{
console.log("we’re stuck, both balls already got unstuck")
}
}
else
{
direction = p5.Vector.sub(this.pos,other.pos); // if they are just intersecting, move this one back a bit along the line joining their centers
direction.setMag(this.r+other.r-direction.mag());
if (!this.gotUnstuck)
{
this.pos.add(direction);
this.gotUnstuck = true;
}
else if (!other.gotUnstuck)
{
other.pos.add(p5.Vector.mult(direction,-1));
other.gotUnstuck = true;
}
else
{
console.log("we’re stuck, both balls already got unstuck")
}
}
}
if (!this.isReleased) // If this ball is still held by the mouse
{
console.log("this is held");
direction = p5.Vector.sub(this.pos,other.pos);
var velProjection = direction.mult((p5.Vector.dot(other.vel,direction)/direction.magSq()));
other.vel.sub(velProjection.mult(2));
if (!this.vel.equals(0,0))
{
velProjection = p5.Vector.mult(direction, p5.Vector.dot(this.vel,direction)/direction.magSq());
other.vel.add(velProjection);
}
}
else if (!other.isReleased) // if other ball is still held by the mouse
{
console.log("other is held");
direction = p5.Vector.sub(other.pos,this.pos);
var velProjection = p5.Vector.mult(direction, p5.Vector.dot(this.vel,direction)/direction.magSq());
this.vel.sub(velProjection.mult(2));
if (!other.vel.equals(0,0))
{
velProjection = p5.Vector.mult(direction, p5.Vector.dot(other.vel,direction)/direction.magSq());
this.vel.add(velProjection);
}
}
else // Otherwise, if both balls are free and bounce into each other
{
direction = p5.Vector.sub(other.pos,this.pos);
var thisProjection = p5.Vector.mult(direction, p5.Vector.dot(this.vel,direction)/direction.magSq());
var otherProjection = p5.Vector.mult(direction, p5.Vector.dot(other.vel,direction)/direction.magSq());
this.vel.sub(thisProjection);
this.vel.add(otherProjection);
other.vel.sub(otherProjection);
other.vel.add(thisProjection);
this.vel.mult(0.975);
other.vel.mult(0.975);
}
}
this.attraction = function()
{
var mouse = createVector(mouseX,mouseY);
var direction = p5.Vector.sub(this.pos,mouse);
screenVector = createVector(width,height);
direction.setMag(-0.001*(screenVector.mag()-direction.mag()));
this.vel.add(direction);
}
this.blown = function()
{
var mouse = createVector(mouseX,mouseY);
var direction = p5.Vector.sub(this.pos,mouse);
screenVector = createVector(width,height);
direction.setMag(0.0002*(screenVector.mag()-direction.mag()));
this.vel.add(direction);
}
}