|
@@ -14,7 +14,6 @@ declare var CANNON: any;
|
|
|
|
|
|
/** @hidden */
|
|
|
export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
-
|
|
|
public world: any;
|
|
|
public name: string = "CannonJSPlugin";
|
|
|
private _physicsMaterials = new Array();
|
|
@@ -108,7 +107,6 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
|
|
|
//should a new body be created for this impostor?
|
|
|
if (impostor.isBodyInitRequired()) {
|
|
|
-
|
|
|
var shape = this._createShape(impostor);
|
|
|
|
|
|
//unregister events, if body is being changed
|
|
@@ -122,7 +120,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
|
|
|
var bodyCreationObject = {
|
|
|
mass: impostor.getParam("mass"),
|
|
|
- material: material
|
|
|
+ material: material,
|
|
|
};
|
|
|
// A simple extend, in case native options were used.
|
|
|
var nativeOptions = impostor.getParam("nativeOptions");
|
|
@@ -141,7 +139,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
//try to keep the body moving in the right direction by taking old properties.
|
|
|
//Should be tested!
|
|
|
if (oldBody) {
|
|
|
- ['force', 'torque', 'velocity', 'angularVelocity'].forEach(function(param) {
|
|
|
+ ["force", "torque", "velocity", "angularVelocity"].forEach(function (param) {
|
|
|
const vec = oldBody[param];
|
|
|
impostor.physicsBody[param].set(vec.x, vec.y, vec.z);
|
|
|
});
|
|
@@ -158,7 +156,6 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
let currentRotation: Nullable<Quaternion> = mainImpostor.object.rotationQuaternion;
|
|
|
if (meshChildren.length) {
|
|
|
var processMesh = (localPosition: Vector3, mesh: AbstractMesh) => {
|
|
|
-
|
|
|
if (!currentRotation || !mesh.rotationQuaternion) {
|
|
|
return;
|
|
|
}
|
|
@@ -168,20 +165,22 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
var parent = childImpostor.parent;
|
|
|
if (parent !== mainImpostor) {
|
|
|
var pPosition = mesh.position.clone();
|
|
|
- let localRotation = mesh.rotationQuaternion.multiply(Quaternion.Inverse(currentRotation));
|
|
|
+ // let localRotation = mesh.rotationQuaternion.multiply(Quaternion.Inverse(currentRotation));
|
|
|
if (childImpostor.physicsBody) {
|
|
|
this.removePhysicsBody(childImpostor);
|
|
|
childImpostor.physicsBody = null;
|
|
|
}
|
|
|
childImpostor.parent = mainImpostor;
|
|
|
childImpostor.resetUpdateFlags();
|
|
|
- mainImpostor.physicsBody.addShape(this._createShape(childImpostor), new this.BJSCANNON.Vec3(pPosition.x, pPosition.y, pPosition.z), new this.BJSCANNON.Quaternion(localRotation.x, localRotation.y, localRotation.z, localRotation.w));
|
|
|
+ mainImpostor.physicsBody.addShape(this._createShape(childImpostor), new this.BJSCANNON.Vec3(pPosition.x, pPosition.y, pPosition.z) /*, new this.BJSCANNON.Quaternion(localRotation.x, localRotation.y, localRotation.z, localRotation.w)*/);
|
|
|
//Add the mass of the children.
|
|
|
mainImpostor.physicsBody.mass += childImpostor.getParam("mass");
|
|
|
}
|
|
|
}
|
|
|
currentRotation.multiplyInPlace(mesh.rotationQuaternion);
|
|
|
- mesh.getChildMeshes(true).filter((m) => !!m.physicsImpostor).forEach(processMesh.bind(this, mesh.getAbsolutePosition()));
|
|
|
+ mesh.getChildMeshes(true)
|
|
|
+ .filter((m) => !!m.physicsImpostor)
|
|
|
+ .forEach(processMesh.bind(this, mesh.getAbsolutePosition()));
|
|
|
};
|
|
|
meshChildren.filter((m) => !!m.physicsImpostor).forEach(processMesh.bind(this, mainImpostor.object.getAbsolutePosition()));
|
|
|
}
|
|
@@ -213,7 +212,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
axisA: jointData.mainAxis ? new this.BJSCANNON.Vec3().set(jointData.mainAxis.x, jointData.mainAxis.y, jointData.mainAxis.z) : null,
|
|
|
axisB: jointData.connectedAxis ? new this.BJSCANNON.Vec3().set(jointData.connectedAxis.x, jointData.connectedAxis.y, jointData.connectedAxis.z) : null,
|
|
|
maxForce: jointData.nativeParams.maxForce,
|
|
|
- collideConnected: !!jointData.collision
|
|
|
+ collideConnected: !!jointData.collision,
|
|
|
};
|
|
|
switch (impostorJoint.joint.type) {
|
|
|
case PhysicsJoint.HingeJoint:
|
|
@@ -230,7 +229,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
stiffness: springData.stiffness,
|
|
|
damping: springData.damping,
|
|
|
localAnchorA: constraintData.pivotA,
|
|
|
- localAnchorB: constraintData.pivotB
|
|
|
+ localAnchorB: constraintData.pivotB,
|
|
|
});
|
|
|
break;
|
|
|
case PhysicsJoint.LockJoint:
|
|
@@ -249,9 +248,11 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
if (impostorJoint.joint.type !== PhysicsJoint.SpringJoint) {
|
|
|
this.world.addConstraint(constraint);
|
|
|
} else {
|
|
|
- (<SpringJointData>impostorJoint.joint.jointData).forceApplicationCallback = (<SpringJointData>impostorJoint.joint.jointData).forceApplicationCallback || function() {
|
|
|
- constraint.applyForce();
|
|
|
- };
|
|
|
+ (<SpringJointData>impostorJoint.joint.jointData).forceApplicationCallback =
|
|
|
+ (<SpringJointData>impostorJoint.joint.jointData).forceApplicationCallback ||
|
|
|
+ function () {
|
|
|
+ constraint.applyForce();
|
|
|
+ };
|
|
|
impostorJoint.mainImpostor.registerAfterPhysicsStep((<SpringJointData>impostorJoint.joint.jointData).forceApplicationCallback);
|
|
|
}
|
|
|
}
|
|
@@ -262,7 +263,6 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
} else {
|
|
|
impostorJoint.mainImpostor.unregisterAfterPhysicsStep((<SpringJointData>impostorJoint.joint.jointData).forceApplicationCallback);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
private _addMaterial(name: string, friction: number, restitution: number) {
|
|
@@ -333,7 +333,9 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
// should transform the vertex data to world coordinates!!
|
|
|
var rawVerts = object.getVerticesData ? object.getVerticesData(VertexBuffer.PositionKind) : [];
|
|
|
var rawFaces = object.getIndices ? object.getIndices() : [];
|
|
|
- if (!rawVerts) { return; }
|
|
|
+ if (!rawVerts) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
// get only scale! so the object could transform correctly.
|
|
|
let oldPosition = object.position.clone();
|
|
|
let oldRotation = object.rotation && object.rotation.clone();
|
|
@@ -387,7 +389,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
}
|
|
|
|
|
|
private _createHeightmap(object: IPhysicsEnabledObject, pointDepth?: number) {
|
|
|
- var pos = <FloatArray>(object.getVerticesData(VertexBuffer.PositionKind));
|
|
|
+ var pos = <FloatArray>object.getVerticesData(VertexBuffer.PositionKind);
|
|
|
let transform = object.computeWorldMatrix(true);
|
|
|
// convert rawVerts to object space
|
|
|
var temp = new Array<number>();
|
|
@@ -405,11 +407,11 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
var dim = Math.min(boundingInfo.boundingBox.extendSizeWorld.x, boundingInfo.boundingBox.extendSizeWorld.y);
|
|
|
var minY = boundingInfo.boundingBox.extendSizeWorld.z;
|
|
|
|
|
|
- var elementSize = dim * 2 / arraySize;
|
|
|
+ var elementSize = (dim * 2) / arraySize;
|
|
|
|
|
|
for (var i = 0; i < pos.length; i = i + 3) {
|
|
|
- var x = Math.round((pos[i + 0]) / elementSize + arraySize / 2);
|
|
|
- var z = Math.round(((pos[i + 1]) / elementSize - arraySize / 2) * -1);
|
|
|
+ var x = Math.round(pos[i + 0] / elementSize + arraySize / 2);
|
|
|
+ var z = Math.round((pos[i + 1] / elementSize - arraySize / 2) * -1);
|
|
|
var y = -pos[i + 2] + minY;
|
|
|
if (!matrix[x]) {
|
|
|
matrix[x] = [];
|
|
@@ -437,13 +439,12 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
newValue = matrix[x][(z + loc++) % arraySize];
|
|
|
}
|
|
|
matrix[x][z] = newValue;
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var shape = new this.BJSCANNON.Heightfield(matrix, {
|
|
|
- elementSize: elementSize
|
|
|
+ elementSize: elementSize,
|
|
|
});
|
|
|
|
|
|
//For future reference, needed for body transformation
|
|
@@ -464,7 +465,9 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
object.computeWorldMatrix && object.computeWorldMatrix(true);
|
|
|
// The delta between the mesh position and the mesh bounding box center
|
|
|
let bInfo = object.getBoundingInfo();
|
|
|
- if (!bInfo) { return; }
|
|
|
+ if (!bInfo) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
var center = impostor.getObjectCenter();
|
|
|
//m.getAbsolutePosition().subtract(m.getBoundingInfo().boundingBox.centerWorld)
|
|
|
this._tmpDeltaPosition.copyFrom(object.getAbsolutePivotPoint().subtract(center));
|
|
@@ -502,8 +505,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
if (oldPivot) {
|
|
|
// create a copy the pivot Matrix as it is modified in place
|
|
|
oldPivot = oldPivot.clone();
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
oldPivot = Matrix.Identity();
|
|
|
}
|
|
|
|
|
@@ -653,16 +655,13 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
result.z = shape.halfExtents.z * 2;
|
|
|
}
|
|
|
|
|
|
- public dispose() {
|
|
|
-
|
|
|
- }
|
|
|
+ public dispose() {}
|
|
|
|
|
|
private _extendNamespace() {
|
|
|
-
|
|
|
//this will force cannon to execute at least one step when using interpolation
|
|
|
let step_tmp1 = new this.BJSCANNON.Vec3();
|
|
|
let Engine = this.BJSCANNON;
|
|
|
- this.BJSCANNON.World.prototype.step = function(dt: number, timeSinceLastCalled: number, maxSubSteps: number) {
|
|
|
+ this.BJSCANNON.World.prototype.step = function (dt: number, timeSinceLastCalled: number, maxSubSteps: number) {
|
|
|
maxSubSteps = maxSubSteps || 10;
|
|
|
timeSinceLastCalled = timeSinceLastCalled || 0;
|
|
|
if (timeSinceLastCalled === 0) {
|
|
@@ -730,4 +729,6 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-PhysicsEngine.DefaultPluginFactory = () => { return new CannonJSPlugin(); };
|
|
|
+PhysicsEngine.DefaultPluginFactory = () => {
|
|
|
+ return new CannonJSPlugin();
|
|
|
+};
|