topical media & game development
mobile-query-three-plugins-cannonjs-vendor-cannon.js-src-objects-Box.js / js
/*global CANNON:true */
@class CANNON.Box
@brief A 3d box shape.
parameter: CANNON.Vec3 halfExtents
author: schteppe
@extends CANNON.Shape
CANNON.Box = function(halfExtents){
CANNON.Shape.call(this);
@property CANNON.Vec3 halfExtents
@memberof CANNON.Box
this.halfExtents = halfExtents;
this.type = CANNON.Shape.types.BOX;
@property CANNON.ConvexPolyhedron convexPolyhedronRepresentation
@brief Used by the contact generator to make contacts with other convex polyhedra for example
@memberof CANNON.Box
this.convexPolyhedronRepresentation = null;
this.updateConvexPolyhedronRepresentation();
};
CANNON.Box.prototype = new CANNON.Shape();
CANNON.Box.prototype.constructor = CANNON.Box;
@method updateConvexPolyhedronRepresentation
@memberof CANNON.Box
@brief Updates the local convex polyhedron representation used for some collisions.
CANNON.Box.prototype.updateConvexPolyhedronRepresentation = function(){
var sx = this.halfExtents.x;
var sy = this.halfExtents.y;
var sz = this.halfExtents.z;
var v = CANNON.Vec3;
var h = new CANNON.ConvexPolyhedron([new v(-sx,-sy,-sz),
new v( sx,-sy,-sz),
new v( sx, sy,-sz),
new v(-sx, sy,-sz),
new v(-sx,-sy, sz),
new v( sx,-sy, sz),
new v( sx, sy, sz),
new v(-sx, sy, sz)],
[
[0,1,2,3], // -z
[4,5,6,7], // +z
[0,1,4,5], // -y
[2,3,6,7], // +y
[0,3,4,7], // -x
[1,2,5,6], // +x
],
[new v( 0, 0,-1),
new v( 0, 0, 1),
new v( 0,-1, 0),
new v( 0, 1, 0),
new v(-1, 0, 0),
new v( 1, 0, 0)]);
this.convexPolyhedronRepresentation = h;
};
CANNON.Box.prototype.calculateLocalInertia = function(mass,target){
target = target || new CANNON.Vec3();
target.x = 1.0 / 12.0 * mass * ( 2*this.halfExtents.y*2*this.halfExtents.y + 2*this.halfExtents.z*2*this.halfExtents.z );
target.y = 1.0 / 12.0 * mass * ( 2*this.halfExtents.x*2*this.halfExtents.x + 2*this.halfExtents.z*2*this.halfExtents.z );
target.z = 1.0 / 12.0 * mass * ( 2*this.halfExtents.y*2*this.halfExtents.y + 2*this.halfExtents.x*2*this.halfExtents.x );
return target;
};
@method getCorners
@memberof CANNON.Box
@brief Get the box corners
parameter: CANNON.Quaternion quat Orientation to apply to the corner vectors. If not provided, the vectors will be in respect to the local frame.
returns: array
CANNON.Box.prototype.getCorners = function(quat){
var corners = [];
var ex = this.halfExtents;
corners.push(new CANNON.Vec3( ex.x, ex.y, ex.z));
corners.push(new CANNON.Vec3( -ex.x, ex.y, ex.z));
corners.push(new CANNON.Vec3( -ex.x, -ex.y, ex.z));
corners.push(new CANNON.Vec3( -ex.x, -ex.y, -ex.z));
corners.push(new CANNON.Vec3( ex.x, -ex.y, -ex.z));
corners.push(new CANNON.Vec3( ex.x, ex.y, -ex.z));
corners.push(new CANNON.Vec3( -ex.x, ex.y, -ex.z));
corners.push(new CANNON.Vec3( ex.x, -ex.y, ex.z));
for(var i=0; quat!=undefined && i<corners.length; i++)
quat.vmult(corners[i],corners[i]);
return corners;
};
@method getSideNormals
@memberof CANNON.Box
@brief Get the box 6 side normals
parameter: bool includeNegative If true, this function returns 6 vectors. If false, it only returns 3 (but you get 6 by reversing those 3)
parameter: CANNON.Quaternion quat Orientation to apply to the normal vectors. If not provided, the vectors will be in respect to the local frame.
returns: array
CANNON.Box.prototype.getSideNormals = function(includeNegative,quat){
var sides = [];
var ex = this.halfExtents;
sides.push(new CANNON.Vec3( ex.x, 0, 0));
sides.push(new CANNON.Vec3( 0, ex.y, 0));
sides.push(new CANNON.Vec3( 0, 0, ex.z));
if(includeNegative!=undefined && includeNegative){
sides.push(new CANNON.Vec3( -ex.x, 0, 0));
sides.push(new CANNON.Vec3( 0, -ex.y, 0));
sides.push(new CANNON.Vec3( 0, 0, -ex.z));
}
if(quat!=undefined){
for(var i=0; i<sides.length; i++)
quat.vmult(sides[i],sides[i]);
}
return sides;
};
CANNON.Box.prototype.volume = function(){
return 8.0 * this.halfExtents.x * this.halfExtents.y * this.halfExtents.z;
};
CANNON.Box.prototype.boundingSphereRadius = function(){
return this.halfExtents.norm();
};
var worldCornerTempPos = new CANNON.Vec3();
var worldCornerTempNeg = new CANNON.Vec3();
CANNON.Box.prototype.forEachWorldCorner = function(pos,quat,callback){
var e = this.halfExtents;
var corners = [[ e.x, e.y, e.z],
[ -e.x, e.y, e.z],
[ -e.x, -e.y, e.z],
[ -e.x, -e.y, -e.z],
[ e.x, -e.y, -e.z],
[ e.x, e.y, -e.z],
[ -e.x, e.y, -e.z],
[ e.x, -e.y, e.z]];
for(var i=0; i<corners.length; i++){
worldCornerTempPos.set(corners[i][0],corners[i][1],corners[i][2]);
quat.vmult(worldCornerTempPos,worldCornerTempPos);
pos.vadd(worldCornerTempPos,worldCornerTempPos);
callback(worldCornerTempPos.x,
worldCornerTempPos.y,
worldCornerTempPos.z);
}
};
CANNON.Box.prototype.calculateWorldAABB = function(pos,quat,min,max){
// Get each axis max
min.set(Infinity,Infinity,Infinity);
max.set(-Infinity,-Infinity,-Infinity);
this.forEachWorldCorner(pos,quat,function(x,y,z){
if(x > max.x) max.x = x;
if(y > max.y) max.y = y;
if(z > max.z) max.z = z;
if(x < min.x) min.x = x;
if(y < min.y) min.y = y;
if(z < min.z) min.z = z;
});
};
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.