const DAMPING = 0.03;\r
const DRAG = 1 - DAMPING;\r
-const MASS = 0.35;\r
+const MASS = 0.1;\r
const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);\r
const K = 1;\r
\r
correction.multiplyScalar(K);\r
correction.clampLength(0, 1);\r
const correctionHalf = correction.multiplyScalar(0.5);\r
- if (this.p1.movable && this.p2.movable) {\r
+\r
+ let p1movable = this.p1.movable && this.p1.movableTmp;\r
+ let p2movable = this.p2.movable && this.p2.movableTmp;\r
+\r
+ if (p1movable && p2movable) {\r
this.p1.position.add(correctionHalf);\r
this.p2.position.sub(correctionHalf);\r
- } else if (! this.p1.movable && this.p2.movable) {\r
+ } else if (! p1movable && p2movable) {\r
this.p2.position.sub(correction);\r
- } else if (this.p1.movable && ! this.p2.movable) {\r
+ } else if (p1movable && ! p2movable) {\r
this.p1.position.add(correction);\r
}\r
}\r
}\r
\r
class Particle {\r
+ movableTmp = true;\r
movable = true;\r
\r
constructor(x, y, z, mass) {\r
nextPosition.add(this.position);\r
nextPosition.add(this.acceleration.multiplyScalar(dt*dt));\r
\r
- if (this.movable) {\r
+ if (this.movable && this.movableTmp) {\r
this.previous = this.position;\r
this.position = nextPosition;\r
}\r
this.height = height;\r
this.numPointsWidth = numPointsWidth;\r
this.numPointsHeight = numPointsHeight;\r
- this.windFactor = new THREE.Vector3(5, 2, 2);\r
+ this.windFactor = new THREE.Vector3(3, 2, 2);\r
\r
/**\r
* distance between two vertices horizontally/vertically\r
\r
//this.particles[this.getVertexIndex(0, 0)].movable = false;\r
const n = 3;\r
- for (let i = 0; i <= n; i++)\r
- this.particles[this.getVertexIndex(0, Math.floor((numPointsHeight-1)*(i/n)))].movable = false;\r
+ for (let i = 0; i < numPointsHeight; i++)\r
+ this.particles[this.getVertexIndex(0, i)].movable = false;\r
//this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;\r
//this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;\r
\r
for (let constraint of this.constraints) {\r
constraint.satisfy();\r
}\r
- //console.log(tmpCorrection);\r
+\r
+ this.intersect();\r
+ }\r
+\r
+ intersect() {\r
+ let npw = this.numPointsWidth;\r
+ function getX(i) { return i % npw; }\r
+ function getY(i) { return Math.floor(i / npw); }\r
+\r
+ for (let i in this.particles) {\r
+ for (let j in this.particles) { \r
+ let p1 = this.particles[i];\r
+ let p2 = this.particles[j];\r
+\r
+ p1.movableTmp = true;\r
+ p2.movableTmp = true;\r
+\r
+ if (i == j || (Math.abs(getX(i) - getX(j)) == 1 && Math.abs(getY(i) - getY(j)) == 1))\r
+ continue;\r
+\r
+ let dist = p1.position.distanceTo(p2.position);\r
+ const collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight);\r
+ if (dist < collisionDistance) {\r
+ p1.movableTmp = false;\r
+ p2.movableTmp = false;\r
+ let diff = p1.position.clone().sub(p2.position).normalize();\r
+ diff.multiplyScalar((collisionDistance - dist) * 1.001 / 2);\r
+ if (p1.movable)\r
+ p1.position.add(diff);\r
+ if (p2.movable)\r
+ p2.position.sub(diff);\r
+ }\r
+ }\r
+ }\r
}\r
+\r
/**\r
* helper function to calculate index of vertex\r
* in "vertices" array based on its x and y positions\r
//const loader = new THREE.TextureLoader();\r
//Red color: 0xC70039\r
\r
- const cloth = new Cloth(1, 0.5, 40, 20);\r
+ const cloth = new Cloth(1, 0.5, 20, 20);\r
const clothGeometry = cloth.generateGeometry();\r
//const clothMaterial = new THREE.MeshStandardMaterial({ map: loader.load('Textures/DeutschlandFlagge.jpg'), color: 0xffffff, side: THREE.DoubleSide, flatShading: false});\r
const clothMaterial = new THREE.MeshStandardMaterial({ color: 0xC70039, side: THREE.DoubleSide, flatShading: false });\r
const clothMesh = new THREE.Mesh(clothGeometry, clothMaterial);\r
scene.add(clothMesh);\r
\r
+ document.getElementById("windToggle").onchange = (e) => {\r
+ options.wind = e.target.checked;\r
+ };\r
+\r
let raycaster = new THREE.Raycaster();\r
let intersects;\r
let rightMousePressed;\r