-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic_collisions_in_3d.html
258 lines (199 loc) · 7.93 KB
/
elastic_collisions_in_3d.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html>
<head>
<title>Elastic Collisions in 3D</title>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name=author content="Paul Masson">
<style>
body { margin: 0px; overflow: hidden; }
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r100/build/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r100/examples/js/controls/OrbitControls.js"></script>
<script>
function randrange(a, b) {
return a + Math.random() * (b - a);
}
var stats = new Stats();
stats.setMode(0);
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0';
stats.domElement.style.top = '0';
// declare once temp variables and modify
var next_p = new THREE.Vector3();
var plus = new THREE.Vector3();
var minus = new THREE.Vector3();
var separation = new THREE.Vector3();
var normal = new THREE.Vector3();
var normal1 = new THREE.Vector3();
var normal2 = new THREE.Vector3();
var relativeVelocity = new THREE.Vector3();
// setup for good-looking animation
var scale = 3; // from 1 to 6
var count = 1 + Math.round(scale * scale * scale); // integer : number of balls if they are all large
var range = 5.0; // size of enclosing box in rendering units
var maxradius = range / (2.0 * scale); // size of large balls
var minradius = maxradius / 3; // size of small balls
var speed = .05 ; // speed of balls
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 1);
document.body.appendChild(renderer.domElement);
document.body.appendChild(stats.domElement);
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
camera.up.set(0, 0, 1);
camera.position.set(3 * range, 3.5 * range, 2.0 * range);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
window.addEventListener('resize', function() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
var box = new THREE.Geometry();
box.vertices.push(new THREE.Vector3(-range, -range, -range));
box.vertices.push(new THREE.Vector3(range, range, range));
var boxMesh = new THREE.Line(box);
scene.add(new THREE.BoxHelper(boxMesh, 'white'));
var light = new THREE.DirectionalLight(0xffffff, .8);
// light.position.set(-range, range, 0);
light.position.set(-2 * range, range, 0);
camera.add(light);
scene.add(camera);
var ambient = new THREE.AmbientLight(0x555555);
scene.add(ambient);
var balls = [];
for (var i = 0; i < count; i++) {
var radius = randrange(minradius, maxradius);
var geometry = new THREE.SphereGeometry(radius, 35, 35);
var material = new THREE.MeshPhongMaterial();
material.color = new THREE.Color().setHSL(Math.random(), 1, .5);
var b1 = new THREE.Mesh(geometry, material);
// random radius
b1.r = radius;
// mass proportional to the cube of the radius
b1.m = radius * radius * radius;
// random position
b1.position.set(randrange(-range + radius, range - radius),
randrange(-range + radius, range - radius),
randrange(-range + radius, range - radius));
// random velocity
b1.next_v = new THREE.Vector3(randrange(-speed, speed),
randrange(-speed, speed),
randrange(-speed, speed));
// check overlapping balls, move their centers away
for (var j = 0; j < i; j++) {
var b2 = balls[j];
var mindistance = b1.r + b2.r;
// check overlap possibility
separation.copy(b1.position).sub(b2.position);
// simple check first
if (Math.abs(separation.x) <= mindistance &&
Math.abs(separation.y) <= mindistance &&
Math.abs(separation.z) <= mindistance) {
// check overlapping balls, move their centers away (best effort)
// (this could create overlapping with other balls, ignore it)
var len = separation.length();
if (len < mindistance) {
normal.copy(separation).normalize();
var delta = mindistance - len;
normal = normal.multiplyScalar(delta / 2.0);
b1.position.add(normal);
b2.position.sub(normal);
}
}
}
b1.v = new THREE.Vector3();
b1.v.copy(b1.next_v);
balls.push(b1);
scene.add(b1);
}
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
stats.update();
for (var i = 0; i < count; i++) {
var b1 = balls[i];
for (var j = i + 1; j < count; j++) {
var b2 = balls[j];
var mindistance = b1.r + b2.r;
// check overlap possibility
separation.copy(b1.position).sub(b2.position);
// simple check first
if (Math.abs(separation.x) <= mindistance &&
Math.abs(separation.y) <= mindistance &&
Math.abs(separation.z) <= mindistance) {
// exact check of overlapping balls
// and move their positions away
// (this could create overlapping with other balls, ignore it)
var len = separation.length();
if (len < mindistance) {
normal.copy(separation).normalize();
var delta = mindistance - len;
normal = normal.multiplyScalar(delta / 2.0);
b1.position.add(normal);
b2.position.sub(normal);
}
}
// check collision
separation.copy(b1.position).add(b1.v).sub(b2.position).sub(b2.v);
// simple check first
if (Math.abs(separation.x) <= mindistance &&
Math.abs(separation.y) <= mindistance &&
Math.abs(separation.z) <= mindistance) {
// exact check of collision conditions
// exchange normal weighted velocities
if (separation.length() < mindistance) {
// normal direction of impact
normal.copy(b2.position).sub(b1.position).normalize();
// impact speed
relativeVelocity.copy(b1.v).sub(b2.v);
var mdot = 2.0 * relativeVelocity.dot(normal) / (b1.m + b2.m);
normal1.copy(normal).multiplyScalar(mdot * b2.m);
normal2.copy(normal).multiplyScalar(mdot * b1.m);
b1.next_v.sub(normal1);
b2.next_v.add(normal2);
}
}
}
}
for (var i = 0; i < count; i++) {
var b1 = balls[i];
next_p.copy(b1.position).add(b1.next_v);
plus.copy(next_p).addScalar(b1.r);
minus.copy(next_p).subScalar(b1.r);
// reverse velocity components at walls
if (plus.x > range) {
b1.next_v.x = -b1.next_v.x;
next_p.x = range - b1.r;
} else if (minus.x < -range) {
b1.next_v.x = -b1.next_v.x;
next_p.x = -range + b1.r;
}
if (plus.y > range) {
b1.next_v.y = -b1.next_v.y;
next_p.y = range - b1.r;
} else if (minus.y < -range) {
b1.next_v.y = -b1.next_v.y;
next_p.y = -range + b1.r;
}
if (plus.z > range) {
b1.next_v.z = -b1.next_v.z;
next_p.z = range - b1.r;
} else if (minus.z < -range) {
b1.next_v.z = -b1.next_v.z;
next_p.z = -range + b1.r;
}
b1.position.copy(next_p);
b1.v.copy(b1.next_v);
}
}
render();
</script>
</body>
</html>