]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
tweaking parameters
[cloth_sim] / Scripts / cloth.js
index 0c4f02c64a4c4935c3c41ad4fa4b0a69d135337d..aaa9d249a25a9702d3abd95d46a1d317f5a95f76 100644 (file)
@@ -1,10 +1,12 @@
 const DAMPING = 0.03;\r
 const DRAG = 1 - DAMPING;\r
-const MASS = 0.1;\r
+const MASS = 0.35;\r
 const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);\r
 const K = 1;\r
 \r
-let tmpCorrection;\r
+const options = {\r
+  wind: true,\r
+};\r
 \r
 class Constraint {\r
   constructor(p1, p2, restDist) {\r
@@ -18,12 +20,19 @@ class Constraint {
     const currentDist = diff.length();\r
     if (currentDist == 0) return;\r
     if (currentDist <= this.restDist) return;\r
-    const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));\r
+    //const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));\r
+    const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist);\r
     correction.multiplyScalar(K);\r
-    tmpCorrection = correction;\r
+    correction.clampLength(0, 1);\r
     const correctionHalf = correction.multiplyScalar(0.5);\r
-    this.p1.position.add(correctionHalf);\r
-    this.p2.position.sub(correctionHalf);\r
+    if (this.p1.movable && this.p2.movable) {\r
+      this.p1.position.add(correctionHalf);\r
+      this.p2.position.sub(correctionHalf);\r
+    } else if (! this.p1.movable && this.p2.movable) {\r
+      this.p2.position.sub(correction);\r
+    } else if (this.p1.movable && ! this.p2.movable) {\r
+      this.p1.position.add(correction);\r
+    }\r
   }\r
 }\r
 \r
@@ -50,8 +59,10 @@ class Particle {
     nextPosition.add(this.position);\r
     nextPosition.add(this.acceleration.multiplyScalar(dt*dt));\r
 \r
-    this.previous = this.position;\r
-    this.position = nextPosition;\r
+    if (this.movable) {\r
+      this.previous = this.position;\r
+      this.position = nextPosition;\r
+    }\r
 \r
     this.acceleration.set(0, 0, 0);\r
   }\r
@@ -63,7 +74,7 @@ class Cloth {
     this.height = height;\r
     this.numPointsWidth = numPointsWidth;\r
     this.numPointsHeight = numPointsHeight;\r
-    this.windFactor = new THREE.Vector3(0.5, 0.2, 0.2);\r
+    this.windFactor = new THREE.Vector3(5, 2, 2);\r
 \r
     /**\r
      * distance between two vertices horizontally/vertically\r
@@ -90,9 +101,12 @@ class Cloth {
       }\r
     }\r
 \r
-    this.particles[this.getVertexIndex(0, 0)].movable = false;\r
-    this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
-    this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;\r
+    //this.particles[this.getVertexIndex(0, 0)].movable = false;\r
+    const n = 3;\r
+    for (let i = 0; i <= n; i++)\r
+      this.particles[this.getVertexIndex(0, Math.floor((numPointsHeight-1)*(i/n)))].movable = false;\r
+    //this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
+    //this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;\r
 \r
     const REST_DIST_X = width / (numPointsWidth-1);\r
     const REST_DIST_Y = height / (numPointsHeight-1);\r
@@ -168,13 +182,9 @@ class Cloth {
     const positions = geometry.attributes.position.array;\r
     for (let i in this.particles) {\r
       let p = this.particles[i];\r
-      if (p.movable) {\r
-        positions[i*3+0] = p.position.x;\r
-        positions[i*3+1] = p.position.y;\r
-        positions[i*3+2] = p.position.z;\r
-      } else {\r
-        p.position = p.previous;\r
-      }\r
+      positions[i*3+0] = p.position.x;\r
+      positions[i*3+1] = p.position.y;\r
+      positions[i*3+2] = p.position.z;\r
     }\r
     geometry.attributes.position.needsUpdate = true;\r
     geometry.computeBoundingSphere();\r
@@ -190,7 +200,8 @@ class Cloth {
         this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
       );\r
       // normalize then multiply?\r
-      particle.addForce(fWind);\r
+      if (options.wind)\r
+        particle.addForce(fWind);\r
       // calculate wind with normal?\r
 \r
       particle.addForce(GRAVITY);\r