1 const DAMPING = 0.03;
\r
2 const DRAG = 1 - DAMPING;
\r
4 const GRAVITY = new THREE.Vector3(0, -9.81 * MASS, 0);
\r
10 constructor(p1, p2, restDist) {
\r
13 this.restDist = restDist;
\r
17 const diff = this.p2.position.clone().sub(this.p1.position);
\r
18 const currentDist = diff.length();
\r
19 if (currentDist == 0) return;
\r
20 if (currentDist <= this.restDist) return;
\r
21 const correction = diff.multiplyScalar(1 - (this.restDist / currentDist));
\r
22 correction.multiplyScalar(K);
\r
23 tmpCorrection = correction;
\r
24 const correctionHalf = correction.multiplyScalar(0.5);
\r
25 this.p1.position.add(correctionHalf);
\r
26 this.p2.position.sub(correctionHalf);
\r
33 constructor(x, y, z, mass) {
\r
34 this.position = new THREE.Vector3(x, y, z);
\r
35 this.previous = new THREE.Vector3(x, y, z);
\r
36 this.acceleration = new THREE.Vector3(0, 0, 0);
\r
40 this.acceleration.add(
\r
41 force.clone().multiplyScalar(1/this.mass)
\r
46 // next position = 2 * current Position - previous position + acceleration * (passed time)^2
\r
47 // acceleration (dv/dt) = F(net)
\r
48 const nextPosition = this.position.clone().sub(this.previous);
\r
49 nextPosition.multiplyScalar(DRAG);
\r
50 nextPosition.add(this.position);
\r
51 nextPosition.add(this.acceleration.multiplyScalar(dt*dt));
\r
53 this.previous = this.position;
\r
54 this.position = nextPosition;
\r
56 this.acceleration.set(0, 0, 0);
\r
61 constructor(width, height, numPointsWidth, numPointsHeight) {
\r
63 this.height = height;
\r
64 this.numPointsWidth = numPointsWidth;
\r
65 this.numPointsHeight = numPointsHeight;
\r
66 this.windFactor = new THREE.Vector3(0.5, 0.2, 0.2);
\r
69 * distance between two vertices horizontally/vertically
\r
70 * divide by the number of points minus one
\r
71 * because there are (n - 1) lines between n vertices
\r
73 let stepWidth = width / (numPointsWidth - 1);
\r
74 let stepHeight = height / (numPointsHeight - 1);
\r
77 * iterate over the number of vertices in x/y axis
\r
78 * and add a new Particle to "particles"
\r
80 this.particles = [];
\r
81 for (let y = 0; y < numPointsHeight; y++) {
\r
82 for (let x = 0; x < numPointsWidth; x++) {
\r
83 this.particles.push(
\r
85 (x - ((numPointsWidth-1)/2)) * stepWidth,
\r
86 height - (y + ((numPointsHeight-1)/2)) * stepHeight,
\r
93 this.particles[this.getVertexIndex(0, 0)].movable = false;
\r
94 this.particles[this.getVertexIndex(0, numPointsHeight-1)].movable = false;
\r
95 this.particles[this.getVertexIndex(numPointsWidth-1, 0)].movable = false;
\r
97 const REST_DIST_X = width / (numPointsWidth-1);
\r
98 const REST_DIST_Y = height / (numPointsHeight-1);
\r
101 * generate constraints (springs)
\r
103 this.constraints = [];
\r
104 for (let y = 0; y < numPointsHeight; y++) {
\r
105 for (let x = 0; x < numPointsWidth; x++) {
\r
106 if (x < numPointsWidth-1) {
\r
107 this.constraints.push(new Constraint(
\r
108 this.particles[this.getVertexIndex(x, y)],
\r
109 this.particles[this.getVertexIndex(x+1, y)],
\r
113 if (y < numPointsHeight-1) {
\r
114 this.constraints.push(new Constraint(
\r
115 this.particles[this.getVertexIndex(x, y)],
\r
116 this.particles[this.getVertexIndex(x, y+1)],
\r
123 generateGeometry() {
\r
124 const geometry = new THREE.BufferGeometry();
\r
126 const vertices = [];
\r
127 const normals = [];
\r
128 const indices = [];
\r
130 for (let particle of this.particles) {
\r
132 particle.position.x,
\r
133 particle.position.y,
\r
134 particle.position.z);
\r
137 const numPointsWidth = this.numPointsWidth;
\r
138 const numPointsHeight = this.numPointsHeight;
\r
141 * generate faces based on 4 vertices
\r
142 * and 6 springs each
\r
144 for (let y = 0; y < numPointsHeight - 1; y++) {
\r
145 for (let x = 0; x < numPointsWidth - 1; x++) {
\r
147 this.getVertexIndex(x, y),
\r
148 this.getVertexIndex(x+1, y),
\r
149 this.getVertexIndex(x+1, y+1)
\r
152 this.getVertexIndex(x, y),
\r
153 this.getVertexIndex(x+1, y+1),
\r
154 this.getVertexIndex(x, y+1)
\r
159 geometry.setIndex(indices);
\r
160 geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
\r
161 //geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
\r
162 geometry.computeBoundingSphere();
\r
163 geometry.computeVertexNormals();
\r
167 updateGeometry(geometry) {
\r
168 const positions = geometry.attributes.position.array;
\r
169 for (let i in this.particles) {
\r
170 let p = this.particles[i];
\r
172 positions[i*3+0] = p.position.x;
\r
173 positions[i*3+1] = p.position.y;
\r
174 positions[i*3+2] = p.position.z;
\r
176 p.position = p.previous;
\r
179 geometry.attributes.position.needsUpdate = true;
\r
180 geometry.computeBoundingSphere();
\r
181 geometry.computeVertexNormals();
\r
184 let now = performance.now();
\r
185 for (let particle of this.particles) {
\r
186 let vertex = particle.position;
\r
187 let fWind = new THREE.Vector3(
\r
188 this.windFactor.x * (Math.sin(vertex.x * vertex.y * now)+1),
\r
189 this.windFactor.y * Math.cos(vertex.z * now),
\r
190 this.windFactor.z * Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))
\r
192 // normalize then multiply?
\r
193 particle.addForce(fWind);
\r
194 // calculate wind with normal?
\r
196 particle.addForce(GRAVITY);
\r
198 particle.verlet(dt);
\r
202 for (let constraint of this.constraints) {
\r
203 constraint.satisfy();
\r
205 //console.log(tmpCorrection);
\r
208 * helper function to calculate index of vertex
\r
209 * in "vertices" array based on its x and y positions
\r
211 * @param {number} x - x index of vertex
\r
212 * @param {number} y - y index of vertex
\r
214 getVertexIndex(x, y) {
\r
215 return y * this.numPointsWidth + x;
\r