-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounce.js
78 lines (70 loc) · 2.45 KB
/
bounce.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
class BouncyBounce {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.r = r;
this.e = document.createElement('circle');
this.e.style.width = r*2 +"px";
this.e.style.height = r*2 +"px";
this.e.style.left = x - r +"px";
this.e.style.top = y - r +"px";
document.body.appendChild(this.e);
}
destroy() {
document.body.removeChild(this.e);
}
update() {
this.vy += grav;
this.x += this.vx;
this.y += this.vy;
//out of screen
if (this.y-100 > window.innerHeight
|| this.y+3000 < 0
|| this.x-100 > window.innerWidth
|| this.x+100 < 0) {
gameover();
}
for (var i=0; i<bouncies.length; i++) {
if (bouncies[i] != this) {
this.collide(bouncies[i]);
}
}
this.e.style.left = this.x - this.r +"px";
this.e.style.top = this.y - this.r +"px";
}
dist(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
collide(o) {
var d = this.dist(this.x, this.y, o.x, o.y);
if (d <= this.r + o.r) {
var overlap = (d - this.r - o.r) / 2;
var nx = (o.x - this.x) / d;
var ny = (o.y - this.y) / d;
var p = 2 * (this.vx * nx + this.vy * ny - o.vx * nx - o.vy * ny) / (this.r + o.r);
//move out of contact
this.x -= overlap * (this.x - o.x) / d;
this.y -= overlap * (this.y - o.y) / d;
o.x -= overlap * (o.x - this.x) / d;
o.y -= overlap * (o.y - this.y) / d;
//bounce
this.vx = this.vx - p * o.r * nx;
this.vy = this.vy - p * o.r * ny;
o.vx = o.vx + p * this.r * nx;
o.vy = o.vy + p * this.r * ny;
//play sound
if(Math.abs(overlap) > 0.3) {
var gain = Math.min((Math.abs(overlap)-0.3) / 10, 1);
var pan = (this.x / window.innerWidth -0.5);
switch (Math.floor(Math.random()*4)) {
case 0: playSound(sounds.tock1, gain, pan); break;
case 1: playSound(sounds.tock2, gain, pan); break;
case 2: playSound(sounds.tock3, gain, pan); break;
case 3: playSound(sounds.tock4, gain, pan); break;
}
}
}
}
}