Skip to content

Commit 380aaa5

Browse files
committed
Updated Shape constructors to only use options object. Renamed Rectangle to Box. Renamed the data property of Heightfield to heights.
1 parent dc90561 commit 380aaa5

File tree

24 files changed

+332
-227
lines changed

24 files changed

+332
-227
lines changed

demos/js/WebGLRenderer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ WebGLRenderer.prototype.drawRenderable = function(obj, graphics, color, lineColo
789789
} else if(child instanceof p2.Line){
790790
WebGLRenderer.drawLine(graphics, offset, angle, child.length, lineColor, lw);
791791

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

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

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

818818
}

src/collision/Narrowphase.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var vec2 = require('../math/vec2')
1313
, Convex = require('../shapes/Convex')
1414
, Shape = require('../shapes/Shape')
1515
, Body = require('../objects/Body')
16-
, Rectangle = require('../shapes/Rectangle');
16+
, Box = require('../shapes/Box');
1717

1818
module.exports = Narrowphase;
1919

@@ -386,29 +386,29 @@ Narrowphase.prototype.convexLine = function(
386386
};
387387

388388
/**
389-
* Line/rectangle narrowphase
390-
* @method lineRectangle
389+
* Line/box narrowphase
390+
* @method lineBox
391391
* @param {Body} lineBody
392392
* @param {Line} lineShape
393393
* @param {Array} lineOffset
394394
* @param {Number} lineAngle
395-
* @param {Body} rectangleBody
396-
* @param {Rectangle} rectangleShape
397-
* @param {Array} rectangleOffset
398-
* @param {Number} rectangleAngle
395+
* @param {Body} boxBody
396+
* @param {Box} boxShape
397+
* @param {Array} boxOffset
398+
* @param {Number} boxAngle
399399
* @param {Boolean} justTest
400400
* @todo Implement me!
401401
*/
402-
Narrowphase.prototype[Shape.LINE | Shape.RECTANGLE] =
403-
Narrowphase.prototype.lineRectangle = function(
402+
Narrowphase.prototype[Shape.LINE | Shape.BOX] =
403+
Narrowphase.prototype.lineBox = function(
404404
lineBody,
405405
lineShape,
406406
lineOffset,
407407
lineAngle,
408-
rectangleBody,
409-
rectangleShape,
410-
rectangleOffset,
411-
rectangleAngle,
408+
boxBody,
409+
boxShape,
410+
boxOffset,
411+
boxAngle,
412412
justTest
413413
){
414414
// TODO
@@ -426,7 +426,7 @@ function setConvexToCapsuleShapeMiddle(convexShape, capsuleShape){
426426
vec2.set(convexShape.vertices[3], -capsuleShape.length * 0.5, capsuleShape.radius);
427427
}
428428

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

432432
/**
@@ -442,7 +442,7 @@ var convexCapsule_tempRect = new Rectangle(1,1),
442442
* @param {Number} capsuleAngle
443443
*/
444444
Narrowphase.prototype[Shape.CAPSULE | Shape.CONVEX] =
445-
Narrowphase.prototype[Shape.CAPSULE | Shape.RECTANGLE] =
445+
Narrowphase.prototype[Shape.CAPSULE | Shape.BOX] =
446446
Narrowphase.prototype.convexCapsule = function(
447447
convexBody,
448448
convexShape,
@@ -515,7 +515,7 @@ Narrowphase.prototype.lineCapsule = function(
515515

516516
var capsuleCapsule_tempVec1 = vec2.create();
517517
var capsuleCapsule_tempVec2 = vec2.create();
518-
var capsuleCapsule_tempRect1 = new Rectangle(1,1);
518+
var capsuleCapsule_tempRect1 = new Box({ width: 1, height: 1 });
519519

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

584-
// Check circles against the center rectangles
584+
// Check circles against the center boxs
585585
var rect = capsuleCapsule_tempRect1;
586586
setConvexToCapsuleShapeMiddle(rect,si);
587587
var result1 = this.convexCapsule(bi,rect,xi,ai, bj,sj,xj,aj, justTest);
@@ -979,7 +979,7 @@ Narrowphase.prototype.circleCapsule = function(bi,si,xi,ai, bj,sj,xj,aj, justTes
979979
* @param {Number} circleRadius
980980
*/
981981
Narrowphase.prototype[Shape.CIRCLE | Shape.CONVEX] =
982-
Narrowphase.prototype[Shape.CIRCLE | Shape.RECTANGLE] =
982+
Narrowphase.prototype[Shape.CIRCLE | Shape.BOX] =
983983
Narrowphase.prototype.circleConvex = function(
984984
circleBody,
985985
circleShape,
@@ -1191,7 +1191,7 @@ function pointInConvex(worldPoint,convexShape,convexOffset,convexAngle){
11911191
* @todo don't transform each vertex, but transform the particle position to convex-local instead
11921192
*/
11931193
Narrowphase.prototype[Shape.PARTICLE | Shape.CONVEX] =
1194-
Narrowphase.prototype[Shape.PARTICLE | Shape.RECTANGLE] =
1194+
Narrowphase.prototype[Shape.PARTICLE | Shape.BOX] =
11951195
Narrowphase.prototype.particleConvex = function(
11961196
particleBody,
11971197
particleShape,
@@ -1384,7 +1384,7 @@ Narrowphase.prototype.circleCircle = function(
13841384
* @param {Boolean} justTest
13851385
*/
13861386
Narrowphase.prototype[Shape.PLANE | Shape.CONVEX] =
1387-
Narrowphase.prototype[Shape.PLANE | Shape.RECTANGLE] =
1387+
Narrowphase.prototype[Shape.PLANE | Shape.BOX] =
13881388
Narrowphase.prototype.planeConvex = function(
13891389
planeBody,
13901390
planeShape,
@@ -1574,7 +1574,7 @@ Narrowphase.prototype.circleParticle = function(
15741574
return 1;
15751575
};
15761576

1577-
var planeCapsule_tmpCircle = new Circle(1),
1577+
var planeCapsule_tmpCircle = new Circle({ radius: 1 }),
15781578
planeCapsule_tmp1 = vec2.create(),
15791579
planeCapsule_tmp2 = vec2.create(),
15801580
planeCapsule_tmp3 = vec2.create();
@@ -1732,8 +1732,8 @@ Narrowphase.prototype.circlePlane = function( bi,si,xi,ai, bj,sj,xj,aj, justTe
17321732
* @param {Number} aj
17331733
*/
17341734
Narrowphase.prototype[Shape.CONVEX] =
1735-
Narrowphase.prototype[Shape.CONVEX | Shape.RECTANGLE] =
1736-
Narrowphase.prototype[Shape.RECTANGLE] =
1735+
Narrowphase.prototype[Shape.CONVEX | Shape.BOX] =
1736+
Narrowphase.prototype[Shape.BOX] =
17371737
Narrowphase.prototype.convexConvex = function( bi,si,xi,ai, bj,sj,xj,aj, justTest, precision ){
17381738
var sepAxis = tmp1,
17391739
worldPoint = tmp2,
@@ -1977,7 +1977,7 @@ Narrowphase.findSeparatingAxis = function(c1,offset1,angle1,c2,offset2,angle2,se
19771977
span1 = fsa_tmp5,
19781978
span2 = fsa_tmp6;
19791979

1980-
if(c1 instanceof Rectangle && c2 instanceof Rectangle){
1980+
if(c1 instanceof Box && c2 instanceof Box){
19811981

19821982
for(var j=0; j!==2; j++){
19831983
var c = c1,
@@ -2186,7 +2186,7 @@ var circleHeightfield_candidate = vec2.create(),
21862186
Narrowphase.prototype[Shape.CIRCLE | Shape.HEIGHTFIELD] =
21872187
Narrowphase.prototype.circleHeightfield = function( circleBody,circleShape,circlePos,circleAngle,
21882188
hfBody,hfShape,hfPos,hfAngle, justTest, radius ){
2189-
var data = hfShape.data,
2189+
var data = hfShape.heights,
21902190
radius = radius || circleShape.radius,
21912191
w = hfShape.elementWidth,
21922192
dist = circleHeightfield_dist,
@@ -2351,7 +2351,7 @@ Narrowphase.prototype.circleHeightfield = function( circleBody,circleShape,circl
23512351
var convexHeightfield_v0 = vec2.create(),
23522352
convexHeightfield_v1 = vec2.create(),
23532353
convexHeightfield_tilePos = vec2.create(),
2354-
convexHeightfield_tempConvexShape = new Convex([vec2.create(),vec2.create(),vec2.create(),vec2.create()]);
2354+
convexHeightfield_tempConvexShape = new Convex({ vertices: [vec2.create(),vec2.create(),vec2.create(),vec2.create()] });
23552355
/**
23562356
* @method circleHeightfield
23572357
* @param {Body} bi
@@ -2362,11 +2362,11 @@ var convexHeightfield_v0 = vec2.create(),
23622362
* @param {Array} xj
23632363
* @param {Number} aj
23642364
*/
2365-
Narrowphase.prototype[Shape.RECTANGLE | Shape.HEIGHTFIELD] =
2365+
Narrowphase.prototype[Shape.BOX | Shape.HEIGHTFIELD] =
23662366
Narrowphase.prototype[Shape.CONVEX | Shape.HEIGHTFIELD] =
23672367
Narrowphase.prototype.convexHeightfield = function( convexBody,convexShape,convexPos,convexAngle,
23682368
hfBody,hfShape,hfPos,hfAngle, justTest ){
2369-
var data = hfShape.data,
2369+
var data = hfShape.heights,
23702370
w = hfShape.elementWidth,
23712371
v0 = convexHeightfield_v0,
23722372
v1 = convexHeightfield_v1,

src/objects/Body.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = Body;
4545
* });
4646
*
4747
* // Add a circular shape to the body
48-
* body.addShape(new Circle(1));
48+
* body.addShape(new Circle({ radius: 1 }));
4949
*
5050
* // Add the body to the world
5151
* world.addBody(body);
@@ -538,7 +538,7 @@ Body.prototype.updateBoundingRadius = function(){
538538
*
539539
* @example
540540
* var body = new Body(),
541-
* shape = new Circle();
541+
* shape = new Circle({ radius: 1 });
542542
*
543543
* // Add the shape to the body, positioned in the center
544544
* body.addShape(shape);
@@ -826,7 +826,7 @@ Body.prototype.fromPolygon = function(path,options){
826826
// Add convexes
827827
for(var i=0; i!==convexes.length; i++){
828828
// Create convex
829-
var c = new Convex(convexes[i].vertices);
829+
var c = new Convex({ vertices: convexes[i].vertices });
830830

831831
// Move all vertices so its center of mass is in the local center of the convex
832832
for(var j=0; j!==c.vertices.length; j++){

src/p2.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var p2 = module.exports = {
3131
PrismaticConstraint : require('./constraints/PrismaticConstraint'),
3232
Ray : require('./collision/Ray'),
3333
RaycastResult : require('./collision/RaycastResult'),
34-
Rectangle : require('./shapes/Rectangle'),
34+
Box : require('./shapes/Box'),
3535
RotationalVelocityEquation : require('./equations/RotationalVelocityEquation'),
3636
SAPBroadphase : require('./collision/SAPBroadphase'),
3737
Shape : require('./shapes/Shape'),
@@ -45,3 +45,10 @@ var p2 = module.exports = {
4545
vec2 : require('./math/vec2'),
4646
version : require('../package.json').version,
4747
};
48+
49+
Object.defineProperty(p2, 'Rectangle', {
50+
get: function() {
51+
console.warn('The Rectangle class has been renamed to Box.');
52+
return this.Box;
53+
}
54+
});

src/shapes/Rectangle.js renamed to src/shapes/Box.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,40 @@ var vec2 = require('../math/vec2')
22
, Shape = require('./Shape')
33
, Convex = require('./Convex');
44

5-
module.exports = Rectangle;
5+
module.exports = Box;
66

77
/**
8-
* Rectangle shape class.
9-
* @class Rectangle
8+
* Box shape class.
9+
* @class Box
1010
* @constructor
11-
* @param {Number} [width=1] Width
12-
* @param {Number} [height=1] Height
11+
* @param {object} [options] (Note that this options object will be passed on to the {{#crossLink "Shape"}}{{/crossLink}} constructor.)
12+
* @param {Number} [options.width=1] Total width of the box
13+
* @param {Number} [options.height=1] Total height of the box
1314
* @extends Convex
1415
*/
15-
function Rectangle(width, height){
16+
function Box(options){
17+
if(typeof(arguments[0]) === 'number' && typeof(arguments[1]) === 'number'){
18+
options = {
19+
width: arguments[0],
20+
height: arguments[1]
21+
};
22+
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, ... })');
23+
}
24+
options = options || {};
1625

1726
/**
18-
* Total width of the rectangle
27+
* Total width of the box
1928
* @property width
2029
* @type {Number}
2130
*/
22-
this.width = width || 1;
31+
var width = this.width = options.width || 1;
2332

2433
/**
25-
* Total height of the rectangle
34+
* Total height of the box
2635
* @property height
2736
* @type {Number}
2837
*/
29-
this.height = height || 1;
38+
var height = this.height = options.height || 1;
3039

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

42-
Convex.call(this, verts, axes);
43-
44-
this.type = Shape.RECTANGLE;
51+
options.vertices = verts;
52+
options.axes = axes;
53+
options.type = Shape.BOX;
54+
Convex.call(this, options);
4555
}
46-
Rectangle.prototype = new Convex([]);
47-
Rectangle.prototype.constructor = Rectangle;
56+
Box.prototype = new Convex();
57+
Box.prototype.constructor = Box;
4858

4959
/**
5060
* Compute moment of inertia
5161
* @method computeMomentOfInertia
5262
* @param {Number} mass
5363
* @return {Number}
5464
*/
55-
Rectangle.prototype.computeMomentOfInertia = function(mass){
65+
Box.prototype.computeMomentOfInertia = function(mass){
5666
var w = this.width,
5767
h = this.height;
5868
return mass * (h*h + w*w) / 12;
@@ -62,7 +72,7 @@ Rectangle.prototype.computeMomentOfInertia = function(mass){
6272
* Update the bounding radius
6373
* @method updateBoundingRadius
6474
*/
65-
Rectangle.prototype.updateBoundingRadius = function(){
75+
Box.prototype.updateBoundingRadius = function(){
6676
var w = this.width,
6777
h = this.height;
6878
this.boundingRadius = Math.sqrt(w*w + h*h) / 2;
@@ -79,11 +89,11 @@ var corner1 = vec2.create(),
7989
* @param {Array} position
8090
* @param {Number} angle
8191
*/
82-
Rectangle.prototype.computeAABB = function(out, position, angle){
92+
Box.prototype.computeAABB = function(out, position, angle){
8393
out.setFromPoints(this.vertices,position,angle,0);
8494
};
8595

86-
Rectangle.prototype.updateArea = function(){
96+
Box.prototype.updateArea = function(){
8797
this.area = this.width * this.height;
8898
};
8999

src/shapes/Capsule.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,40 @@ module.exports = Capsule;
88
* @class Capsule
99
* @constructor
1010
* @extends Shape
11-
* @param {Number} [length=1] The distance between the end points
12-
* @param {Number} [radius=1] Radius of the capsule
11+
* @param {object} [options] (Note that this options object will be passed on to the {{#crossLink "Shape"}}{{/crossLink}} constructor.)
12+
* @param {Number} [options.length=1] The distance between the end points
13+
* @param {Number} [options.radius=1] Radius of the capsule
1314
* @example
14-
* var radius = 1;
15-
* var length = 2;
16-
* var capsuleShape = new Capsule(length, radius);
15+
* var capsuleShape = new Capsule({
16+
* length: 1,
17+
* radius: 2
18+
* });
1719
* body.addShape(capsuleShape);
1820
*/
19-
function Capsule(length, radius){
21+
function Capsule(options){
22+
if(typeof(arguments[0]) === 'number' && typeof(arguments[1]) === 'number'){
23+
options = {
24+
length: arguments[0],
25+
radius: arguments[1]
26+
};
27+
console.warn('The Capsule constructor signature has changed. Please use the following format: new Capsule({ radius: 1, length: 1 })');
28+
}
29+
options = options || {};
2030

2131
/**
2232
* The distance between the end points.
2333
* @property {Number} length
2434
*/
25-
this.length = length || 1;
35+
this.length = options.length || 1;
2636

2737
/**
2838
* The radius of the capsule.
2939
* @property {Number} radius
3040
*/
31-
this.radius = radius || 1;
41+
this.radius = options.radius || 1;
3242

33-
Shape.call(this,Shape.CAPSULE);
43+
options.type = Shape.CAPSULE;
44+
Shape.call(this, options);
3445
}
3546
Capsule.prototype = new Shape();
3647
Capsule.prototype.constructor = Capsule;

0 commit comments

Comments
 (0)