\r
vertexRigidness = [];\r
\r
+ fixedPoints = [];\r
+\r
+ externalForces = [];\r
+ windForce = 50;\r
+\r
+ windFactor = new THREE.Vector3(0, 0, 0);\r
+\r
/**\r
* creates a rectangular piece of cloth\r
* takes the size of the cloth\r
let vertices = [];\r
let faces = [];\r
\r
+ this.width = width;\r
+ this.height = height;\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
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
/**\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
// this.previousPositions.push(vertices[i]);\r
this.vertexWeights.push(0);\r
this.vertexRigidness.push(false);\r
+ this.externalForces.push(new THREE.Vector3(0,0,0));\r
}\r
/**\r
* copy faces,\r
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
*/\r
this.geometry.computeBoundingSphere();\r
this.geometry.computeFaceNormals();\r
+ this.geometry.computeVertexNormals();\r
}\r
\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.checkIntersect();\r
\r
this.time += dt;\r
\r
this.geometry.elementsNeedUpdate = true;\r
this.geometry.computeBoundingSphere();\r
this.geometry.computeFaceNormals();\r
+ this.geometry.computeVertexNormals();\r
\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
* @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 vertex = this.geometry.vertices[vertexIndex];\r
+ let externalForce = this.externalForces[vertexIndex];\r
+ let vertex = this.geometry.vertices[vertexIndex];//.add(externalForce);\r
\r
// Mass of vertex\r
let M = this.vertexWeights[vertexIndex];\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
+ 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
// 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
}\r
\r
let result = new THREE.Vector3(1, 1, 1);\r
+ result.multiplyScalar(M).multiply(g).add(fWind).add(externalForce).add(fAirResistance).sub(springSum);\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
+ document.getElementById("Output").innerText = "SpringSum: " + Math.floor(springSum.y);\r
+\r
+ let threshold = 1;\r
+ let forceReduktion = 0.8;\r
+ if(Math.abs(externalForce.z) > threshold){\r
+ externalForce.z *= forceReduktion;\r
+ } else {\r
+ externalForce.z = 0;\r
+ }\r
+\r
+ if(Math.abs(externalForce.y) > threshold){\r
+ externalForce.y *= forceReduktion;\r
+ } else {\r
+ externalForce.y = 0;\r
+ }\r
+\r
+ if(Math.abs(externalForce.x) > threshold){\r
+ externalForce.x *= forceReduktion;\r
+ } else {\r
+ externalForce.x = 0;\r
+ }\r
+ \r
+ \r
\r
return result;\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
+ this.externalForces[intersect.face.a].z -= this.windForce;\r
+ this.externalForces[intersect.face.b].z -= this.windForce;\r
+ this.externalForces[intersect.face.c].z -= this.windForce;\r
+}\r
+\r
+mousePressed = false;\r
+mouseMoved = false;\r
+intersects;\r
+\r
+mousePress(intersects){\r
+ this.mousePressed = true;\r
+ this.intersects = intersects;\r
+\r
+}\r
+\r
+mouseMove(mousePos){\r
+ this.mouseMoved = true;\r
+ if(this.mousePressed){\r
+ let intersect = this.intersects[0];\r
+ this.externalForces[intersect.face.a].add(mousePos.clone().sub(this.geometry.vertices[intersect.face.a]).multiplyScalar(90));\r
+ /*\r
+ this.geometry.vertices[intersect.face.a].x = mousePos.x;\r
+ this.geometry.vertices[intersect.face.a].y = mousePos.y;\r
+ this.geometry.vertices[intersect.face.a].z = mousePos.z;\r
+ */ \r
+ }\r
+}\r
+\r
+mouseRelease(){\r
+ this.mousePressed = false;\r
}\r
\r
}\r