]> gitweb.ps.run Git - cloth_sim/commitdiff
Add Spring Property
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Mon, 4 Jan 2021 15:24:11 +0000 (16:24 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Mon, 4 Jan 2021 15:24:11 +0000 (16:24 +0100)
Scripts/main.js

index 2388293d3b167ef8d4603bf24b794601e3e62089..1f26606ebe8b0d325ca067c033bfe41558170f22 100644 (file)
@@ -26,14 +26,44 @@ function init() {
     }\r
   }\r
 \r
+  function vectorLength(a, b) {\r
+    let v1 = new THREE.Vector3();\r
+    v1.set(a.x, a.y, a.z);\r
+    let v2 = new THREE.Vector3();\r
+    v2.set(b.x, b.y, b.z);\r
+\r
+    return v1.sub(v2).length();\r
+  }\r
+\r
   class Face {\r
-    indices = [];\r
+    a;\r
+    b;\r
+    c;\r
+    d;\r
+\r
+    springs = [];\r
 \r
     constructor(a, b, c, d) {\r
-      this.indices.push(a);\r
-      this.indices.push(b);\r
-      this.indices.push(c);\r
-      this.indices.push(d);\r
+      this.a = a;\r
+      this.b = b;\r
+      this.c = c;\r
+      this.d = d;\r
+    }\r
+  }\r
+\r
+  class Spring {\r
+    restLength;\r
+    currentLength;\r
+    index1;\r
+    index2;\r
+\r
+    constructor(vertices, index1, index2) {\r
+      this.index1 = index1;\r
+      this.index2 = index2;\r
+\r
+      let length = vectorLength(vertices[index1], vertices[index2]);\r
+      this.restLength = length;\r
+      this.currentLength = length;\r
     }\r
   }\r
 \r
@@ -46,12 +76,6 @@ function init() {
 \r
     vertexWeights = [];\r
 \r
-    springs = [];\r
-\r
-    getSprings(faceIndex) {\r
-\r
-    }\r
-\r
     static CreateBasic(width, height, numPointsWidth, numPointsHeight) {\r
       let vertices = [];\r
       let faces = [];\r
@@ -73,14 +97,21 @@ function init() {
       \r
       for (let y = 0; y < numPointsHeight - 1; y++) {\r
         for (let x = 0; x < numPointsWidth - 1; x++) {\r
-          faces.push(\r
-            new Face(\r
-              getVertexIndex(x, y),\r
-              getVertexIndex(x, y + 1),\r
-              getVertexIndex(x + 1, y),\r
-              getVertexIndex(x + 1, y + 1),\r
-            )\r
+          let newFace = new Face(\r
+            getVertexIndex(x, y),\r
+            getVertexIndex(x, y + 1),\r
+            getVertexIndex(x + 1, y),\r
+            getVertexIndex(x + 1, y + 1),\r
           );\r
+\r
+          newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y)));\r
+          newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x, y + 1)));\r
+          newFace.springs.push(new Spring(vertices, getVertexIndex(x, y), getVertexIndex(x + 1, y + 1)));\r
+          newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x, y + 1)));\r
+          newFace.springs.push(new Spring(vertices, getVertexIndex(x + 1, y), getVertexIndex(x + 1, y + 1)));\r
+          newFace.springs.push(new Spring(vertices, getVertexIndex(x, y + 1), getVertexIndex(x + 1, y + 1)));\r
+          \r
+          faces.push(newFace);\r
         }\r
       }\r
 \r
@@ -94,6 +125,7 @@ function init() {
         result.geometry.vertices.push(vertices[i]);\r
         result.vertexWeights.push(0);\r
       }\r
+      console.log(vertices);\r
       for (let i in faces) {\r
         let face = faces[i];\r
 \r
@@ -103,26 +135,15 @@ function init() {
         result.geometry.faces.push(new THREE.Face3(\r
           face.c, face.b, face.d\r
         ));\r
+\r
+        console.log(face);\r
         \r
-        let xLength =\r
-          result.geometry.vertices[face.b]\r
-          .sub(result.geometry.vertices[face.a])\r
-          .length();\r
-        let yLength =\r
-          result.geometry.vertices[face.c]\r
-          .sub(result.geometry.vertices[face.a])\r
-          .length();\r
+        let xLength = vectorLength(result.geometry.vertices[face.b], result.geometry.vertices[face.a]);\r
+        let yLength = vectorLength(result.geometry.vertices[face.c], result.geometry.vertices[face.a]);\r
         let weight = xLength * yLength / 2;\r
 \r
-        xLength =\r
-          result.geometry.vertices[face.b]\r
-          .sub(result.geometry.vertices[face.d])\r
-          .length();\r
-        yLength =\r
-          result.geometry.vertices[face.c]\r
-          .sub(result.geometry.vertices[face.d])\r
-          .length();\r
-\r
+        xLength = vectorLength(result.geometry.vertices[face.b], result.geometry.vertices[face.d]);\r
+        yLength = vectorLength(result.geometry.vertices[face.c], result.geometry.vertices[face.d]);\r
         weight += xLength * yLength / 2;\r
 \r
         result.vertexWeights[face.a] += weight / 4;\r
@@ -151,7 +172,7 @@ function init() {
   const directionalLight = new THREE.DirectionalLight(0xffffff, 1);\r
   scene.add(directionalLight);\r
 \r
-  let cloth = Cloth.CreateBasic(10, 10, 4, 4);\r
+  let cloth = Cloth.CreateBasic(10, 10, 5, 5);\r
   const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });\r
   const mesh = new THREE.Mesh(cloth.geometry, material);\r
   scene.add(mesh);\r