r3-legacy/bak/r3-d3-raycast-vehicle.js

89 lines
2.0 KiB
JavaScript

/**
* Raycast Vehicles :)
* @param engine R3.D3.Engine
* @param chassisBody R3.D3.RigidBody
* @param wheels R3.D3.RaycastWheel[]
* @constructor
*/
R3.D3.RaycastVehicle = function(
engine,
chassisBody,
wheels,
wheelBodies
) {
this.engine = engine;
this.engine.isNotCannonThrow();
this.id = R3.Utils.RandomId();
this.chassisBody = chassisBody;
if (typeof wheels == 'undefined') {
wheels = [];
}
this.wheels = wheels;
if(R3.Utils.UndefinedOrNull(wheelBodies)) {
wheelBodies = [];
}
this.wheelBodies = wheelBodies;
this.instance = this.createInstance();
R3.Utils.Extend(R3.D3.RaycastVehicle, R3.Component);
};
/**
* private
* @returns {R3.D3.RaycastVehicle|R3.D3.Physics.RaycastVehicle|*}
*/
R3.D3.RaycastVehicle.prototype.createInstance = function() {
return new this.engine.instance.RaycastVehicle({
chassisBody: this.chassisBody.instance
});
};
/**
* Adds a raycast wheel to this vehicle
* @param wheel R3.D3.RaycastWheel
* @param wheelRigidBody R3.D3.RigidBody
*/
R3.D3.RaycastVehicle.prototype.addWheel = function (
wheel,
wheelRigidBody
) {
this.wheels.push(wheel);
this.wheelBodies.push(wheelRigidBody);
wheel.wheelIndex = this.instance.addWheel(wheel.instance);
};
/**
* Returns updated wheel info
* @returns {*}
* @constructor
*/
R3.D3.RaycastVehicle.prototype.getWheelInfo = function() {
return this.instance.wheelInfos;
};
// Override component methods //
R3.D3.RaycastVehicle.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
for (var i = 0; i < this.getWheelInfo().length; i++) {
this.instance.updateWheelTransform(i);
var t = this.getWheelInfo()[i].worldTransform;
var wheelBody = this.wheelBodies[i].instance;
wheelBody.position.copy(t.position);
wheelBody.quaternion.copy(t.quaternion);
}
};
R3.D3.RaycastVehicle.prototype.onRegistered = function(
parentScene
) {
};