@awayjs/scene
Version:
AwayJS scene classes
60 lines (59 loc) • 2.86 kB
JavaScript
import { __extends } from "tslib";
import { Vector3D } from '@awayjs/core';
import { LookAtController } from '../controllers/LookAtController';
/**
* Uses spring physics to animate the target object towards a position that is
* defined as the lookAtTarget object's position plus the vector defined by the
* positionOffset property.
*/
var SpringController = /** @class */ (function (_super) {
__extends(SpringController, _super);
function SpringController(targetObject, lookAtObject, stiffness, mass, damping) {
if (targetObject === void 0) { targetObject = null; }
if (lookAtObject === void 0) { lookAtObject = null; }
if (stiffness === void 0) { stiffness = 1; }
if (mass === void 0) { mass = 40; }
if (damping === void 0) { damping = 4; }
var _this = _super.call(this, targetObject, lookAtObject) || this;
/**
* Offset of spring center from target in target object space,
* ie: Where the camera should ideally be in the target object space.
*/
_this.positionOffset = new Vector3D(0, 500, -1000);
_this.stiffness = stiffness;
_this.damping = damping;
_this.mass = mass;
_this._velocity = new Vector3D();
_this._dv = new Vector3D();
_this._stretch = new Vector3D();
_this._force = new Vector3D();
_this._acceleration = new Vector3D();
_this._desiredPosition = new Vector3D();
return _this;
}
SpringController.prototype.update = function (interpolate) {
if (interpolate === void 0) { interpolate = true; }
if (!this._pLookAtObject || !this._pTargetObject)
return;
this._pControllerDirty = true;
var offs = this._pLookAtObject.transform.matrix3D.deltaTransformVector(this.positionOffset);
this._desiredPosition.x = this._pLookAtObject.x + offs.x;
this._desiredPosition.y = this._pLookAtObject.y + offs.y;
this._desiredPosition.z = this._pLookAtObject.z + offs.z;
this._stretch = this._pTargetObject.transform.position.add(this._desiredPosition);
this._stretch.scaleBy(-this.stiffness);
this._dv.copyFrom(this._velocity);
this._dv.scaleBy(this.damping);
this._force.x = this._stretch.x - this._dv.x;
this._force.y = this._stretch.y - this._dv.y;
this._force.z = this._stretch.z - this._dv.z;
this._acceleration.copyFrom(this._force);
this._acceleration.scaleBy(1 / this.mass);
this._velocity.incrementBy(this._acceleration);
var position = this._pTargetObject.transform.position.add(this._velocity);
this._pTargetObject.transform.moveTo(position.x, position.y, position.z);
_super.prototype.update.call(this);
};
return SpringController;
}(LookAtController));
export { SpringController };