r3-legacy/src/r3-d3-shape-box.js

115 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-04-09 09:35:04 +02:00
/**
* Shape Superset - The apiShape properties get moved into the Shape object itself, and then the instance is created
* @param physics
* @param apiShape R3.D3.API.Shape
* @param halfExtents
* @constructor
*/
R3.D3.Shape.Box = function (
physics,
apiShape,
halfExtents
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (R3.Utils.UndefinedOrNull(apiShape)) {
apiShape = {
shapeType : R3.D3.API.Shape.SHAPE_TYPE_BOX
};
}
if (R3.Utils.UndefinedOrNull(halfExtents)) {
halfExtents = new R3.Vector3(
physics,
new R3.API.Vector3(
1,1,1
)
);
} else if (halfExtents instanceof R3.API.Vector3) {
halfExtents = new R3.Vector3(
this.physics,
halfExtents,
this
)
}
this.halfExtents = halfExtents;
R3.D3.Shape.call(
this,
this.physics,
apiShape
);
};
R3.D3.Shape.Box.prototype = Object.create(R3.D3.Shape.prototype);
R3.D3.Shape.Box.prototype.constructor = R3.D3.Shape.Box;
/**
*
* @returns {R3.D3.Shape.Box|*|SEA3D.Box}
*/
R3.D3.Shape.Box.prototype.createInstance = function() {
if (R3.Utils.UndefinedOrNull(this.halfExtents)) {
throw new Error('no halfExtents');
}
if (R3.Utils.UndefinedOrNull(this.halfExtents.instance)) {
throw new Error('no halfExtents instance');
}
this.instance = new CANNON.Box(
this.halfExtents.instance
);
R3.D3.Shape.prototype.createInstance.call(this);
};
R3.D3.Shape.Box.prototype.updateInstance = function() {
this.instance.halfExtents.x = this.halfExtents.x;
this.instance.halfExtents.y = this.halfExtents.y;
this.instance.halfExtents.z = this.halfExtents.z;
this.instance.updateBoundingSphereRadius();
this.instance.updateConvexPolyhedronRepresentation();
};
R3.D3.Shape.Box.prototype.toApiObject = function() {
var apiShape = R3.D3.Shape.prototype.toApiObject.call(this);
apiShape.halfExtents = this.halfExtents.toApiObject();
return apiShape;
};
R3.D3.Shape.Box.prototype.setFromMesh = function() {
if (this.parentMesh === null) {
console.log('select a mesh first');
return;
}
var box = this.parentMesh.getBoundingBox();
this.halfExtents.x = box.x / 2;
this.halfExtents.y = box.y / 2;
this.halfExtents.z = box.z / 2;
this.halfExtents.updateInstance();
};
R3.D3.Shape.Box.FromObject = function(physics, objectShape) {
var apiShape = R3.D3.API.Shape.FromObject(objectShape);
apiShape.halfExtents = R3.API.Vector3.FromObject(objectShape.halfExtents);
return new R3.D3.Shape.Box(
physics,
apiShape,
apiShape.halfExtents
);
};