1 const DAMPING = 0.03;
\r
2 const DRAG = 1 - DAMPING;
\r
4 const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);
\r
6 const MAX_STRETCH = 1.5;
\r
8 // Explosion am Anfang
\r
9 // Intersect mit Velocity?
\r
17 constructor(p1, p2, restDist) {
\r
20 this.restDist = restDist;
\r
24 const diff = this.p2.position.clone().sub(this.p1.position);
\r
25 const currentDist = diff.length();
\r
26 if (currentDist == 0) return;
\r
27 if (currentDist <= this.restDist) return;
\r
28 //const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));
\r
29 const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist);
\r
30 correction.multiplyScalar(K);
\r
31 if (currentDist >= this.restDist * MAX_STRETCH) {
\r
34 //correction.clampLength(0, 1);
\r
35 const correctionHalf = correction.multiplyScalar(0.5);
\r
37 let p1movable = this.p1.movable && this.p1.movableTmp;
\r
38 let p2movable = this.p2.movable && this.p2.movableTmp;
\r
40 if (p1movable && p2movable) {
\r
41 this.p1.position.add(correctionHalf);
\r
42 this.p2.position.sub(correctionHalf);
\r
43 } else if (! p1movable && p2movable) {
\r
44 this.p2.position.sub(correction);
\r
45 } else if (p1movable && ! p2movable) {
\r
46 this.p1.position.add(correction);
\r
55 constructor(x, y, z, mass) {
\r
56 this.position = new THREE.Vector3(x, y, z);
\r
57 this.previous = new THREE.Vector3(x, y, z);
\r
58 this.acceleration = new THREE.Vector3(0, 0, 0);
\r
62 this.acceleration.add(
\r
63 force.clone().multiplyScalar(1/this.mass)
\r
68 // next position = 2 * current Position - previous position + acceleration * (passed time)^2
\r
69 // acceleration (dv/dt) = F(net)
\r
70 const nextPosition = this.position.clone().sub(this.previous);
\r
71 nextPosition.multiplyScalar(DRAG);
\r
72 nextPosition.add(this.position);
\r
73 nextPosition.add(this.acceleration.multiplyScalar(dt*dt));
\r
75 if (this.movable && this.movableTmp) {
\r
76 this.previous = this.position;
\r
77 this.position = nextPosition;
\r
80 this.acceleration.set(0, 0, 0);
\r
85 constructor(width, height, numPointsWidth, numPointsHeight) {
\r
87 this.height = height;
\r
88 this.numPointsWidth = numPointsWidth;
\r
89 this.numPointsHeight = numPointsHeight;
\r
90 this.windFactor = new THREE.Vector3(3, 2, 2);
\r
93 * distance between two vertices horizontally/vertically
\r
94 * divide by the number of points minus one
\r
95 * because there are (n - 1) lines between n vertices
\r
97 let stepWidth = width / (numPointsWidth - 1);
\r
98 let stepHeight = height / (numPointsHeight - 1);
\r
101 * iterate over the number of vertices in x/y axis
\r
102 * and add a new Particle to "particles"
\r
104 this.particles = [];
\r
105 for (let y = 0; y < numPointsHeight; y++) {
\r
106 for (let x = 0; x < numPointsWidth; x++) {
\r
107 this.particles.push(
\r
109 (x - ((numPointsWidth-1)/2)) * stepWidth,
\r
110 height - (y + ((numPointsHeight-1)/2)) * stepHeight,
\r
117 //this.particles[this.getVertexIndex(0, 0)].movable = false;
\r
119 for (let i = 0; i < numPointsHeight; i++)
\r
120 this.particles[this.getVertexIndex(0, i)].movable = false;
\r
121 //this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;
\r
122 //this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;
\r
124 const REST_DIST_X = width / (numPointsWidth-1);
\r
125 const REST_DIST_Y = height / (numPointsHeight-1);
\r
128 * generate constraints (springs)
\r
130 this.constraints = [];
\r
131 for (let y = 0; y < numPointsHeight; y++) {
\r
132 for (let x = 0; x < numPointsWidth; x++) {
\r
133 if (x < numPointsWidth-1) {
\r
134 this.constraints.push(new Constraint(
\r
135 this.particles[this.getVertexIndex(x, y)],
\r
136 this.particles[this.getVertexIndex(x+1, y)],
\r
140 if (y < numPointsHeight-1) {
\r
141 this.constraints.push(new Constraint(
\r
142 this.particles[this.getVertexIndex(x, y)],
\r
143 this.particles[this.getVertexIndex(x, y+1)],
\r
150 generateGeometry() {
\r
151 const geometry = new THREE.BufferGeometry();
\r
153 const vertices = [];
\r
154 const indices = [];
\r
157 for (let i in this.particles) {
\r
158 let particle = this.particles[i];
\r
160 particle.position.x,
\r
161 particle.position.y,
\r
162 particle.position.z);
\r
164 this.getX(i) / (this.numPointsWidth-1),
\r
165 1 - (this.getY(i) / (this.numPointsHeight-1))
\r
170 * generate faces based on 4 vertices
\r
171 * and 6 springs each
\r
173 for (let y = 0; y < this.numPointsHeight - 1; y++) {
\r
174 for (let x = 0; x < this.numPointsWidth - 1; x++) {
\r
176 this.getVertexIndex(x, y),
\r
177 this.getVertexIndex(x+1, y),
\r
178 this.getVertexIndex(x+1, y+1)
\r
181 this.getVertexIndex(x, y),
\r
182 this.getVertexIndex(x+1, y+1),
\r
183 this.getVertexIndex(x, y+1)
\r
188 geometry.setIndex(indices);
\r
189 geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
\r
190 geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
\r
191 geometry.computeBoundingSphere();
\r
192 geometry.computeVertexNormals();
\r
196 updateGeometry(geometry) {
\r
197 const positions = geometry.attributes.position.array;
\r
198 for (let i in this.particles) {
\r
199 let p = this.particles[i];
\r
200 positions[i*3+0] = p.position.x;
\r
201 positions[i*3+1] = p.position.y;
\r
202 positions[i*3+2] = p.position.z;
\r
204 geometry.attributes.position.needsUpdate = true;
\r
205 geometry.computeBoundingSphere();
\r
206 geometry.computeVertexNormals();
\r
209 let now = performance.now();
\r
210 for (let particle of this.particles) {
\r
211 let vertex = particle.position;
\r
212 let fWind = new THREE.Vector3(
\r
213 this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1),
\r
214 this.windFactor.y * Math.cos(vertex.z * now),
\r
215 this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))
\r
217 // normalize then multiply?
\r
219 particle.addForce(fWind);
\r
220 // calculate wind with normal?
\r
222 particle.addForce(GRAVITY);
\r
224 particle.verlet(dt);
\r
228 for (let constraint of this.constraints) {
\r
229 constraint.satisfy();
\r
232 //this.intersect();
\r
236 for (let i in this.particles) {
\r
237 for (let j in this.particles) {
\r
238 let p1 = this.particles[i];
\r
239 let p2 = this.particles[j];
\r
241 p1.movableTmp = true;
\r
242 p2.movableTmp = true;
\r
244 if (i == j || (Math.abs(this.getX(i) - this.getX(j)) == 1 && Math.abs(this.getY(i) - this.getY(j)) == 1))
\r
247 let dist = p1.position.distanceTo(p2.position);
\r
248 let collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight);
\r
249 // collisionDistance /= 2;
\r
250 if (dist < collisionDistance) {
\r
251 // p1.movableTmp = false;
\r
252 // p2.movableTmp = false;
\r
253 let diffP2P1 = p1.position.clone().sub(p2.position).normalize();
\r
254 diffP2P1.multiplyScalar((collisionDistance - dist) * 1.001 / 2);
\r
255 let diffP1P2 = diffP2P1.clone().multiplyScalar(-1);
\r
257 // let v1 = p1.position.clone().sub(p1.previous).normalize();
\r
258 // let v2 = p2.position.clone().sub(p2.previous).normalize();
\r
260 // let factor1 = (Math.PI - Math.acos(v1.dot(diffP2P1))) / Math.PI * 2;
\r
261 // let factor2 = (Math.PI - Math.acos(v2.dot(diffP1P2))) / Math.PI * 2;
\r
264 p1.position.add(diffP2P1);
\r
265 //p1.position.add(diffP2P1.multiplyScalar(factor1));
\r
267 p2.position.add(diffP1P2);
\r
268 //p2.position.add(diffP1P2.multiplyScalar(factor2));
\r
273 blow(camPos, intersects) {
\r
274 let face = intersects[0].face;
\r
275 let dir = intersects[0].point.clone().sub(camPos).multiplyScalar(100);
\r
276 this.particles[face.a].addForce(dir);
\r
277 this.particles[face.b].addForce(dir);
\r
278 this.particles[face.c].addForce(dir);
\r
280 drag(mousePosWorld, index) {
\r
281 let dir = mousePosWorld.clone().sub(this.particles[index].position).multiplyScalar(200);
\r
282 this.particles[index].addForce(dir);
\r
286 * helper function to calculate index of vertex
\r
287 * in "vertices" array based on its x and y positions
\r
289 * @param {number} x - x index of vertex
\r
290 * @param {number} y - y index of vertex
\r
292 getVertexIndex(x, y) {
\r
293 return y * this.numPointsWidth + x;
\r
295 getX(i) { return i % this.numPointsWidth; }
\r
296 getY(i) { return Math.floor(i / this.numPointsWidth); }
\r