-
Notifications
You must be signed in to change notification settings - Fork 0
/
masses_on_springs.html
315 lines (273 loc) · 9.39 KB
/
masses_on_springs.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<html>
<head>
<title>Masses on springs</title>
<script src="three.js-master/build/three.min.js"></script>
<script src="three.js-master/examples/js/controls/OrbitControls.js"></script>
<script src="three.js-master/examples/js/libs/dat.gui.min.js"></script>
<script src="prng-xorshift64/PRNG.js"></script>
<!--<script src="js/PRNG.js"></script>-->
</head>
<body>
<nav style="position: absolute; top:0; left:0, height:2vh; z-index:999">
<a href="." style="color: #66ffff">Back to index.</a>
</nav>
<script>
/*
Plans for the future:
- Maybe some general bug fixing.
- Code cleanup and documentation.
- Improve upon the total abuse of Euler's method for the motion calculation.
- Maybe fork out a separate static case, for calculating responses of ideal trusses (in a flexible way).
- Maybe calculate with stress and relative strain instead of tension, stiffness and absolute strain.
- Improve visualization, particularly of particle sizes (dependent on mass), and possibly of spring stress.
- Try to optimize some calculations/visualization using GPU. Currently several attributes remain unused in the shaders.
- Add more parameters and configurability.
- Add more ways to initialize the graph. Maybe from files too.
- Add saving to file.
- Add some optimization towards given goals with given constraints. (For fun and learning.)
- Add forced motion of single nodes, and of the entire frame of reference (all the fixed nodes), to see the response of the graph.
Limitations:
- The springs are massless.
- The springs do not bend (and they rotate freely in the massive joints).
- There is no collision handling.
I do not currently plan to make any changes that remove the abovementioned limitations. The simulator may still be a bit useful for cases where collisions aren't an issue (typically static cases), and where the forces are mostly normal (typically trusses).
Giving springs mass in the motion calculation would add a lot of complexity, but it can be useful for some optimization calculations to instead calculate an estimated spring mass from length and stiffness.
*/
"use strict";
//Globals
var clock, renderer, scene, camera, controls;
var mos;
var gc = {
g: 9.81,
seed: 0
}
function MassesOnSprings(N, damping="Linear", C=0.5) {
THREE.Group.call(this);
this.N = N;
this.damping = damping;
this.C = C;
//Initial values:
this.p0 = new Float32Array(3*N).fill(0); //positions
//Relevant for points and lines:
let geom = new THREE.BufferGeometry();
geom.addAttribute("position", new THREE.Float32BufferAttribute(new Float32Array(3*N).fill(0), 3));
//For points only:
geom.addAttribute("velocity", new THREE.Float32BufferAttribute(new Float32Array(3*N).fill(0), 3));
geom.addAttribute("force", new THREE.Float32BufferAttribute(new Float32Array(3*N).fill(0), 3));
geom.addAttribute("mass", new THREE.Float32BufferAttribute(new Float32Array(2*N).fill(0), 1));
//this.geometry = geom;
this.masses = new Masses(geom);
this.springs = new Springs(geom);
this.add(this.masses, this.springs);
this.random = new PRNG();
}
MassesOnSprings.prototype = Object.create(THREE.Group.prototype);
Object.assign(MassesOnSprings.prototype, {
constructor: MassesOnSprings,
initRandom: function(seed, pFixed=0.25, pConnect=0.1) {
this.random.seed(seed);
//masses
let p0 = this.p0;
let m = this.masses.geometry.getAttribute("mass");
let ma = m.array;
for (let i=0; i<this.N; i++) {
p0[3*i] = this.random.rand()*100-50;
p0[3*i+1] = this.random.rand()*100-50;
p0[3*i+2] = this.random.rand()*100-50;
if (this.random.rand()<pFixed) ma[i] = 0;
else ma[i] = this.random.rand()*8;
}
m.needsUpdate = true;
//springs
let springs = this.springs;
springs.indices = [] //Initial indices
springs.L = []; //Initial lengths
springs.k = []; //Stiffnesses
springs.B = []; //breaking strengths
let indices = springs.indices;
let L = springs.L;
let k = springs.k;
let B = springs.B;
let c = 0;
for (let i=0; i<this.N-1; i++) {
let pi = new THREE.Vector3().fromArray(p0, 3*i);
for (let j=i+1; j<this.N; j++) {
if (this.random.rand()<pConnect) {
indices.push(i);
indices.push(j);
L.push(new THREE.Vector3().fromArray(this.p0, 3*j).sub(pi).length());
k.push(this.random.rand()*1000*L[c]);
B.push(this.random.rand()*25000);
c++;
}
}
}
this.restart(true);
},
restart: function(newIndex=false) {
let pos = this.masses.geometry.getAttribute("position");
let pa = pos.array;
for (let i=0; i < 3*this.N; i++) {
pa[i] = this.p0[i];
}
pos.needsUpdate = true;
let vel = this.masses.geometry.getAttribute("velocity");
let va = vel.array;
va.fill(0);
vel.needsUpdate = true;
if (newIndex) {
this.springs.geometry.setIndex(new THREE.BufferAttribute(new Uint16Array(this.springs.indices), 1));
} else {
let index = this.springs.geometry.getIndex();
let ia = index.array;
for (let i=0; i<ia.length; i++) {
ia[i] = this.springs.indices[i];
}
index.needsUpdate = true;
}
},
update: function(dt) {
let F = this.masses.geometry.getAttribute("force");
let Fa = F.array;
Fa.fill(0);
this.springs.update();
this.masses.update(dt);
}
});
function Masses(geometry) {
THREE.Points.call(this, geometry,
new THREE.PointsMaterial({
color: 0xcc3333,
size: 5,
sizeAttenuation: true}));
}
Masses.prototype = Object.create(THREE.Points.prototype);
Object.assign(Masses.prototype, {
constructor: Masses,
update: function(dt) {
let pos = this.geometry.getAttribute("position");
let pa = pos.array;
let vel = this.geometry.getAttribute("velocity");
let va = vel.array;
let F = this.geometry.getAttribute("force");
let Fa = F.array;
let m = this.geometry.getAttribute("mass");
let ma = m.array;
let C = this.parent.C;
let damping = this.parent.damping;
for (let i = 0; i < ma.length; i++) {
let mi = ma[i];
let Fi = new THREE.Vector3().fromArray(Fa, 3*i);
let vi = new THREE.Vector3().fromArray(va, 3*i);
let pi = new THREE.Vector3().fromArray(pa, 3*i);
if (mi !== 0) {
//Gravity
Fi.z -= mi*gc.g;
//Drag/damping
if (damping=="Quadratic") {
Fi.addScaledVector(vi, -C*vi.length());
} else if (damping=="Linear") {
Fi.addScaledVector(vi, -C);
}
//Simple Euler's method integration. Room for improvements.
//acceleration:
vi.addScaledVector(Fi, dt/mi);
vi.toArray(va, 3*i);
//motion:
pi.addScaledVector(vi, dt);
pi.toArray(pa, 3*i);
}
}
pos.needsUpdate = true;
vel.needsUpdate = true;
F.needsUpdate = true; //not needed, really
}
});
function Springs(geometry) {
THREE.LineSegments.call(this, geometry, new THREE.LineBasicMaterial({color: 0x33cc33}));
}
Springs.prototype = Object.create(THREE.LineSegments.prototype);
Object.assign(Springs.prototype, {
constructor: Springs,
update: function() {
let indices = this.geometry.getIndex();
let ia = indices.array;
let pos = this.geometry.getAttribute("position");
let pa = pos.array;
let F = this.geometry.getAttribute("force");
let Fa = F.array;
for (let a = 0; a < this.k.length; a++) {
let [i,j] = [ia[2*a], ia[2*a+1]];
if (i===j) continue; //broken spring
let pi = new THREE.Vector3().fromArray(pa, 3*i);
let pj = new THREE.Vector3().fromArray(pa, 3*j);
let diff = new THREE.Vector3().copy(pi).sub(pj);
let delta = diff.length() - this.L[a];
diff.normalize(); //direction of force
let T = this.k[a]*delta;
if (Math.abs(T) > this.B[a]) {
ia[2*a+1] = i; //Collapse and deactivate
indices.needsUpdate = true;
} else {
let Fi = new THREE.Vector3().fromArray(Fa, 3*i);
Fi.addScaledVector(diff, -T);
Fi.toArray(Fa, 3*i);
let Fj = new THREE.Vector3().fromArray(Fa, 3*j);
Fi.addScaledVector(diff, T);
Fi.toArray(Fa, 3*j);
}
}
F.needsUpdate = true; //does not really matter yet.
}
});
(function main() {
//Renderer setup
clock = new THREE.Clock(/*false*/); //false vil skru av autostart
document.body.style = "overflow: hidden;";
var container = document.createElement("div");
container.style = "position: absolute; top: 0; left: 0;"
document.body.appendChild(container);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
//Scene setup:
scene = new THREE.Scene();
scene.add(new THREE.AxesHelper(50));
//scene.add(new THREE.GridHelper(50, 10));
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
//Mass-spring system:
mos = new MassesOnSprings(100);
mos.initRandom(gc.seed);
scene.add(mos);
//Camera setup
camera = new THREE.PerspectiveCamera(26, window.innerWidth/window.innerHeight, 1, 100000);
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
camera.up.set(0,0,1);
camera.position.set(60, 140, 90);
camera.lookAt(scene.position);
scene.add(camera);
controls = new THREE.OrbitControls(camera, renderer.domElement);
//GUI:
var gui = new dat.GUI();
gui.add(gc, "g", 0, 20);
gui.add(mos, "damping", ["None", "Linear", "Quadratic"]);
gui.add(mos, "C", 0, 3);
gui.add(gc, "seed", 1<<31, 0x7fffffff, 1).onChange(function (value) {
mos.initRandom(value);
});
gui.add(mos, "restart");
animate();
})();
function animate() {
requestAnimationFrame(animate);
var dt = 1/60; //fixed time step
mos.update(dt);
renderer.render(scene, camera);
}
</script>
</body>
</html>