@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
39 lines (38 loc) • 1.07 kB
JavaScript
"use strict";
import { Vector3 } from "three";
import { vecCopy } from "./SoftBodyMath";
let nextId = 0;
const _p = [0, 0, 0];
export class SoftBodyConstraint {
constructor(_softBody, pointIndex) {
this._softBody = _softBody;
this.pointIndex = pointIndex;
this.id = nextId++;
this.invMass = 0;
this._previousPosition = new Vector3();
this._position = new Vector3();
this._velocity = new Vector3();
if (!this._softBody) {
return;
}
vecCopy(_p, 0, this._softBody.pos, this.pointIndex);
this._position.fromArray(_p);
this._previousPosition.copy(this._position);
}
dispose() {
this._softBody = null;
}
setPosition(position, lerp, dt) {
if (!this._softBody) {
return;
}
this._position.lerp(position, lerp);
this._position.toArray(_p);
vecCopy(this._softBody.pos, this.pointIndex, _p, 0);
this._velocity.copy(position).sub(this._previousPosition).divideScalar(dt);
this._previousPosition.copy(this._position);
}
velocity(target) {
this._velocity.toArray(target);
}
}