]> gitweb.ps.run Git - cloth_sim/commitdiff
add Constraint class, add constraints to cloth
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 6 Feb 2021 10:38:43 +0000 (11:38 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 6 Feb 2021 10:38:43 +0000 (11:38 +0100)
Scripts/cloth.js

index 05ff6f1f30eb54da8985b99c786d3596662f214e..db07ac302cf916846f12ce487ef5e7b4636547a9 100644 (file)
@@ -1,9 +1,13 @@
-// cloth rendering\r
-// simulate\r
-// setup scene\r
-\r
 const MASS = 0.1;\r
 \r
+class Constraint {\r
+  constructor(p1, p2, distance) {\r
+    this.p1 = p1;\r
+    this.p2 = p2;\r
+    this.distance = distance;\r
+  }\r
+}\r
+\r
 class Particle {\r
   constructor(x, y, z, mass) {\r
     this.position = new THREE.Vector3(x, y, z);\r
@@ -50,6 +54,32 @@ class Cloth {
         );\r
       }\r
     }\r
+\r
+    const REST_DIST_X = width / (numPointsWidth-1);\r
+    const REST_DIST_Y = height / (numPointsHeight-1);\r
+\r
+    /**\r
+     * generate constraints (springs)\r
+     */\r
+    this.constraints = [];\r
+    for (let y = 0; y < numPointsHeight; y++) {\r
+      for (let x = 0; x < numPointsWidth; x++) {\r
+        if (x < numPointsWidth-1) {\r
+          this.constraints.push(new Constraint(\r
+            this.particles[this.getVertexIndex(x, y)],\r
+            this.particles[this.getVertexIndex(x+1, y)],\r
+            REST_DIST_X\r
+          ));\r
+        }\r
+        if (x < numPointsWidth-1) {\r
+          this.constraints.push(new Constraint(\r
+            this.particles[this.getVertexIndex(x, y)],\r
+            this.particles[this.getVertexIndex(x, y+1)],\r
+            REST_DIST_Y\r
+          ));\r
+        }\r
+      }\r
+    }\r
   }\r
   generateGeometry() {\r
     const geometry = new THREE.BufferGeometry();\r
@@ -68,17 +98,6 @@ class Cloth {
     const numPointsWidth = this.numPointsWidth;\r
     const numPointsHeight = this.numPointsHeight;\r
 \r
-    /**\r
-     * helper function to calculate index of vertex\r
-     * in "vertices" array based on its x and y positions\r
-     * in the mesh\r
-     * @param {number} x - x index of vertex\r
-     * @param {number} y - y index of vertex\r
-     */\r
-    function getVertexIndex(x, y) {\r
-      return y * numPointsWidth + x;\r
-    }\r
-\r
     /**\r
      * generate faces based on 4 vertices\r
      * and 6 springs each\r
@@ -86,14 +105,14 @@ class Cloth {
     for (let y = 0; y < numPointsHeight - 1; y++) {\r
       for (let x = 0; x < numPointsWidth - 1; x++) {\r
         indices.push(\r
-          getVertexIndex(x, y),\r
-          getVertexIndex(x+1, y),\r
-          getVertexIndex(x+1, y+1)\r
+          this.getVertexIndex(x, y),\r
+          this.getVertexIndex(x+1, y),\r
+          this.getVertexIndex(x+1, y+1)\r
         );\r
         indices.push(\r
-          getVertexIndex(x, y),\r
-          getVertexIndex(x+1, y+1),\r
-          getVertexIndex(x, y+1)\r
+          this.getVertexIndex(x, y),\r
+          this.getVertexIndex(x+1, y+1),\r
+          this.getVertexIndex(x, y+1)\r
         );\r
       }\r
     }\r
@@ -109,4 +128,20 @@ class Cloth {
   updateGeometry(geometry) {\r
 \r
   }\r
+  satisfyConstraints() {\r
+\r
+  }\r
+  simulate() {\r
+\r
+  }\r
+  /**\r
+   * helper function to calculate index of vertex\r
+   * in "vertices" array based on its x and y positions\r
+   * in the mesh\r
+   * @param {number} x - x index of vertex\r
+   * @param {number} y - y index of vertex\r
+   */\r
+  getVertexIndex(x, y) {\r
+    return y * this.numPointsWidth + x;\r
+  }\r
 }
\ No newline at end of file