1 const DAMPING = 0.03;
\r
2 const DRAG = 1 - DAMPING;
\r
4 const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);
\r
14 constructor(p1, p2, restDist) {
\r
17 this.restDist = restDist;
\r
21 const diff = this.p2.position.clone().sub(this.p1.position);
\r
22 const currentDist = diff.length();
\r
23 if (currentDist == 0) return;
\r
24 if (currentDist <= this.restDist) return;
\r
25 //const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));
\r
26 const correction = diff.multiplyScalar((currentDist - this.restDist) / currentDist);
\r
27 correction.multiplyScalar(K);
\r
28 correction.clampLength(0, 1);
\r
29 const correctionHalf = correction.multiplyScalar(0.5);
\r
31 let p1movable = this.p1.movable && this.p1.movableTmp;
\r
32 let p2movable = this.p2.movable && this.p2.movableTmp;
\r
34 if (p1movable && p2movable) {
\r
35 this.p1.position.add(correctionHalf);
\r
36 this.p2.position.sub(correctionHalf);
\r
37 } else if (! p1movable && p2movable) {
\r
38 this.p2.position.sub(correction);
\r
39 } else if (p1movable && ! p2movable) {
\r
40 this.p1.position.add(correction);
\r
49 constructor(x, y, z, mass) {
\r
50 this.position = new THREE.Vector3(x, y, z);
\r
51 this.previous = new THREE.Vector3(x, y, z);
\r
52 this.acceleration = new THREE.Vector3(0, 0, 0);
\r
56 this.acceleration.add(
\r
57 force.clone().multiplyScalar(1/this.mass)
\r
62 // next position = 2 * current Position - previous position + acceleration * (passed time)^2
\r
63 // acceleration (dv/dt) = F(net)
\r
64 const nextPosition = this.position.clone().sub(this.previous);
\r
65 nextPosition.multiplyScalar(DRAG);
\r
66 nextPosition.add(this.position);
\r
67 nextPosition.add(this.acceleration.multiplyScalar(dt*dt));
\r
69 if (this.movable && this.movableTmp) {
\r
70 this.previous = this.position;
\r
71 this.position = nextPosition;
\r
74 this.acceleration.set(0, 0, 0);
\r
79 constructor(width, height, numPointsWidth, numPointsHeight) {
\r
81 this.height = height;
\r
82 this.numPointsWidth = numPointsWidth;
\r
83 this.numPointsHeight = numPointsHeight;
\r
84 this.windFactor = new THREE.Vector3(3, 2, 2);
\r
87 * distance between two vertices horizontally/vertically
\r
88 * divide by the number of points minus one
\r
89 * because there are (n - 1) lines between n vertices
\r
91 let stepWidth = width / (numPointsWidth - 1);
\r
92 let stepHeight = height / (numPointsHeight - 1);
\r
95 * iterate over the number of vertices in x/y axis
\r
96 * and add a new Particle to "particles"
\r
98 this.particles = [];
\r
99 for (let y = 0; y < numPointsHeight; y++) {
\r
100 for (let x = 0; x < numPointsWidth; x++) {
\r
101 this.particles.push(
\r
103 (x - ((numPointsWidth-1)/2)) * stepWidth,
\r
104 height - (y + ((numPointsHeight-1)/2)) * stepHeight,
\r
111 //this.particles[this.getVertexIndex(0, 0)].movable = false;
\r
113 for (let i = 0; i < numPointsHeight; i++)
\r
114 this.particles[this.getVertexIndex(0, i)].movable = false;
\r
115 //this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;
\r
116 //this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;
\r
118 const REST_DIST_X = width / (numPointsWidth-1);
\r
119 const REST_DIST_Y = height / (numPointsHeight-1);
\r
122 * generate constraints (springs)
\r
124 this.constraints = [];
\r
125 for (let y = 0; y < numPointsHeight; y++) {
\r
126 for (let x = 0; x < numPointsWidth; x++) {
\r
127 if (x < numPointsWidth-1) {
\r
128 this.constraints.push(new Constraint(
\r
129 this.particles[this.getVertexIndex(x, y)],
\r
130 this.particles[this.getVertexIndex(x+1, y)],
\r
134 if (y < numPointsHeight-1) {
\r
135 this.constraints.push(new Constraint(
\r
136 this.particles[this.getVertexIndex(x, y)],
\r
137 this.particles[this.getVertexIndex(x, y+1)],
\r
144 generateGeometry() {
\r
145 const geometry = new THREE.BufferGeometry();
\r
147 const vertices = [];
\r
148 const indices = [];
\r
151 for (let i in this.particles) {
\r
152 let particle = this.particles[i];
\r
154 particle.position.x,
\r
155 particle.position.y,
\r
156 particle.position.z);
\r
158 this.getX(i) / (this.numPointsWidth-1),
\r
159 1 - (this.getY(i) / (this.numPointsHeight-1))
\r
164 * generate faces based on 4 vertices
\r
165 * and 6 springs each
\r
167 for (let y = 0; y < this.numPointsHeight - 1; y++) {
\r
168 for (let x = 0; x < this.numPointsWidth - 1; x++) {
\r
170 this.getVertexIndex(x, y),
\r
171 this.getVertexIndex(x+1, y),
\r
172 this.getVertexIndex(x+1, y+1)
\r
175 this.getVertexIndex(x, y),
\r
176 this.getVertexIndex(x+1, y+1),
\r
177 this.getVertexIndex(x, y+1)
\r
182 geometry.setIndex(indices);
\r
183 geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
\r
184 geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
\r
185 geometry.computeBoundingSphere();
\r
186 geometry.computeVertexNormals();
\r
190 updateGeometry(geometry) {
\r
191 const positions = geometry.attributes.position.array;
\r
192 for (let i in this.particles) {
\r
193 let p = this.particles[i];
\r
194 positions[i*3+0] = p.position.x;
\r
195 positions[i*3+1] = p.position.y;
\r
196 positions[i*3+2] = p.position.z;
\r
198 geometry.attributes.position.needsUpdate = true;
\r
199 geometry.computeBoundingSphere();
\r
200 geometry.computeVertexNormals();
\r
203 let now = performance.now();
\r
204 for (let particle of this.particles) {
\r
205 let vertex = particle.position;
\r
206 let fWind = new THREE.Vector3(
\r
207 this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1),
\r
208 this.windFactor.y * Math.cos(vertex.z * now),
\r
209 this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))
\r
211 // normalize then multiply?
\r
213 particle.addForce(fWind);
\r
214 // calculate wind with normal?
\r
216 particle.addForce(GRAVITY);
\r
218 particle.verlet(dt);
\r
222 for (let constraint of this.constraints) {
\r
223 constraint.satisfy();
\r
230 for (let i in this.particles) {
\r
231 for (let j in this.particles) {
\r
232 let p1 = this.particles[i];
\r
233 let p2 = this.particles[j];
\r
235 p1.movableTmp = true;
\r
236 p2.movableTmp = true;
\r
238 if (i == j || (Math.abs(this.getX(i) - this.getX(j)) == 1 && Math.abs(this.getY(i) - this.getY(j)) == 1))
\r
241 let dist = p1.position.distanceTo(p2.position);
\r
242 const collisionDistance = Math.min(this.width / this.numPointsWidth, this.height / this.numPointsHeight);
\r
243 if (dist < collisionDistance) {
\r
244 // p1.movableTmp = false;
\r
245 // p2.movableTmp = false;
\r
246 let diff = p1.position.clone().sub(p2.position).normalize();
\r
247 diff.multiplyScalar((collisionDistance - dist) * 1.001 / 2);
\r
249 p1.position.add(diff);
\r
251 p2.position.sub(diff);
\r
258 * helper function to calculate index of vertex
\r
259 * in "vertices" array based on its x and y positions
\r
261 * @param {number} x - x index of vertex
\r
262 * @param {number} y - y index of vertex
\r
264 getVertexIndex(x, y) {
\r
265 return y * this.numPointsWidth + x;
\r
267 getX(i) { return i % this.numPointsWidth; }
\r
268 getY(i) { return Math.floor(i / this.numPointsWidth); }
\r