Skip to content

Commit

Permalink
Updated Shape constructors to only use options object. Renamed Rectan…
Browse files Browse the repository at this point in the history
…gle to Box. Renamed the data property of Heightfield to heights.
  • Loading branch information
schteppe committed Jul 12, 2015
1 parent dc90561 commit 380aaa5
Show file tree
Hide file tree
Showing 24 changed files with 332 additions and 227 deletions.
8 changes: 4 additions & 4 deletions demos/js/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ WebGLRenderer.prototype.drawRenderable = function(obj, graphics, color, lineColo
} else if(child instanceof p2.Line){
WebGLRenderer.drawLine(graphics, offset, angle, child.length, lineColor, lw);

} else if(child instanceof p2.Rectangle){
} else if(child instanceof p2.Box){
this.drawRectangle(graphics, offset[0], offset[1], angle, child.width, child.height, lineColor, color, lw, isSleeping);

} else if(child instanceof p2.Capsule){
Expand All @@ -808,11 +808,11 @@ WebGLRenderer.prototype.drawRenderable = function(obj, graphics, color, lineColo

} else if(child instanceof p2.Heightfield){
var path = [[0,-100]];
for(var j=0; j!==child.data.length; j++){
var v = child.data[j];
for(var j=0; j!==child.heights.length; j++){
var v = child.heights[j];
path.push([j*child.elementWidth, v]);
}
path.push([child.data.length*child.elementWidth,-100]);
path.push([child.heights.length*child.elementWidth,-100]);
this.drawPath(graphics, path, lineColor, color, lw, isSleeping);

}
Expand Down
56 changes: 28 additions & 28 deletions src/collision/Narrowphase.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var vec2 = require('../math/vec2')
, Convex = require('../shapes/Convex')
, Shape = require('../shapes/Shape')
, Body = require('../objects/Body')
, Rectangle = require('../shapes/Rectangle');
, Box = require('../shapes/Box');

module.exports = Narrowphase;

Expand Down Expand Up @@ -386,29 +386,29 @@ Narrowphase.prototype.convexLine = function(
};

/**
* Line/rectangle narrowphase
* @method lineRectangle
* Line/box narrowphase
* @method lineBox
* @param {Body} lineBody
* @param {Line} lineShape
* @param {Array} lineOffset
* @param {Number} lineAngle
* @param {Body} rectangleBody
* @param {Rectangle} rectangleShape
* @param {Array} rectangleOffset
* @param {Number} rectangleAngle
* @param {Body} boxBody
* @param {Box} boxShape
* @param {Array} boxOffset
* @param {Number} boxAngle
* @param {Boolean} justTest
* @todo Implement me!
*/
Narrowphase.prototype[Shape.LINE | Shape.RECTANGLE] =
Narrowphase.prototype.lineRectangle = function(
Narrowphase.prototype[Shape.LINE | Shape.BOX] =
Narrowphase.prototype.lineBox = function(
lineBody,
lineShape,
lineOffset,
lineAngle,
rectangleBody,
rectangleShape,
rectangleOffset,
rectangleAngle,
boxBody,
boxShape,
boxOffset,
boxAngle,
justTest
){
// TODO
Expand All @@ -426,7 +426,7 @@ function setConvexToCapsuleShapeMiddle(convexShape, capsuleShape){
vec2.set(convexShape.vertices[3], -capsuleShape.length * 0.5, capsuleShape.radius);
}

var convexCapsule_tempRect = new Rectangle(1,1),
var convexCapsule_tempRect = new Box({ width: 1, height: 1 }),
convexCapsule_tempVec = vec2.create();

/**
Expand All @@ -442,7 +442,7 @@ var convexCapsule_tempRect = new Rectangle(1,1),
* @param {Number} capsuleAngle
*/
Narrowphase.prototype[Shape.CAPSULE | Shape.CONVEX] =
Narrowphase.prototype[Shape.CAPSULE | Shape.RECTANGLE] =
Narrowphase.prototype[Shape.CAPSULE | Shape.BOX] =
Narrowphase.prototype.convexCapsule = function(
convexBody,
convexShape,
Expand Down Expand Up @@ -515,7 +515,7 @@ Narrowphase.prototype.lineCapsule = function(

var capsuleCapsule_tempVec1 = vec2.create();
var capsuleCapsule_tempVec2 = vec2.create();
var capsuleCapsule_tempRect1 = new Rectangle(1,1);
var capsuleCapsule_tempRect1 = new Box({ width: 1, height: 1 });

/**
* Capsule/capsule narrowphase
Expand Down Expand Up @@ -581,7 +581,7 @@ Narrowphase.prototype.capsuleCapsule = function(bi,si,xi,ai, bj,sj,xj,aj, justTe
this.enableFriction = false;
}

// Check circles against the center rectangles
// Check circles against the center boxs
var rect = capsuleCapsule_tempRect1;
setConvexToCapsuleShapeMiddle(rect,si);
var result1 = this.convexCapsule(bi,rect,xi,ai, bj,sj,xj,aj, justTest);
Expand Down Expand Up @@ -979,7 +979,7 @@ Narrowphase.prototype.circleCapsule = function(bi,si,xi,ai, bj,sj,xj,aj, justTes
* @param {Number} circleRadius
*/
Narrowphase.prototype[Shape.CIRCLE | Shape.CONVEX] =
Narrowphase.prototype[Shape.CIRCLE | Shape.RECTANGLE] =
Narrowphase.prototype[Shape.CIRCLE | Shape.BOX] =
Narrowphase.prototype.circleConvex = function(
circleBody,
circleShape,
Expand Down Expand Up @@ -1191,7 +1191,7 @@ function pointInConvex(worldPoint,convexShape,convexOffset,convexAngle){
* @todo don't transform each vertex, but transform the particle position to convex-local instead
*/
Narrowphase.prototype[Shape.PARTICLE | Shape.CONVEX] =
Narrowphase.prototype[Shape.PARTICLE | Shape.RECTANGLE] =
Narrowphase.prototype[Shape.PARTICLE | Shape.BOX] =
Narrowphase.prototype.particleConvex = function(
particleBody,
particleShape,
Expand Down Expand Up @@ -1384,7 +1384,7 @@ Narrowphase.prototype.circleCircle = function(
* @param {Boolean} justTest
*/
Narrowphase.prototype[Shape.PLANE | Shape.CONVEX] =
Narrowphase.prototype[Shape.PLANE | Shape.RECTANGLE] =
Narrowphase.prototype[Shape.PLANE | Shape.BOX] =
Narrowphase.prototype.planeConvex = function(
planeBody,
planeShape,
Expand Down Expand Up @@ -1574,7 +1574,7 @@ Narrowphase.prototype.circleParticle = function(
return 1;
};

var planeCapsule_tmpCircle = new Circle(1),
var planeCapsule_tmpCircle = new Circle({ radius: 1 }),
planeCapsule_tmp1 = vec2.create(),
planeCapsule_tmp2 = vec2.create(),
planeCapsule_tmp3 = vec2.create();
Expand Down Expand Up @@ -1732,8 +1732,8 @@ Narrowphase.prototype.circlePlane = function( bi,si,xi,ai, bj,sj,xj,aj, justTe
* @param {Number} aj
*/
Narrowphase.prototype[Shape.CONVEX] =
Narrowphase.prototype[Shape.CONVEX | Shape.RECTANGLE] =
Narrowphase.prototype[Shape.RECTANGLE] =
Narrowphase.prototype[Shape.CONVEX | Shape.BOX] =
Narrowphase.prototype[Shape.BOX] =
Narrowphase.prototype.convexConvex = function( bi,si,xi,ai, bj,sj,xj,aj, justTest, precision ){
var sepAxis = tmp1,
worldPoint = tmp2,
Expand Down Expand Up @@ -1977,7 +1977,7 @@ Narrowphase.findSeparatingAxis = function(c1,offset1,angle1,c2,offset2,angle2,se
span1 = fsa_tmp5,
span2 = fsa_tmp6;

if(c1 instanceof Rectangle && c2 instanceof Rectangle){
if(c1 instanceof Box && c2 instanceof Box){

for(var j=0; j!==2; j++){
var c = c1,
Expand Down Expand Up @@ -2186,7 +2186,7 @@ var circleHeightfield_candidate = vec2.create(),
Narrowphase.prototype[Shape.CIRCLE | Shape.HEIGHTFIELD] =
Narrowphase.prototype.circleHeightfield = function( circleBody,circleShape,circlePos,circleAngle,
hfBody,hfShape,hfPos,hfAngle, justTest, radius ){
var data = hfShape.data,
var data = hfShape.heights,
radius = radius || circleShape.radius,
w = hfShape.elementWidth,
dist = circleHeightfield_dist,
Expand Down Expand Up @@ -2351,7 +2351,7 @@ Narrowphase.prototype.circleHeightfield = function( circleBody,circleShape,circl
var convexHeightfield_v0 = vec2.create(),
convexHeightfield_v1 = vec2.create(),
convexHeightfield_tilePos = vec2.create(),
convexHeightfield_tempConvexShape = new Convex([vec2.create(),vec2.create(),vec2.create(),vec2.create()]);
convexHeightfield_tempConvexShape = new Convex({ vertices: [vec2.create(),vec2.create(),vec2.create(),vec2.create()] });
/**
* @method circleHeightfield
* @param {Body} bi
Expand All @@ -2362,11 +2362,11 @@ var convexHeightfield_v0 = vec2.create(),
* @param {Array} xj
* @param {Number} aj
*/
Narrowphase.prototype[Shape.RECTANGLE | Shape.HEIGHTFIELD] =
Narrowphase.prototype[Shape.BOX | Shape.HEIGHTFIELD] =
Narrowphase.prototype[Shape.CONVEX | Shape.HEIGHTFIELD] =
Narrowphase.prototype.convexHeightfield = function( convexBody,convexShape,convexPos,convexAngle,
hfBody,hfShape,hfPos,hfAngle, justTest ){
var data = hfShape.data,
var data = hfShape.heights,
w = hfShape.elementWidth,
v0 = convexHeightfield_v0,
v1 = convexHeightfield_v1,
Expand Down
6 changes: 3 additions & 3 deletions src/objects/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = Body;
* });
*
* // Add a circular shape to the body
* body.addShape(new Circle(1));
* body.addShape(new Circle({ radius: 1 }));
*
* // Add the body to the world
* world.addBody(body);
Expand Down Expand Up @@ -538,7 +538,7 @@ Body.prototype.updateBoundingRadius = function(){
*
* @example
* var body = new Body(),
* shape = new Circle();
* shape = new Circle({ radius: 1 });
*
* // Add the shape to the body, positioned in the center
* body.addShape(shape);
Expand Down Expand Up @@ -826,7 +826,7 @@ Body.prototype.fromPolygon = function(path,options){
// Add convexes
for(var i=0; i!==convexes.length; i++){
// Create convex
var c = new Convex(convexes[i].vertices);
var c = new Convex({ vertices: convexes[i].vertices });

// Move all vertices so its center of mass is in the local center of the convex
for(var j=0; j!==c.vertices.length; j++){
Expand Down
9 changes: 8 additions & 1 deletion src/p2.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var p2 = module.exports = {
PrismaticConstraint : require('./constraints/PrismaticConstraint'),
Ray : require('./collision/Ray'),
RaycastResult : require('./collision/RaycastResult'),
Rectangle : require('./shapes/Rectangle'),
Box : require('./shapes/Box'),
RotationalVelocityEquation : require('./equations/RotationalVelocityEquation'),
SAPBroadphase : require('./collision/SAPBroadphase'),
Shape : require('./shapes/Shape'),
Expand All @@ -45,3 +45,10 @@ var p2 = module.exports = {
vec2 : require('./math/vec2'),
version : require('../package.json').version,
};

Object.defineProperty(p2, 'Rectangle', {
get: function() {
console.warn('The Rectangle class has been renamed to Box.');
return this.Box;
}
});
48 changes: 29 additions & 19 deletions src/shapes/Rectangle.js → src/shapes/Box.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@ var vec2 = require('../math/vec2')
, Shape = require('./Shape')
, Convex = require('./Convex');

module.exports = Rectangle;
module.exports = Box;

/**
* Rectangle shape class.
* @class Rectangle
* Box shape class.
* @class Box
* @constructor
* @param {Number} [width=1] Width
* @param {Number} [height=1] Height
* @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 Rectangle(width, height){
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 rectangle
* Total width of the box
* @property width
* @type {Number}
*/
this.width = width || 1;
var width = this.width = options.width || 1;

/**
* Total height of the rectangle
* Total height of the box
* @property height
* @type {Number}
*/
this.height = height || 1;
var height = this.height = options.height || 1;

var verts = [
vec2.fromValues(-width/2, -height/2),
Expand All @@ -39,20 +48,21 @@ function Rectangle(width, height){
vec2.fromValues(0, 1)
];

Convex.call(this, verts, axes);

this.type = Shape.RECTANGLE;
options.vertices = verts;
options.axes = axes;
options.type = Shape.BOX;
Convex.call(this, options);
}
Rectangle.prototype = new Convex([]);
Rectangle.prototype.constructor = Rectangle;
Box.prototype = new Convex();
Box.prototype.constructor = Box;

/**
* Compute moment of inertia
* @method computeMomentOfInertia
* @param {Number} mass
* @return {Number}
*/
Rectangle.prototype.computeMomentOfInertia = function(mass){
Box.prototype.computeMomentOfInertia = function(mass){
var w = this.width,
h = this.height;
return mass * (h*h + w*w) / 12;
Expand All @@ -62,7 +72,7 @@ Rectangle.prototype.computeMomentOfInertia = function(mass){
* Update the bounding radius
* @method updateBoundingRadius
*/
Rectangle.prototype.updateBoundingRadius = function(){
Box.prototype.updateBoundingRadius = function(){
var w = this.width,
h = this.height;
this.boundingRadius = Math.sqrt(w*w + h*h) / 2;
Expand All @@ -79,11 +89,11 @@ var corner1 = vec2.create(),
* @param {Array} position
* @param {Number} angle
*/
Rectangle.prototype.computeAABB = function(out, position, angle){
Box.prototype.computeAABB = function(out, position, angle){
out.setFromPoints(this.vertices,position,angle,0);
};

Rectangle.prototype.updateArea = function(){
Box.prototype.updateArea = function(){
this.area = this.width * this.height;
};

29 changes: 20 additions & 9 deletions src/shapes/Capsule.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,40 @@ module.exports = Capsule;
* @class Capsule
* @constructor
* @extends Shape
* @param {Number} [length=1] The distance between the end points
* @param {Number} [radius=1] Radius of the capsule
* @param {object} [options] (Note that this options object will be passed on to the {{#crossLink "Shape"}}{{/crossLink}} constructor.)
* @param {Number} [options.length=1] The distance between the end points
* @param {Number} [options.radius=1] Radius of the capsule
* @example
* var radius = 1;
* var length = 2;
* var capsuleShape = new Capsule(length, radius);
* var capsuleShape = new Capsule({
* length: 1,
* radius: 2
* });
* body.addShape(capsuleShape);
*/
function Capsule(length, radius){
function Capsule(options){
if(typeof(arguments[0]) === 'number' && typeof(arguments[1]) === 'number'){
options = {
length: arguments[0],
radius: arguments[1]
};
console.warn('The Capsule constructor signature has changed. Please use the following format: new Capsule({ radius: 1, length: 1 })');
}
options = options || {};

/**
* The distance between the end points.
* @property {Number} length
*/
this.length = length || 1;
this.length = options.length || 1;

/**
* The radius of the capsule.
* @property {Number} radius
*/
this.radius = radius || 1;
this.radius = options.radius || 1;

Shape.call(this,Shape.CAPSULE);
options.type = Shape.CAPSULE;
Shape.call(this, options);
}
Capsule.prototype = new Shape();
Capsule.prototype.constructor = Capsule;
Expand Down
Loading

0 comments on commit 380aaa5

Please sign in to comment.