-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathballs.html
161 lines (138 loc) · 4.99 KB
/
balls.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- <meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> -->
<title>Elastic Balls</title>
<style type="text/css">
body { background-color: #444; color: #CCC; margin: 0px; overflow: hidden; font-family: monospace;}
#info { top: 0px; width: 100%; color: #777; padding: 5px; text-align:center; }
#container { text-align:center; }
#container canvas { border:0px solid #777; }
div { text-align:center; }
</style>
</head>
<body>
<br/>
<div id="container"></div>
<div>
Initial Momentum: <span id="ilabel"></span><br/>
Current Momentum: <span id="clabel"></span><br/>
<script type="text/javascript">
function Ball() {
this.x = 0;
this.y = 0;
this.sx = 0;
this.sy = 0;
this.size = 10;
this.mass = 10;
this.color = "rgba(255,255,255,0.5)";
}
var canvas, canvas2, context, image, data, context2;
var balls = [];
var WIDTH = 400;
var HEIGHT = 400;
var BALLCOUNT = 8;
init();
setInterval(loop, 1000 / 60);
function init() {
var container;
container = document.getElementById('container');
canvas = document.createElement("canvas");
canvas.width = WIDTH;
canvas.height = HEIGHT;
canvas.style.width = WIDTH + "px";
canvas.style.height = HEIGHT + "px";
container.appendChild(canvas);
context = canvas.getContext("2d");
context.fillStyle = "rgb(0, 0, 0)";
context.fillRect (0, 0, WIDTH, HEIGHT);
image = context.getImageData(0, 0, WIDTH, HEIGHT);
data = image.data;
canvas2 = document.createElement("canvas");
canvas2.width = WIDTH;
canvas2.height = HEIGHT;
context2 = canvas2.getContext("2d");
for (var i = 0; i < BALLCOUNT; i++) {
balls[i] = new Ball();
balls[i].x = Math.random() * WIDTH;
balls[i].y = Math.random() * HEIGHT;
balls[i].sx = Math.random() * 10 - 5;
balls[i].sy = Math.random() * 10 - 5;
balls[i].size = Math.random() * 30 + 10;
balls[i].color = "rgba("+Math.floor(Math.random()*127+128)+","+Math.floor(Math.random()*127+128)+","+Math.floor(Math.random()*127+128)+",0.75)";
}
document.getElementById('ilabel').innerHTML = energy();
}
function loop() {
// Clear the buffer;
context2.fillStyle="rgba(0,0,0,1)";
context2.fillRect(0,0,WIDTH,HEIGHT);
// Boundary collision
for (var i = 0; i < balls.length; i++) {
if (((balls[i].x - balls[i].size < 0) && (balls[i].sx < 0)) ||
((balls[i].x + balls[i].size > WIDTH) && balls[i].sx > 0)) {
balls[i].sx *= -1;
}
if (((balls[i].y - balls[i].size < 0) && (balls[i].sy < 0)) ||
((balls[i].y + balls[i].size > HEIGHT) && balls[i].sy > 0)) {
balls[i].sy *= -1;
}
}
// Ball collision --- Slowly steps through each pair of balls.
for (var i = 0; i < balls.length - 1; i++) {
for (var j = i + 1; j < balls.length; j++) {
if ((Math.pow(balls[i].x - balls[j].x,2) + Math.pow(balls[i].y - balls[j].y,2)) <= Math.pow(balls[i].size + balls[j].size,2)) {
// Collision Detected
// Determine Unit Normal Velocity.
var unx = balls[j].x - balls[i].x;
var uny = balls[j].y - balls[i].y;
var nc = Math.sqrt(unx*unx + uny*uny);
unx /= nc;
uny /= nc;
// Skip if they're already moving away from each other.
if ((balls[j].sx - balls[i].sx) * unx + (balls[j].sy - balls[i].sy) * uny > 0) { continue; }
// Unit Tangenital Velocity.
var utx = uny * -1;
var uty = unx;
var v1n = unx * balls[i].sx + uny * balls[i].sy;
var v1t = utx * balls[i].sx + uty * balls[i].sy;
var v2n = unx * balls[j].sx + uny * balls[j].sy;
var v2t = utx * balls[j].sx + uty * balls[j].sy;
// We're using the circle's area as the ball's mass.
var m1 = balls[i].size * balls[i].size * Math.PI;
var m2 = balls[j].size * balls[j].size * Math.PI;
var vv1n = (v1n * (m1 - m2) + 2 * m2 * v2n) / (m1 + m2);
var vv2n = (v2n * (m2 - m1) + 2 * m1 * v1n) / (m1 + m2);
balls[i].sx = unx * vv1n + utx * v1t;
balls[i].sy = uny * vv1n + uty * v1t;
balls[j].sx = unx * vv2n + utx * v2t;
balls[j].sy = uny * vv2n + uty * v2t;
}
}
}
// Draw the balls.
for (var i = 0; i < balls.length; i++) {
balls[i].x += balls[i].sx;
balls[i].y += balls[i].sy;
context2.fillStyle=balls[i].color;
context2.beginPath();
context2.arc(Math.floor(balls[i].x),Math.floor(balls[i].y),balls[i].size,0,Math.PI*2,true);
context2.fill();
}
// Page flip to the on-screen canvas. Is page-flipping even necessary?
// Old frame buffer habits die hard.
context.drawImage(canvas2,0,0,WIDTH,HEIGHT);
document.getElementById('clabel').innerHTML = energy();
}
function energy() {
var momentum = 0;
for (var i = 0; i < balls.length; i++) {
var m = balls[i].size * balls[i].size * Math.PI;
momentum += m * Math.sqrt(balls[i].sx*balls[i].sx+balls[i].sy*balls[i].sy);
}
return momentum.toFixed(5);
}
</script>
</body>
</html>