From 999a549825d3de0b53e2922462ed1e120e176ec2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Patrick=20Sch=C3=B6nberger?= Date: Sat, 6 Feb 2021 12:48:25 +0100 Subject: [PATCH] simulate --- Scripts/cloth.js | 90 +++++++++++++++++++++++++++++---- Scripts/main.js | 4 +- Textures/DeutschlandFlagge.jpg | Bin 0 -> 4333 bytes 3 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 Textures/DeutschlandFlagge.jpg diff --git a/Scripts/cloth.js b/Scripts/cloth.js index db07ac3..0c4f02c 100644 --- a/Scripts/cloth.js +++ b/Scripts/cloth.js @@ -1,14 +1,35 @@ +const DAMPING = 0.03; +const DRAG = 1 - DAMPING; const MASS = 0.1; +const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0); +const K = 1; + +let tmpCorrection; class Constraint { - constructor(p1, p2, distance) { + constructor(p1, p2, restDist) { this.p1 = p1; this.p2 = p2; - this.distance = distance; + this.restDist = restDist; + } + + satisfy() { + const diff = this.p2.position.clone().sub(this.p1.position); + const currentDist = diff.length(); + if (currentDist == 0) return; + if (currentDist <= this.restDist) return; + const correction = diff.multiplyScalar(1 - (this.restDist / currentDist)); + correction.multiplyScalar(K); + tmpCorrection = correction; + const correctionHalf = correction.multiplyScalar(0.5); + this.p1.position.add(correctionHalf); + this.p2.position.sub(correctionHalf); } } class Particle { + movable = true; + constructor(x, y, z, mass) { this.position = new THREE.Vector3(x, y, z); this.previous = new THREE.Vector3(x, y, z); @@ -16,10 +37,23 @@ class Particle { this.mass = mass; } addForce(force) { - + this.acceleration.add( + force.clone().multiplyScalar(1/this.mass) + ); } verlet(dt) { - + // verlet algorithm + // next position = 2 * current Position - previous position + acceleration * (passed time)^2 + // acceleration (dv/dt) = F(net) + const nextPosition = this.position.clone().sub(this.previous); + nextPosition.multiplyScalar(DRAG); + nextPosition.add(this.position); + nextPosition.add(this.acceleration.multiplyScalar(dt*dt)); + + this.previous = this.position; + this.position = nextPosition; + + this.acceleration.set(0, 0, 0); } } @@ -29,6 +63,7 @@ class Cloth { this.height = height; this.numPointsWidth = numPointsWidth; this.numPointsHeight = numPointsHeight; + this.windFactor = new THREE.Vector3(0.5, 0.2, 0.2); /** * distance between two vertices horizontally/vertically @@ -55,6 +90,10 @@ class Cloth { } } + this.particles[this.getVertexIndex(0, 0)].movable = false; + this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false; + this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false; + const REST_DIST_X = width / (numPointsWidth-1); const REST_DIST_Y = height / (numPointsHeight-1); @@ -71,7 +110,7 @@ class Cloth { REST_DIST_X )); } - if (x < numPointsWidth-1) { + if (y < numPointsHeight-1) { this.constraints.push(new Constraint( this.particles[this.getVertexIndex(x, y)], this.particles[this.getVertexIndex(x, y+1)], @@ -126,13 +165,44 @@ class Cloth { return geometry; } updateGeometry(geometry) { - - } - satisfyConstraints() { - + const positions = geometry.attributes.position.array; + for (let i in this.particles) { + let p = this.particles[i]; + if (p.movable) { + positions[i*3+0] = p.position.x; + positions[i*3+1] = p.position.y; + positions[i*3+2] = p.position.z; + } else { + p.position = p.previous; + } + } + geometry.attributes.position.needsUpdate = true; + geometry.computeBoundingSphere(); + geometry.computeVertexNormals(); } - simulate() { + simulate(dt) { + let now = performance.now(); + for (let particle of this.particles) { + let vertex = particle.position; + let fWind = new THREE.Vector3( + this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1), + this.windFactor.y * Math.cos(vertex.z * now), + this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z)) + ); + // normalize then multiply? + particle.addForce(fWind); + // calculate wind with normal? + + particle.addForce(GRAVITY); + + particle.verlet(dt); + } + + for (let constraint of this.constraints) { + constraint.satisfy(); + } + //console.log(tmpCorrection); } /** * helper function to calculate index of vertex diff --git a/Scripts/main.js b/Scripts/main.js index d54aa9f..3353f4d 100644 --- a/Scripts/main.js +++ b/Scripts/main.js @@ -98,7 +98,9 @@ function init() { * @param {number} dt - time passed since last frame in ms */ function animate(dt) { - // simulate cloth + cloth.simulate(dt / 1000); + + cloth.updateGeometry(clothGeometry); raycaster.setFromCamera( new THREE.Vector2((mousePos.x / w) * 2 - 1, ((h - mousePos.y) / h) * 2 - 1), camera ); diff --git a/Textures/DeutschlandFlagge.jpg b/Textures/DeutschlandFlagge.jpg new file mode 100644 index 0000000000000000000000000000000000000000..09e63ab5cf508e0b2e83660f793c434a06642fe3 GIT binary patch literal 4333 zcmex=xfQheJt1R)k+xfR~3~On_TN zR9Q^o{~-oJ4u;>1E14OU7?=bZnFSgDA7PMYV1xp&Q$YkfD=J%Ac3t% zZ5lu{1Jh#v=jVklfZ42*&YqvAz5vYTR-HZH#@_+V7V|c@tC$B-uQM&L=A$iGm?2W< z&d*0x5ca0Xou7|<0kauXHWxqdd;w-NolHYE{)B%U14BXS@fcYKhS1kGJrx_ks#xu2 zpE%wPW-*9?K0MiWi|Bo<8F)}hRGeQC$BFezT%Fe>f z296IV0R{$!$DrVv#lXNI?7+all)=Ej;LE_k!1?2P0|P_Ep$!ZSOxH{s7#PGhu3=zc z*xH-Gz`(jzt%iYtamm$`0}Kq(>S{g=42+wKkU3Q#C)Ac>cK|ahI|~!U0n8u=OuoRt zz>?{}z@X&Cz`!UJz`(#UseysPwf6e~1_sHqee)O?7$=uzK44&wG!H}O*k5B{V5rIO zUckV>C8^iIz@U+`i-Cb*;iMM~3{36k0~i?C`TrkbP!?cdWM*MvVg@Haa40Y`i?FZ? z3JU}nnlKqV3V{QIk%5te>8ECFaL`@%Pj_xT$}Ke8F+0Y0o!WKb?9Q!6XeN+d`@KBx z(d&2L%JTI;mwvCdHlO};*YCY^XP^FaxAxw-vrqr!?R%e|_US)^d3^ci&6V}D*YCc0 zbI<;>*YCc0b1(iGRAXZBJ!`r+&GxepPF2i1w?6@uBS5QR?5Oi^W71 zrXD4VGLO1yvNrDZtL#PYyME1jG4b8LWxG_W*RNdHX&I^K-e0)q%3)K3vp&YbJR~xD zYx9q$Ub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