abouttreesummaryrefslogcommitdiff
path: root/Scripts/cloth.js
diff options
context:
space:
mode:
authorPatrick Schönberger2021-02-01 14:08:56 +0100
committerPatrick Schönberger2021-02-01 14:08:56 +0100
commit8ecb6f064f0708ed010f1239a7bddc283411b6e7 (patch)
tree0e753b3295d66f4b4e4393244cc4b9f63a6cec89 /Scripts/cloth.js
parent6b8872e162f44cb469a0dc4cdbb30981912a4ee3 (diff)
downloadcloth_sim-8ecb6f064f0708ed010f1239a7bddc283411b6e7.tar.gz
cloth_sim-8ecb6f064f0708ed010f1239a7bddc283411b6e7.zip
cloth-cloth collision
Diffstat (limited to 'Scripts/cloth.js')
-rw-r--r--Scripts/cloth.js47
1 files changed, 39 insertions, 8 deletions
diff --git a/Scripts/cloth.js b/Scripts/cloth.js
index 1ec79d5..0708d5e 100644
--- a/Scripts/cloth.js
+++ b/Scripts/cloth.js
@@ -98,8 +98,10 @@ export class Cloth {
vertexRigidness = [];
+ fixedPoints = [];
+
externalForces = [];
- windForce = 100;
+ windForce = 50;
/**
* creates a rectangular piece of cloth
@@ -133,7 +135,7 @@ export class Cloth {
for (let y = 0; y < numPointsHeight; y++) {
for (let x = 0; x < numPointsWidth; x++) {
vertices.push(
- new THREE.Vector3(x * stepWidth, height - y * stepHeight, 0)
+ new THREE.Vector3((x - (numPointsWidth/2)) * stepWidth, height - y * stepHeight, 0)
);
}
}
@@ -182,8 +184,10 @@ export class Cloth {
/**
* hand cloth from left and right upper corners
*/
- this.vertexRigidness[0] = true;
- this.vertexRigidness[numPointsWidth-1] = true;
+ //this.vertexRigidness[0] = true;
+ //this.vertexRigidness[numPointsWidth * (numPointsHeight - 1)] = true;
+ this.fixedPoints.push(getVertexIndex(8, 10));
+ this.fixedPoints.push(getVertexIndex(12, 9));
}
/**
@@ -341,7 +345,8 @@ export class Cloth {
this.previousPositions[i].copy(this.geometry.vertices[i]);
this.geometry.vertices[i].copy(currentPosition);
}
- //console.log(this.getAcceleration(1, dt));
+
+ this.checkIntersect();
this.time += dt;
@@ -364,7 +369,31 @@ export class Cloth {
}
-
+checkIntersect() {
+ let npw = this.numPointsWidth;
+ function getX(i, ) { return i % npw; }
+ function getY(i) { return Math.floor(i / npw); }
+ for (let i in this.geometry.vertices) {
+ for (let j in this.geometry.vertices) {
+ this.vertexRigidness[i] = false;
+ this.vertexRigidness[j] = false;
+ if (i == j || (Math.abs(getX(i) - getX(j)) == 1 && Math.abs(getY(i) - getY(j)) == 1))
+ continue;
+ let posI = this.geometry.vertices[i];
+ let posJ = this.geometry.vertices[j];
+ let dist = posI.distanceTo(posJ);
+ const collisionDistance = 0.5;
+ if (dist < collisionDistance) {
+ this.vertexRigidness[i] = true;
+ this.vertexRigidness[j] = true;
+ let diff = this.geometry.vertices[i].clone().sub(this.geometry.vertices[j]).normalize().multiplyScalar((collisionDistance - dist) * 1.001 / 2);
+ this.geometry.vertices[i].add(diff);
+ this.geometry.vertices[j].sub(diff);
+ console.log(this.geometry.vertices[i].distanceTo(this.geometry.vertices[j]));
+ }
+ }
+ }
+}
/**
* Equation of motion for each vertex which represents the acceleration
@@ -372,8 +401,10 @@ export class Cloth {
* @param {number} dt The time passed since last frame
*/
getAcceleration(vertexIndex, dt) {
- if (this.vertexRigidness[vertexIndex])
+ if (this.fixedPoints.includes(parseInt(vertexIndex)) ||
+ this.vertexRigidness[vertexIndex]) {
return new THREE.Vector3(0, 0, 0);
+ }
let externalForce = this.externalForces[vertexIndex];
let vertex = this.geometry.vertices[vertexIndex];//.add(externalForce);
@@ -383,7 +414,7 @@ getAcceleration(vertexIndex, dt) {
// constant gravity
let g = new THREE.Vector3(0, -9.8, 0);
// stiffness
- let k = 1000;
+ let k = 500;
// Wind vector
let fWind = new THREE.Vector3(