]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
orbit/background, flag, wind control
[cloth_sim] / Scripts / cloth.js
index c873a3d05cd232d1aa5c361dc336b6e343393ed3..546f7d34ff05b7a561cee2a6fda279087ae63240 100644 (file)
@@ -98,8 +98,12 @@ export class Cloth {
 \r
   vertexRigidness = [];\r
 \r
+  fixedPoints = [];\r
+\r
   externalForces = [];\r
-  windForce = 0;\r
+  windForce = 50;\r
+\r
+  windFactor = new THREE.Vector3(0, 0, 0);\r
 \r
   /**\r
    * creates a rectangular piece of cloth\r
@@ -115,6 +119,8 @@ export class Cloth {
     let vertices = [];\r
     let faces = [];\r
 \r
+    this.width = width;\r
+    this.height = height;\r
     this.numPointsWidth = numPointsWidth;\r
     this.numPointsHeight = numPointsHeight;\r
 \r
@@ -133,7 +139,7 @@ export class Cloth {
     for (let y = 0; y < numPointsHeight; y++) {\r
       for (let x = 0; x < numPointsWidth; x++) {\r
         vertices.push(\r
-          new THREE.Vector3(x * stepWidth, height - y * stepHeight, 0)\r
+          new THREE.Vector3((x - ((numPointsWidth-1)/2)) * stepWidth, height - (y + ((numPointsHeight-1)/2)) * stepHeight, 0)\r
         );\r
       }\r
     }\r
@@ -182,8 +188,8 @@ export class Cloth {
     /**\r
      * hand cloth from left and right upper corners\r
      */\r
-    this.vertexRigidness[0] = true;\r
-    this.vertexRigidness[numPointsWidth-1] = true;\r
+    this.fixedPoints.push(getVertexIndex(0, 0));\r
+    this.fixedPoints.push(getVertexIndex(0, 19));\r
   }\r
 \r
   /**\r
@@ -240,6 +246,8 @@ export class Cloth {
       yLength = vectorLength(this.geometry.vertices[face.c], this.geometry.vertices[face.d]);\r
       weight += xLength * yLength / 2;\r
 \r
+      weight *= 10;\r
+\r
       /**\r
        * split weight equally between four surrounding vertices\r
        */\r
@@ -341,7 +349,8 @@ export class Cloth {
       this.previousPositions[i].copy(this.geometry.vertices[i]);\r
       this.geometry.vertices[i].copy(currentPosition);\r
     }\r
-    //console.log(this.getAcceleration(1, dt));\r
+    \r
+    this.checkIntersect();\r
     \r
     this.time += dt;\r
 \r
@@ -364,7 +373,32 @@ export class Cloth {
 \r
   }\r
 \r
-\r
+checkIntersect() {\r
+  let npw = this.numPointsWidth;\r
+  function getX(i, ) { return i % npw; }\r
+  function getY(i) { return Math.floor(i / npw); }\r
+  for (let i in this.geometry.vertices) {\r
+    for (let j in this.geometry.vertices) {\r
+      this.vertexRigidness[i] = false;\r
+      this.vertexRigidness[j] = false;\r
+      if (i == j || (Math.abs(getX(i) - getX(j)) == 1 && Math.abs(getY(i) - getY(j)) == 1))\r
+        continue;\r
+      let posI = this.geometry.vertices[i];\r
+      let posJ = this.geometry.vertices[j];\r
+      let dist = posI.distanceTo(posJ);\r
+      const collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight);\r
+      if (dist < collisionDistance) {\r
+        this.vertexRigidness[i] = true;\r
+        this.vertexRigidness[j] = true;\r
+        let diff = this.geometry.vertices[i].clone().sub(this.geometry.vertices[j]).normalize().multiplyScalar((collisionDistance - dist) * 1.001 / 2);\r
+        if (!(this.fixedPoints.includes(i) || this.fixedPoints.includes(j))) {\r
+          this.geometry.vertices[i].add(diff);\r
+          this.geometry.vertices[j].sub(diff);\r
+        }\r
+      }\r
+    }\r
+  }\r
+}\r
 \r
 /**\r
  * Equation of motion for each vertex which represents the acceleration \r
@@ -372,8 +406,10 @@ export class Cloth {
  *  @param {number} dt The time passed since last frame\r
  */\r
 getAcceleration(vertexIndex, dt) {\r
-  if (this.vertexRigidness[vertexIndex])\r
+  if (this.fixedPoints.includes(parseInt(vertexIndex)) ||\r
+      this.vertexRigidness[vertexIndex]) {\r
     return new THREE.Vector3(0, 0, 0);\r
+  }\r
 \r
   let externalForce = this.externalForces[vertexIndex];\r
   let vertex = this.geometry.vertices[vertexIndex];//.add(externalForce);\r
@@ -387,10 +423,11 @@ getAcceleration(vertexIndex, dt) {
 \r
   // 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
+    this.windFactor.x * (Math.sin(vertex.x * vertex.y * this.time)+1),\r
+    this.windFactor.y * Math.cos(vertex.z * this.time),\r
+    this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
   );\r
+  //console.log(fWind);\r
 \r
   /**\r
    * constant determined by the properties of the surrounding fluids (air)\r
@@ -520,7 +557,7 @@ verlet(currentPosition, previousPosition, acceleration, passedTime) {
   // next position = 2 * current Position - previous position + acceleration * (passed time)^2\r
   // acceleration (dv/dt) = F(net)\r
   // Dependency for one vertex: gravity, fluids/air, springs\r
-  const DRAG = 0.96;\r
+  const DRAG = 0.97;\r
   let nextPosition = new THREE.Vector3(\r
     (currentPosition.x - previousPosition.x) * DRAG + currentPosition.x + acceleration.x * (passedTime * passedTime),\r
     (currentPosition.y - previousPosition.y) * DRAG + currentPosition.y + acceleration.y * (passedTime * passedTime),\r