diff options
| -rw-r--r-- | Scripts/cloth.js | 63 | ||||
| -rw-r--r-- | Scripts/main.js | 12 |
2 files changed, 41 insertions, 34 deletions
diff --git a/Scripts/cloth.js b/Scripts/cloth.js index 600ac8f..1497583 100644 --- a/Scripts/cloth.js +++ b/Scripts/cloth.js @@ -6,9 +6,9 @@ */
function vectorLength(a, b) {
let v1 = new THREE.Vector3();
- v1.set(a.x, a.y, a.z);
+ v1.copy(a);
let v2 = new THREE.Vector3();
- v2.set(b.x, b.y, b.z);
+ v2.copy(b);
return v1.sub(v2).length();
}
@@ -65,11 +65,8 @@ export class Spring { }
getDirection(vertices) {
- let direction = new THREE.Vector3(
- vertices[this.index1].x,
- vertices[this.index1].y,
- vertices[this.index1].z
- );
+ let direction = new THREE.Vector3();
+ direction.copy(vertices[this.index1]);
direction.sub(vertices[this.index2]);
direction.divideScalar(vectorLength(vertices[this.index1], vertices[this.index2]));
@@ -197,8 +194,10 @@ export class Cloth { * Copy vertices and initialize vertex weights to 0
*/
for (let i in vertices) {
- this.geometry.vertices.push(vertices[i]);
- this.previousPositions.push(vertices[i]);
+ this.geometry.vertices.push(vertices[i].clone());
+ this.previousPositions.push(vertices[i].clone());
+ // this.geometry.vertices.push(vertices[i]);
+ // this.previousPositions.push(vertices[i]);
this.vertexWeights.push(0);
this.vertexRigidness.push(false);
}
@@ -315,24 +314,25 @@ export class Cloth { time = 0;
/**
*
- * @param {number} dt
+ * @param {number} dt time in seconds since last frame
*/
simulate(dt) {
for (let i in this.geometry.vertices) {
- let currentPosition;
let acceleration = this.getAcceleration(i, dt);
- // TODO: decide on clamping
- acceleration.clampLength(0, 100);
+ //acceleration.clampLength(0, 10);
+
+ if (Math.abs(acceleration.length()) <= 10e-4) {
+ acceleration.set(0, 0, 0);
+ }
- currentPosition = this.verlet(this.geometry.vertices[i], this.previousPositions[i], acceleration, dt/500);
- //currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt/10);
+ let currentPosition = this.verlet(this.geometry.vertices[i].clone(), this.previousPositions[i].clone(), acceleration, dt);
+ //let currentPosition = this.euler(this.geometry.vertices[i], acceleration, dt);
- this.previousPositions[i] = currentPosition;
- this.geometry.vertices[i] = currentPosition;
+ this.previousPositions[i].copy(this.geometry.vertices[i]);
+ this.geometry.vertices[i].copy(currentPosition);
}
-
- //this.getAcceleration(1, dt, true);
+ //console.log(this.getAcceleration(1, dt));
this.time += dt;
@@ -371,32 +371,31 @@ getAcceleration(vertexIndex, dt) { // constant gravity
let g = new THREE.Vector3(0, -9.8, 0);
// stiffness
- let k = 300;
+ let k = 1000;
// Wind vector
- // TODO: include wind vector
let fWind = new THREE.Vector3(
Math.sin(vertex.x * vertex.y * this.time),
- Math.cos(vertex.z* this.time),
+ Math.cos(vertex.z * this.time),
Math.sin(Math.cos(5 * vertex.x * vertex.y * vertex.z))
);
- fWind = new THREE.Vector3(0, 0, 0);
+ fWind.set(0, 0, 0);
/**
* constant determined by the properties of the surrounding fluids (air)
* achievement of cloth effects through try out
* */
- let a = 1;
+ let a = 0.01;
let velocity = new THREE.Vector3(
- (vertex.x - this.previousPositions[vertexIndex].x) * dt,
- (vertex.y - this.previousPositions[vertexIndex].y) * dt,
- (vertex.z - this.previousPositions[vertexIndex].z) * dt
+ (this.previousPositions[vertexIndex].x - vertex.x) / dt,
+ (this.previousPositions[vertexIndex].y - vertex.y) / dt,
+ (this.previousPositions[vertexIndex].z - vertex.z) / dt
);
- // TODO: include air resistance
- let fAirResistance = velocity.multiply(velocity).multiplyScalar(-a);
- fAirResistance = new THREE.Vector3(0, 0, 0);
+ //console.log(velocity, vertex, this.previousPositions[vertexIndex]);
+
+ let fAirResistance = velocity.cross(velocity).multiplyScalar(-a);
let springSum = new THREE.Vector3(0, 0, 0);
@@ -411,10 +410,10 @@ getAcceleration(vertexIndex, dt) { let springDirection = spring.getDirection(this.geometry.vertices);
- if (this.faces[i].springs[j].index2 == vertexIndex)
+ if (this.faces[i].springs[j].index1 == vertexIndex)
springDirection.multiplyScalar(-1);
- springSum.add(springDirection.multiplyScalar(k * (spring.currentLength - spring.restLength)));
+ springSum.add(springDirection.multiplyScalar(k * (spring.restLength - spring.currentLength)));
}
}
}
diff --git a/Scripts/main.js b/Scripts/main.js index ececd6a..8c61f82 100644 --- a/Scripts/main.js +++ b/Scripts/main.js @@ -1,6 +1,14 @@ import { Face, Spring, Cloth } from './cloth.js';
+/**
+ * rendering
+ * Einheiten konsistent
+ * Wind
+ * evtl. an Stoff ziehen
+ */
+
+
class Point {
constructor(x, y) {
this.x = x;
@@ -87,10 +95,10 @@ function init() { /**
* function called every frame
- * @param {number} dt - time passed since last frame
+ * @param {number} dt - time passed since last frame in ms
*/
function animate(dt) {
- cloth.simulate(dt);
+ cloth.simulate(dt/1000);
setTimeout(() => {
animate(frameTime);
|
