]> gitweb.ps.run Git - cloth_sim/commitdiff
simulate
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 6 Feb 2021 11:48:25 +0000 (12:48 +0100)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 6 Feb 2021 11:48:25 +0000 (12:48 +0100)
Scripts/cloth.js
Scripts/main.js
Textures/DeutschlandFlagge.jpg [new file with mode: 0644]

index db07ac302cf916846f12ce487ef5e7b4636547a9..0c4f02c64a4c4935c3c41ad4fa4b0a69d135337d 100644 (file)
@@ -1,14 +1,35 @@
+const DAMPING = 0.03;\r
+const DRAG = 1 - DAMPING;\r
 const MASS = 0.1;\r
+const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);\r
+const K = 1;\r
+\r
+let tmpCorrection;\r
 \r
 class Constraint {\r
-  constructor(p1, p2, distance) {\r
+  constructor(p1, p2, restDist) {\r
     this.p1 = p1;\r
     this.p2 = p2;\r
-    this.distance = distance;\r
+    this.restDist = restDist;\r
+  }\r
+\r
+  satisfy() {\r
+    const diff = this.p2.position.clone().sub(this.p1.position);\r
+    const currentDist = diff.length();\r
+    if (currentDist == 0) return;\r
+    if (currentDist <= this.restDist) return;\r
+    const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));\r
+    correction.multiplyScalar(K);\r
+    tmpCorrection = correction;\r
+    const correctionHalf = correction.multiplyScalar(0.5);\r
+    this.p1.position.add(correctionHalf);\r
+    this.p2.position.sub(correctionHalf);\r
   }\r
 }\r
 \r
 class Particle {\r
+  movable = true;\r
+\r
   constructor(x, y, z, mass) {\r
     this.position = new THREE.Vector3(x, y, z);\r
     this.previous = new THREE.Vector3(x, y, z);\r
@@ -16,10 +37,23 @@ class Particle {
     this.mass = mass;\r
   }\r
   addForce(force) {\r
-\r
+    this.acceleration.add(\r
+      force.clone().multiplyScalar(1/this.mass)\r
+    );\r
   }\r
   verlet(dt) {\r
-\r
+    // verlet algorithm\r
+    // next position = 2 * current Position - previous position + acceleration * (passed time)^2\r
+    // acceleration (dv/dt) = F(net)\r
+    const nextPosition = this.position.clone().sub(this.previous);\r
+    nextPosition.multiplyScalar(DRAG);\r
+    nextPosition.add(this.position);\r
+    nextPosition.add(this.acceleration.multiplyScalar(dt*dt));\r
+\r
+    this.previous = this.position;\r
+    this.position = nextPosition;\r
+\r
+    this.acceleration.set(0, 0, 0);\r
   }\r
 }\r
 \r
@@ -29,6 +63,7 @@ class Cloth {
     this.height = height;\r
     this.numPointsWidth = numPointsWidth;\r
     this.numPointsHeight = numPointsHeight;\r
+    this.windFactor = new THREE.Vector3(0.5, 0.2, 0.2);\r
 \r
     /**\r
      * distance between two vertices horizontally/vertically\r
@@ -55,6 +90,10 @@ class Cloth {
       }\r
     }\r
 \r
+    this.particles[this.getVertexIndex(0, 0)].movable = false;\r
+    this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
+    this.particles[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
@@ -71,7 +110,7 @@ class Cloth {
             REST_DIST_X\r
           ));\r
         }\r
-        if (x < numPointsWidth-1) {\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
@@ -126,13 +165,44 @@ class Cloth {
     return geometry;\r
   }\r
   updateGeometry(geometry) {\r
-\r
-  }\r
-  satisfyConstraints() {\r
-\r
+    const positions = geometry.attributes.position.array;\r
+    for (let i in this.particles) {\r
+      let p = this.particles[i];\r
+      if (p.movable) {\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
+      } else {\r
+        p.position = p.previous;\r
+      }\r
+    }\r
+    geometry.attributes.position.needsUpdate = true;\r
+    geometry.computeBoundingSphere();\r
+    geometry.computeVertexNormals();\r
   }\r
-  simulate() {\r
+  simulate(dt) {\r
+    let now = performance.now();\r
+    for (let particle of this.particles) {\r
+      let vertex = particle.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
+        this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
+      );\r
+      // normalize then multiply?\r
+      particle.addForce(fWind);\r
+      // calculate wind with normal?\r
+\r
+      particle.addForce(GRAVITY);\r
+\r
+      particle.verlet(dt);\r
+    }\r
 \r
+    \r
+    for (let constraint of this.constraints) {\r
+      constraint.satisfy();\r
+    }\r
+    //console.log(tmpCorrection);\r
   }\r
   /**\r
    * helper function to calculate index of vertex\r
index d54aa9fc258ddc5e670268f519d3cfcb70eefe7c..3353f4dae854d6249718305dde58b21f33f97bee 100644 (file)
@@ -98,7 +98,9 @@ function init() {
    * @param {number} dt - time passed since last frame in ms\r
    */\r
   function animate(dt) {\r
-    // simulate cloth\r
+    cloth.simulate(dt / 1000);\r
+\r
+    cloth.updateGeometry(clothGeometry);\r
     \r
     raycaster.setFromCamera( new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera );\r
 \r
diff --git a/Textures/DeutschlandFlagge.jpg b/Textures/DeutschlandFlagge.jpg
new file mode 100644 (file)
index 0000000..09e63ab
Binary files /dev/null and b/Textures/DeutschlandFlagge.jpg differ