]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
rename to match mass spring model
[cloth_sim] / Scripts / cloth.js
index c77fb1c911a444746845901c7a7d411f86631494..26a8ffed62449aaee2f5dcbd55d5965c5b310032 100644 (file)
@@ -13,7 +13,7 @@ const options = {
   wind: true,\r
 };\r
 \r
-class Constraint {\r
+class Spring {\r
   constructor(p1, p2, restDist) {\r
     this.p1 = p1;\r
     this.p2 = p2;\r
@@ -48,7 +48,7 @@ class Constraint {
   }\r
 }\r
 \r
-class Particle {\r
+class Mass {\r
   movableTmp = true;\r
   movable = true;\r
 \r
@@ -99,13 +99,13 @@ class Cloth {
 \r
     /**\r
      * iterate over the number of vertices in x/y axis\r
-     * and add a new Particle to "particles"\r
+     * and add a new Particle to "masses"\r
      */\r
-    this.particles = [];\r
+    this.masses = [];\r
     for (let y = 0; y < numPointsHeight; y++) {\r
       for (let x = 0; x < numPointsWidth; x++) {\r
-        this.particles.push(\r
-          new Particle(\r
+        this.masses.push(\r
+          new Mass(\r
             (x - ((numPointsWidth-1)/2)) * stepWidth,\r
             height - (y + ((numPointsHeight-1)/2)) * stepHeight,\r
             0,\r
@@ -114,33 +114,33 @@ class Cloth {
       }\r
     }\r
 \r
-    //this.particles[this.getVertexIndex(0, 0)].movable = false;\r
+    //this.masses[this.getVertexIndex(0, 0)].movable = false;\r
     const n = 3;\r
     for (let i = 0; i < numPointsHeight; i++)\r
-      this.particles[this.getVertexIndex(0, i)].movable = false;\r
-    //this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
-    //this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;\r
+      this.masses[this.getVertexIndex(0, i)].movable = false;\r
+    //this.masses[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
+    //this.masses[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
 \r
     /**\r
-     * generate constraints (springs)\r
+     * generate springs (springs)\r
      */\r
-    this.constraints = [];\r
+    this.springs = [];\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
+          this.springs.push(new Spring(\r
+            this.masses[this.getVertexIndex(x, y)],\r
+            this.masses[this.getVertexIndex(x+1, y)],\r
             REST_DIST_X\r
           ));\r
         }\r
         if (y < numPointsHeight-1) {\r
-          this.constraints.push(new Constraint(\r
-            this.particles[this.getVertexIndex(x, y)],\r
-            this.particles[this.getVertexIndex(x, y+1)],\r
+          this.springs.push(new Spring(\r
+            this.masses[this.getVertexIndex(x, y)],\r
+            this.masses[this.getVertexIndex(x, y+1)],\r
             REST_DIST_Y\r
           ));\r
         }\r
@@ -154,8 +154,8 @@ class Cloth {
     const indices = [];\r
     const uvs = [];\r
 \r
-    for (let i in this.particles) {\r
-      let particle = this.particles[i];\r
+    for (let i in this.masses) {\r
+      let particle = this.masses[i];\r
       vertices.push(\r
         particle.position.x,\r
         particle.position.y,\r
@@ -195,8 +195,8 @@ class Cloth {
   }\r
   updateGeometry(geometry) {\r
     const positions = geometry.attributes.position.array;\r
-    for (let i in this.particles) {\r
-      let p = this.particles[i];\r
+    for (let i in this.masses) {\r
+      let p = this.masses[i];\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
@@ -207,8 +207,8 @@ class Cloth {
   }\r
   simulate(dt) {\r
     let now = performance.now();\r
-    for (let particle of this.particles) {\r
-      let vertex = particle.position;\r
+    for (let mass of this.masses) {\r
+      let vertex = mass.position;\r
       let fWind = new THREE.Vector3(\r
         this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1),\r
         this.windFactor.y * Math.cos(vertex.z * now),\r
@@ -216,16 +216,16 @@ class Cloth {
       );\r
       // normalize then multiply?\r
       if (options.wind)\r
-        particle.addForce(fWind);\r
+        mass.addForce(fWind);\r
       // calculate wind with normal?\r
 \r
-      particle.addForce(GRAVITY);\r
+      mass.addForce(GRAVITY);\r
 \r
-      particle.verlet(dt);\r
+      mass.verlet(dt);\r
     }\r
 \r
     \r
-    for (let constraint of this.constraints) {\r
+    for (let constraint of this.springs) {\r
       constraint.satisfy();\r
     }\r
 \r
@@ -233,10 +233,10 @@ class Cloth {
   }\r
 \r
   intersect() {\r
-    for (let i in this.particles) {\r
-      for (let j in this.particles) {  \r
-        let p1 = this.particles[i];\r
-        let p2 = this.particles[j];\r
+    for (let i in this.masses) {\r
+      for (let j in this.masses) {  \r
+        let p1 = this.masses[i];\r
+        let p2 = this.masses[j];\r
 \r
         p1.movableTmp = true;\r
         p2.movableTmp = true;\r
@@ -273,13 +273,13 @@ class Cloth {
   blow(camPos, intersects) {\r
     let face = intersects[0].face;\r
     let dir = intersects[0].point.clone().sub(camPos).multiplyScalar(100);\r
-    this.particles[face.a].addForce(dir);\r
-    this.particles[face.b].addForce(dir);\r
-    this.particles[face.c].addForce(dir);\r
+    this.masses[face.a].addForce(dir);\r
+    this.masses[face.b].addForce(dir);\r
+    this.masses[face.c].addForce(dir);\r
   }\r
   drag(mousePosWorld, index) {\r
-    let dir = mousePosWorld.clone().sub(this.particles[index].position).multiplyScalar(200);\r
-    this.particles[index].addForce(dir);\r
+    let dir = mousePosWorld.clone().sub(this.masses[index].position).multiplyScalar(200);\r
+    this.masses[index].addForce(dir);\r
   }\r
 \r
   /**\r