-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBouncyBalls.js
More file actions
213 lines (162 loc) · 4.14 KB
/
Copy pathBouncyBalls.js
File metadata and controls
213 lines (162 loc) · 4.14 KB
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
/*
Bouncy balls! With physics! And mass!
Next/prev adds/removes balls
select nudges random ball
escape quits
by gib
*/
const display = require("display");
const keyboard = require("keyboard");
const SCREEN_WIDTH = display.width();
const SCREEN_HEIGHT = display.height();
var bDoRun = true;
var lastTime = now();
var balls = [];
function Ball(x, y, radius, color){
if(color === undefined) color = random(16, 65535);
this.x = x || 0;
this.y = y || 0;
this.radius = radius || 8;
this.color = color;
this.mass = this.radius * this.radius;
this.xVel = 0;
this.yVel = 0;
this.xAccel = 0;
this.yAccel = 500;
this.bounce = 0.8;
this.friction = 0.99;
balls.push(this);
}
Ball.prototype.update = function(deltaTime){
this.xVel += this.xAccel * deltaTime;
this.yVel += this.yAccel * deltaTime;
this.xVel *= this.friction;
this.yVel *= this.friction;
this.x += this.xVel * deltaTime;
this.y += this.yVel * deltaTime;
if(this.x < this.radius){
this.x = this.radius;
this.xVel *= -this.bounce;
}
if(this.x > SCREEN_WIDTH - this.radius){
this.x = SCREEN_WIDTH - this.radius;
this.xVel *= -this.bounce;
}
if(this.y > SCREEN_HEIGHT - this.radius){
this.y = SCREEN_HEIGHT - this.radius;
this.yVel *= -this.bounce;
}
if(this.y < this.radius){
this.y = this.radius;
this.yVel *= -this.bounce;
}
if(Math.abs(this.xVel) < 0.5){
this.xVel = 0;
}
if(Math.abs(this.yVel) < 0.5){
this.yVel = 0;
}
};
Ball.prototype.draw = function(){display.drawFillCircle(this.x, this.y, this.radius, this.color);};
function checkBallCollision(a, b){
var dx = b.x - a.x;
var dy = b.y - a.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return distance < (a.radius + b.radius);
}
function resolveBallCollision(a, b){
if(!checkBallCollision(a, b)) return;
var dx = b.x - a.x;
var dy = b.y - a.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist === 0) dist = 0.1;
var nx = dx / dist;
var ny = dy / dist;
var rvx = b.xVel - a.xVel;
var rvy = b.yVel - a.yVel;
var velAlongNormal = rvx * nx + rvy * ny;
if (velAlongNormal > 0) return;
var restitution = 0.9;
var m1 = a.mass;
var m2 = b.mass;
var j = - (1 + restitution) * velAlongNormal;
j = j / (1 / m1 + 1 / m2);
var impulseX = j * nx;
var impulseY = j * ny;
a.xVel -= impulseX / m1;
a.yVel -= impulseY / m1;
b.xVel += impulseX / m2;
b.yVel += impulseY / m2;
var overlap = (a.radius + b.radius) - dist;
var correctionX = nx * overlap * 0.5;
var correctionY = ny * overlap * 0.5;
a.x -= correctionX;
a.y -= correctionY;
b.x += correctionX;
b.y += correctionY;
}
function updateBalls(deltaTime){
for(var i = 0; i < balls.length; i++){
balls[i].update(deltaTime);
}
for(var i = 0; i < balls.length; i++){
for(var j = i + 1; j < balls.length; j++){
resolveBallCollision(balls[i], balls[j]);
}
}
}
function drawBalls(){
display.fill(BRUCE_BGCOLOR);
for(var i = 0; i < balls.length; i++){
balls[i].draw();
}
}
function handleInput(){
if(keyboard.getEscPress()){
bDoRun = false;
}
if(keyboard.getNextPress()){
addBallAt();
}
if(keyboard.getPrevPress()){
removeRandomBall();
}
if(keyboard.getSelPress()){
nudgeRandomBall(1000);
}
}
function removeRandomBall(){
balls.splice(random(0, balls.length), 1);
}
function addRandomV(thisBall, amountV){
thisBall.xVel = random(-amountV, amountV);
thisBall.yVel = random(-amountV, amountV);
}
function addBallAt(x, y, r, addRandV, amountV){
if(x === undefined) x = random(0, SCREEN_WIDTH);
if(y === undefined) y = random(0, SCREEN_HEIGHT);
if(r === undefined) r = random(2, SCREEN_HEIGHT / 8);
if(addRandV === undefined) addRandV = true;
if(amountV === undefined) amountV = 200;
var ball = new Ball(x, y, r);
if(addRandV){
addRandomV(ball, amountV);
}
}
function nudgeRandomBall(howHard){
var chosenBall = balls[random(0, balls.length)];
addRandomV(chosenBall, howHard);
}
function main(){
while(bDoRun){
var currentTime = now();
var deltaTime = (currentTime - lastTime) / 1000;
lastTime = currentTime;
handleInput();
updateBalls(deltaTime);
drawBalls();
// ms - FPS: 6 ~ 165, 7 ~ 144, 8 ~ 120, 11 ~ 90, 16 ~ 60, 20 = 50, 21 ~ 48, 33 ~ 30, 40 = 25, 42 ~ 23.976, 66 ~ 15
delay(33);
}
}
main();