]> gitweb.ps.run Git - cloth_sim/commitdiff
make vertices rigid, tune parameters
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Mon, 18 Jan 2021 14:10:05 +0000 (15:10 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Mon, 18 Jan 2021 14:10:05 +0000 (15:10 +0100)
Scripts/cloth.js
Scripts/main.js

index 9389ef60c13cb8e827d6054d92012727ac4129a1..600ac8f21ac81b8582017b5f5bf307c6a4502020 100644 (file)
@@ -76,6 +76,11 @@ export class Spring {
 \r
     return direction;\r
   }\r
+\r
+  update(vertices) {\r
+    let length = vectorLength(vertices[this.index1], vertices[this.index2]);\r
+    this.currentLength = length;\r
+  }\r
 }\r
 \r
 /**\r
@@ -94,7 +99,7 @@ export class Cloth {
 \r
   vertexWeights = [];\r
 \r
-\r
+  vertexRigidness = [];\r
 \r
   /**\r
    * creates a rectangular piece of cloth\r
@@ -170,6 +175,12 @@ export class Cloth {
      * with generated vertices and faces\r
      */\r
     this.createExplicit(vertices, faces);\r
+\r
+    /**\r
+     * hand cloth from left and right upper corners\r
+     */\r
+    this.vertexRigidness[0] = true;\r
+    this.vertexRigidness[numPointsWidth-1] = true;\r
   }\r
 \r
   /**\r
@@ -189,6 +200,7 @@ export class Cloth {
       this.geometry.vertices.push(vertices[i]);\r
       this.previousPositions.push(vertices[i]);\r
       this.vertexWeights.push(0);\r
+      this.vertexRigidness.push(false);\r
     }\r
     /**\r
      * copy faces,\r
@@ -306,22 +318,30 @@ export class Cloth {
    * @param {number} dt \r
    */\r
   simulate(dt) {\r
-\r
-\r
-\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
  \r
-      currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/2000);\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
      \r
       this.previousPositions[i] = currentPosition;\r
       this.geometry.vertices[i] = currentPosition;\r
-      \r
     }\r
-    console.log(this.geometry.vertices[0]);\r
+\r
+    //this.getAcceleration(1, dt, true);\r
+    \r
     this.time += dt;\r
 \r
+    for (let face of this.faces) {\r
+      for (let spring of face.springs) {\r
+        spring.update(this.geometry.vertices);\r
+      }\r
+    }\r
+\r
     /**\r
      * let THREE JS compute bounding sphere around generated mesh\r
      * needed for View Frustum Culling internally\r
@@ -341,22 +361,26 @@ export class Cloth {
  *  @param {number} dt The time passed since last frame\r
  */\r
 getAcceleration(vertexIndex, dt) {\r
+  if (this.vertexRigidness[vertexIndex])\r
+    return new THREE.Vector3(0, 0, 0);\r
 \r
   let vertex = this.geometry.vertices[vertexIndex];\r
 \r
   // Mass of vertex\r
   let M = this.vertexWeights[vertexIndex];\r
   // constant gravity\r
-  let g = new THREE.Vector3(0, -1.8, 0);\r
+  let g = new THREE.Vector3(0, -9.8, 0);\r
   // stiffness\r
-  let k = 5;\r
+  let k = 300;\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.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
   );\r
