]> gitweb.ps.run Git - cloth_sim/blobdiff - Scripts/cloth.js
fix spring force calculation
[cloth_sim] / Scripts / cloth.js
index 038b235e4a4da2817c487f0d539af2562d340640..e39b95b9850400cf98cbd7c20f5f3aa1b5d73fbb 100644 (file)
@@ -112,6 +112,9 @@ export class Cloth {
     let vertices = [];\r
     let faces = [];\r
 \r
+    this.numPointsWidth = numPointsWidth;\r
+    this.numPointsHeight = numPointsHeight;\r
+\r
     /**\r
      * distance between two vertices horizontally/vertically\r
      * divide by the number of points minus one\r
@@ -373,7 +376,7 @@ getAcceleration(vertexIndex, dt) {
   // constant gravity\r
   let g = new THREE.Vector3(0, -9.8, 0);\r
   // stiffness\r
-  let k = 500;\r
+  let k = 1000;\r
 \r
   // Wind vector\r
   let fWind = new THREE.Vector3(\r
@@ -381,13 +384,12 @@ getAcceleration(vertexIndex, dt) {
     Math.cos(vertex.z * this.time),\r
     Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))\r
   );\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 = 0.01;\r
+  let a = 0.1;\r
   \r
   let velocity = new THREE.Vector3(\r
     (this.previousPositions[vertexIndex].x - vertex.x) / dt,\r
@@ -404,8 +406,8 @@ getAcceleration(vertexIndex, dt) {
   // Get the bounding springs and add them to the needed springs\r
   // TODO: optimize\r
 \r
-  const numPointsX = 10;\r
-  const numPointsY = 10;\r
+  const numPointsX = this.numPointsWidth;\r
+  const numPointsY = this.numPointsHeight;\r
   const numFacesX = numPointsX - 1;\r
   const numFacesY = numPointsY - 1;\r
 \r
@@ -490,13 +492,19 @@ 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
-\r
+  const DRAG = 0.97;\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
+    (currentPosition.x - previousPosition.x) * DRAG + currentPosition.x + acceleration.x * (passedTime * passedTime),\r
+    (currentPosition.y - previousPosition.y) * DRAG + currentPosition.y + acceleration.y * (passedTime * passedTime),\r
+    (currentPosition.z - previousPosition.z) * DRAG + currentPosition.z + acceleration.z * (passedTime * passedTime),\r
   );\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
+  // );\r
+\r
   return nextPosition;\r
 }\r
 \r
@@ -510,5 +518,12 @@ euler(currentPosition, acceleration, passedTime) {
   return nextPosition;\r
 }\r
 \r
+wind(intersects) {\r
+  let intersect = intersects[0];\r
+  this.geometry.vertices[intersect.face.a].z -= 0.05;\r
+  this.geometry.vertices[intersect.face.b].z -= 0.05;\r
+  this.geometry.vertices[intersect.face.c].z -= 0.05;\r
+}\r
+\r
 }\r
 \r