*/\r
function vectorLength(a, b) {\r
let v1 = new THREE.Vector3();\r
- v1.set(a.x, a.y, a.z);\r
+ v1.copy(a);\r
let v2 = new THREE.Vector3();\r
- v2.set(b.x, b.y, b.z);\r
+ v2.copy(b);\r
\r
return v1.sub(v2).length();\r
}\r
}\r
\r
getDirection(vertices) {\r
- let direction = new THREE.Vector3(\r
- vertices[this.index1].x,\r
- vertices[this.index1].y,\r
- vertices[this.index1].z\r
- );\r
+ let direction = new THREE.Vector3();\r
+ direction.copy(vertices[this.index1]);\r
\r
direction.sub(vertices[this.index2]);\r
direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2]));\r
* Copy vertices and initialize vertex weights to 0\r
*/\r
for (let i in vertices) {\r
- this.geometry.vertices.push(vertices[i]);\r
- this.previousPositions.push(vertices[i]);\r
+ this.geometry.vertices.push(vertices[i].clone());\r
+ this.previousPositions.push(vertices[i].clone());\r
+ // this.geometry.vertices.push(vertices[i]);\r
+ // this.previousPositions.push(vertices[i]);\r
this.vertexWeights.push(0);\r
this.vertexRigidness.push(false);\r
}\r
time = 0;\r
/**\r
* \r
- * @param {number} dt \r
+ * @param {number} dt time in seconds since last frame\r
*/\r
simulate(dt) {\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
+ //acceleration.clampLength(0, 10);\r
+\r
+ if (Math.abs(acceleration.length()) <= 10e-4) {\r
+ acceleration.set(0, 0, 0);\r
+ }\r
\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
+ 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] = currentPosition;\r
- this.geometry.vertices[i] = currentPosition;\r
+ this.previousPositions[i].copy(this.geometry.vertices[i]);\r
+ this.geometry.vertices[i].copy(currentPosition);\r
}\r
-\r
- //this.getAcceleration(1, dt, true);\r
+ //console.log(this.getAcceleration(1, dt));\r
\r
this.time += dt;\r
\r
// constant gravity\r
let g = new THREE.Vector3(0, -9.8, 0);\r
// stiffness\r
- let k = 300;\r
+ let k = 1000;\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.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
+ 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 = 1;\r
+ let a = 0.01;\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
+ (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
- // TODO: include air resistance\r
- let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);\r
- fAirResistance = new THREE.Vector3(0, 0, 0);\r
+ //console.log(velocity, vertex, this.previousPositions[vertexIndex]);\r
+\r
+ let fAirResistance = velocity.cross(velocity).multiplyScalar(-a);\r
\r
let springSum = new THREE.Vector3(0, 0, 0);\r
\r
let springDirection = spring.getDirection(this.geometry.vertices);\r
\r
\r
- if (this.faces[i].springs[j].index2 == vertexIndex)\r
+ if (this.faces[i].springs[j].index1 == vertexIndex)\r
springDirection.multiplyScalar(-1);\r
\r
- springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));\r
+ springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));\r
}\r
}\r
}\r