r3-legacy/bak/r3-d3-rigid-body.js

195 lines
6.1 KiB
JavaScript

/**
* RigidBody Superset
* @param engine R3.D3.Engine
* @param mass
* @param friction
* @param position
* @param quaternion
* @param velocity
* @param angularVelocity
* @param linearDamping
* @param angularDamping
* @param allowSleep
* @param sleepSpeedLimit
* @param sleepTimeLimit
* @param collisionFilterGroup
* @param collisionFilterMask
* @param fixedRotation
* @param shape R3.D3.Shape
* @param kinematic Boolean
* @returns {R3.D3.Physics.RigidBody}
* @constructor
*/
R3.D3.RigidBody = function(
engine,
mass,
friction,
position,
quaternion,
velocity,
angularVelocity,
linearDamping,
angularDamping,
allowSleep,
sleepSpeedLimit,
sleepTimeLimit,
collisionFilterGroup,
collisionFilterMask,
fixedRotation,
shape,
kinematic
) {
this.id = R3.Utils.RandomId();
this.position = position || new R3.API.Vector3();
this.velocity = velocity || new R3.API.Vector3();
this.angularVelocity = angularVelocity || new R3.API.Vector3();
this.quaternion = quaternion || new R3.API.Quaternion(0, 0, 0, 1);
this.mass = typeof mass == "undefined" ? 0 : mass;
this.friction = typeof friction == "undefined" ? 5 : friction;
this.linearDamping = typeof linearDamping == "undefined" ? 0.01 : linearDamping;
this.angularDamping = typeof angularDamping == "undefined" ? 0.01 : angularDamping;
this.allowSleep = typeof allowSleep == "undefined" ? true : allowSleep;
this.sleepSpeedLimit = typeof sleepSpeedLimit == "undefined" ? 0.1 : sleepSpeedLimit;
this.sleepTimeLimit = typeof sleepTimeLimit == "undefined" ? 1.0 : sleepTimeLimit;
this.collisionFilterGroup = typeof collisionFilterGroup == "undefined" ? 1 : collisionFilterGroup;
this.collisionFilterMask = typeof collisionFilterMask == "undefined" ? 1 : collisionFilterMask;
this.fixedRotation = typeof fixedRotation == "undefined" ? false : fixedRotation;
this.shape = typeof shape == "undefined" ? null : shape;
this.kinematic = kinematic || false;
this.engine = engine;
this.engine.isNotCannonThrow();
this.instance = this.createInstance();
// Todo: this should be executed somewhere in r3-z, so that we don't execute it on every construction of an object.
R3.Utils.Extend(R3.D3.RigidBody, R3.Component);
};
/**
* private function
* @returns {*}
*/
R3.D3.RigidBody.prototype.createInstance = function() {
var instance = new this.engine.instance.Body({
mass: this.mass,
friction: this.friction,
position: new this.engine.instance.Vec3(
this.position.x,
this.position.y,
this.position.z
),
velocity: new this.engine.instance.Vec3(
this.velocity.x,
this.velocity.y,
this.velocity.z
),
quaternion: new this.engine.instance.Quaternion(
this.quaternion.x,
this.quaternion.y,
this.quaternion.z,
this.quaternion.w
),
angularVelocity: new this.engine.instance.Vec3(
this.angularVelocity.x,
this.angularVelocity.y,
this.angularVelocity.z
),
linearDamping: this.linearDamping,
angularDamping: this.angularDamping,
allowSleep: this.allowSleep,
sleepSpeedLimit: this.sleepSpeedLimit,
sleepTimeLimit: this.sleepTimeLimit,
collisionFilterGroup: this.collisionFilterGroup,
collisionFilterMask: this.collisionFilterMask,
fixedRotation: this.fixedRotation,
shape: this.shape && this.shape.instance ? this.shape.instance : null
});
this.instance = instance;
return instance;
};
R3.D3.RigidBody.prototype.toApiRigidBody = function() {
return null;
};
/**
* Adds a shape to this rigid body
* @param shape R3.D3.Shape
* @param offset R3.API.Vector3
* @param orientation R3.API.Quaternion
* @constructor
*/
R3.D3.RigidBody.prototype.addShape = function(
shape,
offset,
orientation
) {
if (!offset || typeof offset == 'undefined') {
offset = new R3.API.Vector3(0,0,0);
}
if (!orientation || typeof orientation == 'undefined') {
orientation = new R3.API.Quaternion(0,0,0,1);
}
this.instance.addShape(
shape.instance,
new this.engine.instance.Vec3(
offset.x,
offset.y,
offset.z
),
new this.engine.instance.Quaternion(
orientation.x,
orientation.y,
orientation.z,
orientation.w
)
);
};
///////////////////////// Methods to override //////////////////////////
R3.D3.RigidBody.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if(parentEntity) {
if (this.kinematic) {
// reapply the entity's transform back to the rigid body, since it is kinematic
this.instance.position.x = parentEntity.position.x;
this.instance.position.y = parentEntity.position.y;
this.instance.position.z = parentEntity.position.z;
this.instance.quaternion.x = parentEntity.quaternion.x;
this.instance.quaternion.y = parentEntity.quaternion.y;
this.instance.quaternion.z = parentEntity.quaternion.z;
this.instance.quaternion.w = parentEntity.quaternion.w;
this.instance.inertia.x = 0;
this.instance.inertia.y = 0;
this.instance.inertia.z = 0;
this.instance.velocity.x = 0;
this.instance.velocity.y = 0;
this.instance.velocity.z = 0;
} else {
var quaternion = new THREE.Quaternion();
quaternion.copy(this.instance.quaternion);
var position = new THREE.Vector3();
position.copy(this.instance.position);
parentEntity.position.x = position.x;
parentEntity.position.y = position.y;
parentEntity.position.z = position.z;
parentEntity.quaternion.x = quaternion.x;
parentEntity.quaternion.y = quaternion.y;
parentEntity.quaternion.z = quaternion.z;
parentEntity.quaternion.w = quaternion.w;
}
}
};