+\r
+ previousPositions = [];\r
+ time = 0;\r
+ /**\r
+ * \r
+ * @param {number} dt time in seconds since last frame\r
+ */\r
+ simulate(dt) {\r
+ for (let i in this.geometry.vertices) {\r
+ let acceleration = this.getAcceleration(i, dt);\r
+\r
+ //acceleration.clampLength(0, 10);\r
+\r
+ if (Math.abs(acceleration.length()) <= 10e-4) {\r
+ acceleration.set(0, 0, 0);\r
+ }\r
+ \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].copy(this.geometry.vertices[i]);\r
+ this.geometry.vertices[i].copy(currentPosition);\r
+ }\r
+ //console.log(this.getAcceleration(1, dt));\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
+ */\r
+\r
+ this.geometry.verticesNeedUpdate = true;\r
+ this.geometry.elementsNeedUpdate = true;\r
+ this.geometry.computeBoundingSphere();\r
+\r
+ }\r
+\r
+\r
+\r
+/**\r
+ * Equation of motion for each vertex which represents the acceleration \r
+ * @param {number} vertexIndex The index of the current vertex whose acceleration should be calculated\r
+ * @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, -9.8, 0);\r
+ // stiffness\r
+ let k = 500;\r
+\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
+ );\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
+ \r
+ let velocity = new THREE.Vector3(\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
+ //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
+ // 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
+ if (this.faces[i].springs[j].index1 == vertexIndex || this.faces[i].springs[j].index2 == vertexIndex) {\r
+\r
+ let spring = this.faces[i].springs[j];\r
+ let springDirection = spring.getDirection(this.geometry.vertices);\r
+\r
+\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
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ let result = new THREE.Vector3(1, 1, 1);\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
+/**\r
+ * The Verlet algorithm as an integrator \r
+ * to get the next position of a vertex \r
+ * @param {Vector3} currentPosition \r
+ * @param {Vector3} previousPosition \r
+ * @param {Vector3} acceleration \r
+ * @param {number} passedTime The delta time since last frame\r
+ */\r
+verlet(currentPosition, previousPosition, acceleration, passedTime) {\r
+ // verlet algorithm\r
+ // 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
+ 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
+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