File: src/shapes/Box.js
var vec2 = require('../math/vec2')
, Shape = require('./Shape')
, Convex = require('./Convex');
module.exports = Box;
/**
* Box shape class.
* @class Box
* @constructor
* @param {object} [options] (Note that this options object will be passed on to the {{#crossLink "Shape"}}{{/crossLink}} constructor.)
* @param {Number} [options.width=1] Total width of the box
* @param {Number} [options.height=1] Total height of the box
* @extends Convex
*/
function Box(options){
if(typeof(arguments[0]) === 'number' && typeof(arguments[1]) === 'number'){
options = {
width: arguments[0],
height: arguments[1]
};
console.warn('The Rectangle has been renamed to Box and its constructor signature has changed. Please use the following format: new Box({ width: 1, height: 1, ... })');
}
options = options || {};
/**
* Total width of the box
* @property width
* @type {Number}
*/
var width = this.width = options.width || 1;
/**
* Total height of the box
* @property height
* @type {Number}
*/
var height = this.height = options.height || 1;
var verts = [
vec2.fromValues(-width/2, -height/2),
vec2.fromValues( width/2, -height/2),
vec2.fromValues( width/2, height/2),
vec2.fromValues(-width/2, height/2)
];
var axes = [
vec2.fromValues(1, 0),
vec2.fromValues(0, 1)
];
options.vertices = verts;
options.axes = axes;
options.type = Shape.BOX;
Convex.call(this, options);
}
Box.prototype = new Convex();
Box.prototype.constructor = Box;
/**
* Compute moment of inertia
* @method computeMomentOfInertia
* @param {Number} mass
* @return {Number}
*/
Box.prototype.computeMomentOfInertia = function(mass){
var w = this.width,
h = this.height;
return mass * (h*h + w*w) / 12;
};
/**
* Update the bounding radius
* @method updateBoundingRadius
*/
Box.prototype.updateBoundingRadius = function(){
var w = this.width,
h = this.height;
this.boundingRadius = Math.sqrt(w*w + h*h) / 2;
};
var corner1 = vec2.create(),
corner2 = vec2.create(),
corner3 = vec2.create(),
corner4 = vec2.create();
/**
* @method computeAABB
* @param {AABB} out The resulting AABB.
* @param {Array} position
* @param {Number} angle
*/
Box.prototype.computeAABB = function(out, position, angle){
out.setFromPoints(this.vertices,position,angle,0);
};
Box.prototype.updateArea = function(){
this.area = this.width * this.height;
};