From: Patrick Schönberger Date: Sat, 6 Feb 2021 10:38:43 +0000 (+0100) Subject: add Constraint class, add constraints to cloth X-Git-Url: https://gitweb.ps.run/cloth_sim/commitdiff_plain/19d9f67e228e9925958489574339448bb608c9db add Constraint class, add constraints to cloth --- diff --git a/Scripts/cloth.js b/Scripts/cloth.js index 05ff6f1..db07ac3 100644 --- a/Scripts/cloth.js +++ b/Scripts/cloth.js @@ -1,9 +1,13 @@ -// cloth rendering -// simulate -// setup scene - const MASS = 0.1; +class Constraint { + constructor(p1, p2, distance) { + this.p1 = p1; + this.p2 = p2; + this.distance = distance; + } +} + class Particle { constructor(x, y, z, mass) { this.position = new THREE.Vector3(x, y, z); @@ -50,6 +54,32 @@ class Cloth { ); } } + + const REST_DIST_X = width / (numPointsWidth-1); + const REST_DIST_Y = height / (numPointsHeight-1); + + /** + * generate constraints (springs) + */ + this.constraints = []; + for (let y = 0; y < numPointsHeight; y++) { + for (let x = 0; x < numPointsWidth; x++) { + if (x < numPointsWidth-1) { + this.constraints.push(new Constraint( + this.particles[this.getVertexIndex(x, y)], + this.particles[this.getVertexIndex(x+1, y)], + REST_DIST_X + )); + } + if (x < numPointsWidth-1) { + this.constraints.push(new Constraint( + this.particles[this.getVertexIndex(x, y)], + this.particles[this.getVertexIndex(x, y+1)], + REST_DIST_Y + )); + } + } + } } generateGeometry() { const geometry = new THREE.BufferGeometry(); @@ -68,17 +98,6 @@ class Cloth { const numPointsWidth = this.numPointsWidth; const numPointsHeight = this.numPointsHeight; - /** - * helper function to calculate index of vertex - * in "vertices" array based on its x and y positions - * in the mesh - * @param {number} x - x index of vertex - * @param {number} y - y index of vertex - */ - function getVertexIndex(x, y) { - return y * numPointsWidth + x; - } - /** * generate faces based on 4 vertices * and 6 springs each @@ -86,14 +105,14 @@ class Cloth { for (let y = 0; y < numPointsHeight - 1; y++) { for (let x = 0; x < numPointsWidth - 1; x++) { indices.push( - getVertexIndex(x, y), - getVertexIndex(x+1, y), - getVertexIndex(x+1, y+1) + this.getVertexIndex(x, y), + this.getVertexIndex(x+1, y), + this.getVertexIndex(x+1, y+1) ); indices.push( - getVertexIndex(x, y), - getVertexIndex(x+1, y+1), - getVertexIndex(x, y+1) + this.getVertexIndex(x, y), + this.getVertexIndex(x+1, y+1), + this.getVertexIndex(x, y+1) ); } } @@ -109,4 +128,20 @@ class Cloth { updateGeometry(geometry) { } + satisfyConstraints() { + + } + simulate() { + + } + /** + * helper function to calculate index of vertex + * in "vertices" array based on its x and y positions + * in the mesh + * @param {number} x - x index of vertex + * @param {number} y - y index of vertex + */ + getVertexIndex(x, y) { + return y * this.numPointsWidth + x; + } } \ No newline at end of file