X-Git-Url: https://gitweb.ps.run/cloth_sim/blobdiff_plain/999a549825d3de0b53e2922462ed1e120e176ec2..8199b09231f15b0f84480656a0e92827d795fcdc:/Scripts/cloth.js diff --git a/Scripts/cloth.js b/Scripts/cloth.js index 0c4f02c..ff55549 100644 --- a/Scripts/cloth.js +++ b/Scripts/cloth.js @@ -4,7 +4,9 @@ const MASS = 0.1; const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0); const K = 1; -let tmpCorrection; +const options = { + wind: true, +}; class Constraint { constructor(p1, p2, restDist) { @@ -18,16 +20,28 @@ class Constraint { const currentDist = diff.length(); if (currentDist == 0) return; if (currentDist <= this.restDist) return; - const correction = diff.multiplyScalar(1 - (this.restDist / currentDist)); + //const correction = diff.multiplyScalar(1 - (this.restDist / currentDist)); + const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist); correction.multiplyScalar(K); - tmpCorrection = correction; + correction.clampLength(0, 1); const correctionHalf = correction.multiplyScalar(0.5); - this.p1.position.add(correctionHalf); - this.p2.position.sub(correctionHalf); + + let p1movable = this.p1.movable && this.p1.movableTmp; + let p2movable = this.p2.movable && this.p2.movableTmp; + + if (p1movable && p2movable) { + this.p1.position.add(correctionHalf); + this.p2.position.sub(correctionHalf); + } else if (! p1movable && p2movable) { + this.p2.position.sub(correction); + } else if (p1movable && ! p2movable) { + this.p1.position.add(correction); + } } } class Particle { + movableTmp = true; movable = true; constructor(x, y, z, mass) { @@ -50,8 +64,10 @@ class Particle { nextPosition.add(this.position); nextPosition.add(this.acceleration.multiplyScalar(dt*dt)); - this.previous = this.position; - this.position = nextPosition; + if (this.movable && this.movableTmp) { + this.previous = this.position; + this.position = nextPosition; + } this.acceleration.set(0, 0, 0); } @@ -63,7 +79,7 @@ class Cloth { this.height = height; this.numPointsWidth = numPointsWidth; this.numPointsHeight = numPointsHeight; - this.windFactor = new THREE.Vector3(0.5, 0.2, 0.2); + this.windFactor = new THREE.Vector3(3, 2, 2); /** * distance between two vertices horizontally/vertically @@ -90,9 +106,12 @@ class Cloth { } } - this.particles[this.getVertexIndex(0, 0)].movable = false; - this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false; - this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false; + //this.particles[this.getVertexIndex(0, 0)].movable = false; + const n = 3; + for (let i = 0; i < numPointsHeight; i++) + this.particles[this.getVertexIndex(0, i)].movable = false; + //this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false; + //this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false; const REST_DIST_X = width / (numPointsWidth-1); const REST_DIST_Y = height / (numPointsHeight-1); @@ -168,13 +187,9 @@ class Cloth { const positions = geometry.attributes.position.array; for (let i in this.particles) { let p = this.particles[i]; - if (p.movable) { - positions[i*3+0] = p.position.x; - positions[i*3+1] = p.position.y; - positions[i*3+2] = p.position.z; - } else { - p.position = p.previous; - } + positions[i*3+0] = p.position.x; + positions[i*3+1] = p.position.y; + positions[i*3+2] = p.position.z; } geometry.attributes.position.needsUpdate = true; geometry.computeBoundingSphere(); @@ -190,7 +205,8 @@ class Cloth { this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z)) ); // normalize then multiply? - particle.addForce(fWind); + if (options.wind) + particle.addForce(fWind); // calculate wind with normal? particle.addForce(GRAVITY); @@ -202,8 +218,42 @@ class Cloth { for (let constraint of this.constraints) { constraint.satisfy(); } - //console.log(tmpCorrection); + + this.intersect(); } + + intersect() { + let npw = this.numPointsWidth; + function getX(i) { return i % npw; } + function getY(i) { return Math.floor(i / npw); } + + for (let i in this.particles) { + for (let j in this.particles) { + let p1 = this.particles[i]; + let p2 = this.particles[j]; + + p1.movableTmp = true; + p2.movableTmp = true; + + if (i == j || (Math.abs(getX(i) - getX(j)) == 1 && Math.abs(getY(i) - getY(j)) == 1)) + continue; + + let dist = p1.position.distanceTo(p2.position); + const collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight); + if (dist < collisionDistance) { + p1.movableTmp = false; + p2.movableTmp = false; + let diff = p1.position.clone().sub(p2.position).normalize(); + diff.multiplyScalar((collisionDistance - dist) * 1.001 / 2); + if (p1.movable) + p1.position.add(diff); + if (p2.movable) + p2.position.sub(diff); + } + } + } + } + /** * helper function to calculate index of vertex * in "vertices" array based on its x and y positions