+  fWind = new THREE.Vector3(0, 0, 0);\r
 \r
   /**\r
    * constant determined by the properties of the surrounding fluids (air)\r
@@ -365,17 +389,19 @@ getAcceleration(vertexIndex, dt) {
   let a = 1;\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
+    (vertex.x - this.previousPositions[vertexIndex].x) * dt,\r
+    (vertex.y - this.previousPositions[vertexIndex].y) * dt,\r
+    (vertex.z - this.previousPositions[vertexIndex].z) * dt\r
   );\r
 \r
-\r
-  let fAirResistance = velocity.multiplyScalar(-a);\r
+  // TODO: include air resistance\r
+  let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);\r
+  fAirResistance = new THREE.Vector3(0, 0, 0);\r
 \r
   let springSum = new THREE.Vector3(0, 0, 0);\r
 \r
   // Get the bounding springs and add them to the needed springs\r
+  // TODO: optimize\r
   for (let i in this.faces) {\r
     if (this.faces[i].a == vertexIndex || this.faces[i].b == vertexIndex || this.faces[i].c == vertexIndex || this.faces[i].d == vertexIndex) {\r
       for (let j in this.faces[i].springs) {\r
@@ -385,26 +411,20 @@ getAcceleration(vertexIndex, dt) {
           let springDirection = spring.getDirection(this.geometry.vertices);\r
 \r
 \r
-          if (this.faces[i].springs[j].index1 == vertexIndex)\r
+          if (this.faces[i].springs[j].index2 == vertexIndex)\r
             springDirection.multiplyScalar(-1);\r
 \r
           springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));\r
-\r
         }\r
       }\r
     }\r
   }\r
-\r
   \r
   let result = new THREE.Vector3(1, 1, 1);\r
 \r
-  \r
   result.multiplyScalar(M).multiply(g).add(fWind).add(fAirResistance).sub(springSum);\r
   \r
-\r
   return result;\r
-\r
-\r
 }\r
 \r
 /**\r
@@ -422,14 +442,23 @@ verlet(currentPosition, previousPosition, acceleration, passedTime) {
   // Dependency for one vertex: gravity, fluids/air, springs\r
 \r
   let nextPosition = new THREE.Vector3(\r
-    2 * currentPosition.x - previousPosition.x + acceleration.x * (passedTime * passedTime),\r
-    2 * currentPosition.y - previousPosition.y + acceleration.y * (passedTime * passedTime),\r
-    2 * currentPosition.z - previousPosition.z + acceleration.z * (passedTime * passedTime),\r
+    (2 * currentPosition.x) - previousPosition.x + acceleration.x * (passedTime * passedTime),\r
+    (2 * currentPosition.y) - previousPosition.y + acceleration.y * (passedTime * passedTime),\r
+    (2 * currentPosition.z) - previousPosition.z + acceleration.z * (passedTime * passedTime),\r
   );\r
 \r
   return nextPosition;\r
 }\r
 \r
+euler(currentPosition, acceleration, passedTime) {\r
+  let nextPosition = new THREE.Vector3(\r
+    currentPosition.x + acceleration.x * passedTime,\r
+    currentPosition.y + acceleration.y * passedTime,\r
+    currentPosition.z + acceleration.z * passedTime,\r
+  );\r
+\r
+  return nextPosition;\r
+}\r
 \r
 }\r
 \r
index 5ed1866027c92343b1cde743c6b527dedbeb2f27..ececd6af5bda8d4546a29fc286c73f08a78f3a6f 100644 (file)
@@ -65,13 +65,16 @@ function init() {
    */\r
   const canvasSpace = 200;\r
 \r
+  /** Constant Frame Time */\r
+  const frameTime = 1000.0 / 60.0;\r
+\r
   /** Setup scene */\r
   let [scene, camera, renderer] = setup_scene(canvasSpace);\r
   \r
   /** setup cloth and generate debug mesh */\r
   let cloth = new Cloth();\r
-  cloth.createBasic(10, 10, 5, 5);\r
-  cloth.createDebugMesh(scene);\r
+  cloth.createBasic(10, 10, 10, 10);\r
+  //cloth.createDebugMesh(scene);\r
 \r
   const material = new THREE.MeshBasicMaterial({ color: 0x0000ff });\r
   const mesh = new THREE.Mesh(cloth.geometry, material);\r
@@ -87,12 +90,11 @@ function init() {
    * @param {number} dt - time passed since last frame\r
    */\r
   function animate(dt) {\r
-    \r
     cloth.simulate(dt);\r
 \r
     setTimeout(() => {\r
-      animate(2000);\r
-    }, 2000);\r
+      animate(frameTime);\r
+    }, frameTime);\r
     renderer.render(scene, camera);\r
   }\r
 \r