]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
Adjusting parameters
[cloth_sim] / Scripts / cloth.js
index 600ac8f21ac81b8582017b5f5bf307c6a4502020..f1ac682fa3c9a4841f425ee9412b0b9d523ff159 100644 (file)
@@ -6,9 +6,9 @@
  */\r
 function vectorLength(a, b) {\r
   let v1 = new THREE.Vector3();\r
-  v1.set(a.x, a.y, a.z);\r
+  v1.copy(a);\r
   let v2 = new THREE.Vector3();\r
-  v2.set(b.x, b.y, b.z);\r
+  v2.copy(b);\r
 \r
   return v1.sub(v2).length();\r
 }\r
@@ -65,11 +65,8 @@ export class Spring {
   }\r
 \r
   getDirection(vertices) {\r
-    let direction = new THREE.Vector3(\r
-      vertices[this.index1].x,\r
-      vertices[this.index1].y,\r
-      vertices[this.index1].z\r
-    );\r
+    let direction = new THREE.Vector3();\r
+    direction.copy(vertices[this.index1]);\r
 \r
     direction.sub(vertices[this.index2]);\r
     direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2]));\r
@@ -197,8 +194,10 @@ export class Cloth {
      * Copy vertices and initialize vertex weights to 0\r
      */\r
     for (let i in vertices) {\r
-      this.geometry.vertices.push(vertices[i]);\r
-      this.previousPositions.push(vertices[i]);\r
+      this.geometry.vertices.push(vertices[i].clone());\r
+      this.previousPositions.push(vertices[i].clone());\r
+      // this.geometry.vertices.push(vertices[i]);\r
+      // this.previousPositions.push(vertices[i]);\r
       this.vertexWeights.push(0);\r
       this.vertexRigidness.push(false);\r
     }\r
@@ -315,24 +314,25 @@ export class Cloth {
   time = 0;\r
   /**\r
    * \r
-   * @param {number} dt \r
+   * @param {number} dt time in seconds since last frame\r
    */\r
   simulate(dt) {\r
     for (let i in this.geometry.vertices) {\r
-      let currentPosition;\r
       let acceleration = this.getAcceleration(i, dt);\r
 \r
-      // TODO: decide on clamping\r
-      acceleration.clampLength(0, 100);\r
+      //acceleration.clampLength(0, 10);\r
+\r
+      if (Math.abs(acceleration.length()) <= 10e-4) {\r
+        acceleration.set(0, 0, 0);\r
+      }\r
  \r
-      currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/500);\r
-      //currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt/10);\r
+      let currentPosition = this.verlet(this.geometry.vertices[i].clone(), this.previousPositions[i].clone(), acceleration, dt);\r
+      //let currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt);\r
      \r
-      this.previousPositions[i] = currentPosition;\r
-      this.geometry.vertices[i] = currentPosition;\r
+      this.previousPositions[i].copy(this.geometry.vertices[i]);\r
+      this.geometry.vertices[i].copy(currentPosition);\r
     }\r
-\r
-    //this.getAcceleration(1, dt, true);\r
+    //console.log(this.getAcceleration(1, dt));\r
     \r
     this.time += dt;\r
 \r
@@ -371,33 +371,32 @@ getAcceleration(vertexIndex, dt) {
   // constant gravity\r
   let g = new THREE.Vector3(0, -9.8, 0);\r
   // stiffness\r
-  let k = 300;\r
+  let k = 500;\r
 \r
   // Wind vector\r
-  // TODO: include wind vector\r
   let fWind = new THREE.Vector3(\r
     Math.sin(vertex.x * vertex.y * this.time),\r
-    Math.cos(vertex.z* this.time),\r
+    Math.cos(vertex.z * this.time),\r
     Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
   );\r
-  fWind = new THREE.Vector3(0, 0, 0);\r
+  fWind.set(0, 0, 0);\r
 \r
   /**\r
    * constant determined by the properties of the surrounding fluids (air)\r
    * achievement of cloth effects through try out\r
    * */\r
-  let a = 1;\r
-\r
+  let a = 0.01;\r
+  \r
   let velocity = new THREE.Vector3(\r
-    (vertex.x - this.previousPositions[vertexIndex].x) * dt,\r
-    (vertex.y - this.previousPositions[vertexIndex].y) * dt,\r
-    (vertex.z - this.previousPositions[vertexIndex].z) * dt\r
+    (this.previousPositions[vertexIndex].x - vertex.x) / dt,\r
+    (this.previousPositions[vertexIndex].y - vertex.y) / dt,\r
+    (this.previousPositions[vertexIndex].z - vertex.z) / dt\r
   );\r
 \r
-  // TODO: include air resistance\r
-  let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);\r
-  fAirResistance = new THREE.Vector3(0, 0, 0);\r
+  //console.log(velocity, vertex, this.previousPositions[vertexIndex]);\r
 \r
+  let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);\r
+  \r
   let springSum = new THREE.Vector3(0, 0, 0);\r
 \r
   // Get the bounding springs and add them to the needed springs\r
@@ -411,10 +410,11 @@ getAcceleration(vertexIndex, dt) {
           let springDirection = spring.getDirection(this.geometry.vertices);\r
 \r
 \r
-          if (this.faces[i].springs[j].index2 == vertexIndex)\r
+          if (this.faces[i].springs[j].index1 == vertexIndex)\r
             springDirection.multiplyScalar(-1);\r
+     \r
+          springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\r
 \r
-          springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));\r
         }\r
       }\r
     }\r
@@ -422,8 +422,10 @@ getAcceleration(vertexIndex, dt) {
   \r
   let result = new THREE.Vector3(1, 1, 1);\r
 \r
-  result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum);\r
-  \r
+  let temp = result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).clone();\r
+  result.sub(springSum);\r
+  document.getElementById("Output").innerText = "SpringSum: " + Math.floor(springSum.y) + "\nTemp: " + Math.floor(temp.y);\r
+\r
   return result;\r
 }\r
 \r