Skip to content

Commit d031d91

Browse files
committed
Added bodyBounds class, finished alignBody and fixed typedefs
1 parent 9aeead5 commit d031d91

1 file changed

Lines changed: 64 additions & 63 deletions

File tree

src/physics/matter-js/MatterPhysics.js

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var ALIGN_CONST = require('../../display/align/const');
88
var Axes = require('./lib/geometry/Axes');
99
var Bodies = require('./lib/factory/Bodies');
1010
var Body = require('./lib/body/Body');
11+
var BodyBounds = require('./BodyBounds');
1112
var Bounds = require('./lib/geometry/Bounds');
1213
var Class = require('../../utils/Class');
1314
var Composite = require('./lib/body/Composite');
@@ -138,7 +139,7 @@ var MatterPhysics = new Class({
138139
this.world;
139140

140141
/**
141-
* An instance of the Matter Factory. This class provides lots of functions for creatying a
142+
* An instance of the Matter Factory. This class provides lots of functions for creating a
142143
* wide variety of physics objects and adds them automatically to the Matter World.
143144
*
144145
* You can use this class to cut-down on the amount of code required in your game, however,
@@ -151,6 +152,16 @@ var MatterPhysics = new Class({
151152
*/
152153
this.add;
153154

155+
/**
156+
* An instance of the Body Bounds class. This class contains functions used for getting the
157+
* world position from various points around the bounds of a physics body.
158+
*
159+
* @name Phaser.Physics.Matter.MatterPhysics#bodyBounds
160+
* @type {Phaser.Physics.Matter.BodyBounds}
161+
* @since 3.22.0
162+
*/
163+
this.bodyBounds;
164+
154165
// Body
155166

156167
/**
@@ -425,6 +436,7 @@ var MatterPhysics = new Class({
425436
{
426437
this.world = new World(this.scene, this.config);
427438
this.add = new Factory(this.world);
439+
this.bodyBounds = new BodyBounds();
428440

429441
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
430442
},
@@ -671,7 +683,7 @@ var MatterPhysics = new Class({
671683
* @method Phaser.Physics.Matter.MatterPhysics#containsPoint
672684
* @since 3.22.0
673685
*
674-
* @param {(MatterJS.Body|MatterJS.Body[])} body - The body, or an array of bodies, to check against the point.
686+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} body - The body, or an array of bodies, to check against the point.
675687
* @param {number} x - The horizontal coordinate of the point.
676688
* @param {number} y - The vertical coordinate of the point.
677689
*
@@ -702,9 +714,9 @@ var MatterPhysics = new Class({
702714
*
703715
* @param {number} x - The horizontal coordinate of the point.
704716
* @param {number} y - The vertical coordinate of the point.
705-
* @param {MatterJS.Body[]} [bodies] - An array of bodies to check. If not provided it will search all bodies in the world.
717+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} [bodies] - An array of bodies to check. If not provided it will search all bodies in the world.
706718
*
707-
* @return {MatterJS.Body[]} An array of bodies which contain the given point.
719+
* @return {Phaser.Types.Physics.Matter.MatterBody[]} An array of bodies which contain the given point.
708720
*/
709721
intersectPoint: function (x, y, bodies)
710722
{
@@ -742,9 +754,9 @@ var MatterPhysics = new Class({
742754
* @param {number} width - The width of the area.
743755
* @param {number} height - The height of the area.
744756
* @param {boolean} [outside=false] - If `false` it checks for vertices inside the area, if `true` it checks for vertices outside the area.
745-
* @param {MatterJS.Body[]} [bodies] - An array of bodies to check. If not provided it will search all bodies in the world.
757+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} [bodies] - An array of bodies to check. If not provided it will search all bodies in the world.
746758
*
747-
* @return {MatterJS.Body[]} An array of bodies that intersect with the given area.
759+
* @return {Phaser.Types.Physics.Matter.MatterBody[]} An array of bodies that intersect with the given area.
748760
*/
749761
intersectRect: function (x, y, width, height, outside, bodies)
750762
{
@@ -787,9 +799,9 @@ var MatterPhysics = new Class({
787799
* @param {number} x2 - The horizontal coordinate of the end of the ray segment.
788800
* @param {number} y2 - The vertical coordinate of the end of the ray segment.
789801
* @param {number} [rayWidth=1] - The width of the ray segment.
790-
* @param {MatterJS.Body[]} [bodies] - An array of bodies to check. If not provided it will search all bodies in the world.
802+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} [bodies] - An array of bodies to check. If not provided it will search all bodies in the world.
791803
*
792-
* @return {MatterJS.Body[]} An array of bodies whos vertices intersect with the ray segment.
804+
* @return {Phaser.Types.Physics.Matter.MatterBody[]} An array of bodies whos vertices intersect with the ray segment.
793805
*/
794806
intersectRay: function (x1, y1, x2, y2, rayWidth, bodies)
795807
{
@@ -816,10 +828,10 @@ var MatterPhysics = new Class({
816828
* @method Phaser.Physics.Matter.MatterPhysics#intersectBody
817829
* @since 3.22.0
818830
*
819-
* @param {MatterJS.Body} body - The target body.
820-
* @param {MatterJS.Body[]} [bodies] - An array of bodies to check the target body against. If not provided it will search all bodies in the world.
831+
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The target body.
832+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} [bodies] - An array of bodies to check the target body against. If not provided it will search all bodies in the world.
821833
*
822-
* @return {MatterJS.Body[]} An array of bodies whos vertices intersect with target body.
834+
* @return {Phaser.Types.Physics.Matter.MatterBody[]} An array of bodies whos vertices intersect with target body.
823835
*/
824836
intersectBody: function (body, bodies)
825837
{
@@ -866,8 +878,8 @@ var MatterPhysics = new Class({
866878
* @method Phaser.Physics.Matter.MatterPhysics#overlap
867879
* @since 3.22.0
868880
*
869-
* @param {(MatterJS.Body|MatterJS.Body[])} target - The target body, or array of target bodies, to check.
870-
* @param {MatterJS.Body[]} [bodies] - The second body, or array of bodies, to check. If falsey it will check against all bodies in the world.
881+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} target - The target body, or array of target bodies, to check.
882+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} [bodies] - The second body, or array of bodies, to check. If falsey it will check against all bodies in the world.
871883
* @param {ArcadePhysicsCallback} [overlapCallback] - An optional callback function that is called if the bodies overlap.
872884
* @param {ArcadePhysicsCallback} [processCallback] - An optional callback function that lets you perform additional checks against the two bodies if they overlap. If this is set then `overlapCallback` will only be invoked if this callback returns `true`.
873885
* @param {*} [callbackContext] - The context, or scope, in which to run the callbacks.
@@ -932,7 +944,7 @@ var MatterPhysics = new Class({
932944
* @method Phaser.Physics.Matter.MatterPhysics#setCollisionCategory
933945
* @since 3.22.0
934946
*
935-
* @param {MatterJS.Body[]} bodies - An array of bodies to update. If falsey it will use all bodies in the world.
947+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} bodies - An array of bodies to update. If falsey it will use all bodies in the world.
936948
* @param {number} value - Unique category bitfield.
937949
*
938950
* @return {this} This Matter Physics instance.
@@ -961,7 +973,7 @@ var MatterPhysics = new Class({
961973
* @method Phaser.Physics.Matter.MatterPhysics#setCollisionGroup
962974
* @since 3.22.0
963975
*
964-
* @param {MatterJS.Body[]} bodies - An array of bodies to update. If falsey it will use all bodies in the world.
976+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} bodies - An array of bodies to update. If falsey it will use all bodies in the world.
965977
* @param {number} value - Unique group index.
966978
*
967979
* @return {this} This Matter Physics instance.
@@ -988,7 +1000,7 @@ var MatterPhysics = new Class({
9881000
* @method Phaser.Physics.Matter.MatterPhysics#setCollidesWith
9891001
* @since 3.22.0
9901002
*
991-
* @param {MatterJS.Body[]} bodies - An array of bodies to update. If falsey it will use all bodies in the world.
1003+
* @param {Phaser.Types.Physics.Matter.MatterBody[]} bodies - An array of bodies to update. If falsey it will use all bodies in the world.
9921004
* @param {(number|number[])} categories - A unique category bitfield, or an array of them.
9931005
*
9941006
* @return {this} This Matter Physics instance.
@@ -1064,7 +1076,7 @@ var MatterPhysics = new Class({
10641076
* @method Phaser.Physics.Matter.MatterPhysics#setVelocity
10651077
* @since 3.22.0
10661078
*
1067-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1079+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
10681080
* @param {number} x - The horizontal linear velocity value.
10691081
* @param {number} y - The vertical linear velocity value.
10701082
*
@@ -1094,7 +1106,7 @@ var MatterPhysics = new Class({
10941106
* @method Phaser.Physics.Matter.MatterPhysics#setVelocityX
10951107
* @since 3.22.0
10961108
*
1097-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1109+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
10981110
* @param {number} x - The horizontal linear velocity value.
10991111
*
11001112
* @return {this} This Matter Physics instance.
@@ -1123,7 +1135,7 @@ var MatterPhysics = new Class({
11231135
* @method Phaser.Physics.Matter.MatterPhysics#setVelocityY
11241136
* @since 3.22.0
11251137
*
1126-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1138+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
11271139
* @param {number} y - The vertical linear velocity value.
11281140
*
11291141
* @return {this} This Matter Physics instance.
@@ -1152,7 +1164,7 @@ var MatterPhysics = new Class({
11521164
* @method Phaser.Physics.Matter.MatterPhysics#setAngularVelocity
11531165
* @since 3.22.0
11541166
*
1155-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1167+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
11561168
* @param {number} value - The angular velocity.
11571169
*
11581170
* @return {this} This Matter Physics instance.
@@ -1175,7 +1187,7 @@ var MatterPhysics = new Class({
11751187
* @method Phaser.Physics.Matter.MatterPhysics#applyForce
11761188
* @since 3.22.0
11771189
*
1178-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1190+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
11791191
* @param {Phaser.Types.Math.Vector2Like} force - A Vector that specifies the force to apply.
11801192
*
11811193
* @return {this} This Matter Physics instance.
@@ -1206,7 +1218,7 @@ var MatterPhysics = new Class({
12061218
* @method Phaser.Physics.Matter.MatterPhysics#applyForceFromPosition
12071219
* @since 3.22.0
12081220
*
1209-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1221+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
12101222
* @param {Phaser.Types.Math.Vector2Like} position - A Vector that specifies the world-space position to apply the force at.
12111223
* @param {number} speed - A speed value to be applied to a directional force.
12121224
* @param {number} [angle] - The angle, in radians, to apply the force from. Leave undefined to use the current body angle.
@@ -1244,7 +1256,7 @@ var MatterPhysics = new Class({
12441256
* @method Phaser.Physics.Matter.MatterPhysics#applyForceFromAngle
12451257
* @since 3.22.0
12461258
*
1247-
* @param {(MatterJS.Body|MatterJS.Body[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
1259+
* @param {(Phaser.Types.Physics.Matter.MatterBody|Phaser.Types.Physics.Matter.MatterBody[])} bodies - Either a single Body, or an array of bodies to update. If falsey it will use all bodies in the world.
12481260
* @param {number} speed - A speed value to be applied to a directional force.
12491261
* @param {number} [angle] - The angle, in radians, to apply the force from. Leave undefined to use the current body angle.
12501262
*
@@ -1310,90 +1322,79 @@ var MatterPhysics = new Class({
13101322
* The alignment takes place using the body bounds, which take into consideration things
13111323
* like body scale and rotation.
13121324
*
1313-
* For example, if you wanted to align a body so it sat in the bottom-left of the
1325+
* Although a Body has a `position` property, it is based on the center of mass for the body,
1326+
* not a dimension based center. This makes aligning bodies difficult, especially if they have
1327+
* rotated or scaled. This method will derive the correct position based on the body bounds and
1328+
* its center of mass offset, in order to align the body with the given coordinate.
1329+
*
1330+
* For example, if you wanted to align a body so it sat in the bottom-center of the
13141331
* Scene, and the world was 800 x 600 in size:
13151332
*
13161333
* ```javascript
1317-
* this.matter.alignBody(body, 0, 600, Phaser.Display.Align.BOTTOM_LEFT);
1334+
* this.matter.alignBody(body, 400, 600, Phaser.Display.Align.BOTTOM_CENTER);
13181335
* ```
1336+
*
1337+
* You pass in 400 for the x coordinate, because that is the center of the world, and 600 for
1338+
* the y coordinate, as that is the base of the world.
13191339
*
13201340
* @method Phaser.Physics.Matter.MatterPhysics#alignBody
13211341
* @since 3.22.0
13221342
*
1323-
* @param {MatterJS.Body} body - The Body to align.
1343+
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to align.
13241344
* @param {number} x - The horizontal position to align the body to.
13251345
* @param {number} y - The vertical position to align the body to.
13261346
* @param {integer} align - One of the `Phaser.Display.Align` constants, such as `Phaser.Display.Align.TOP_LEFT`.
13271347
*
1328-
* @return {number} The length of the constraint.
1348+
* @return {this} This Matter Physics instance.
13291349
*/
13301350
alignBody: function (body, x, y, align)
13311351
{
1332-
body = (body.hasOwnProperty('body')) ? body.body : body;
1333-
1334-
var boundsWidth = body.bounds.max.x - body.bounds.min.x;
1335-
var boundsHeight = body.bounds.max.y - body.bounds.min.y;
1336-
1337-
var boundsCenterX = boundsWidth / 2;
1338-
var boundsCenterY = boundsHeight / 2;
1339-
1340-
var bodyCenterX = boundsWidth * body.centerOfMass.x;
1341-
var bodyCenterY = boundsHeight * body.centerOfMass.y;
1342-
1343-
var diffX = bodyCenterX - boundsCenterX;
1344-
var diffY = bodyCenterY - boundsCenterY;
1345-
1346-
var posX;
1347-
var posY;
1352+
var pos;
13481353

13491354
switch (align)
13501355
{
13511356
case ALIGN_CONST.TOP_LEFT:
13521357
case ALIGN_CONST.LEFT_TOP:
1353-
posX = x + boundsCenterX + diffX;
1354-
posY = y + boundsCenterY + diffY;
1358+
pos = this.bodyBounds.getTopLeft(body, x, y);
13551359
break;
13561360

13571361
case ALIGN_CONST.TOP_CENTER:
1358-
posX = x + diffX;
1359-
posY = y + boundsCenterY + diffY;
1362+
pos = this.bodyBounds.getTopCenter(body, x, y);
13601363
break;
13611364

13621365
case ALIGN_CONST.TOP_RIGHT:
13631366
case ALIGN_CONST.RIGHT_TOP:
1364-
posX = x - (boundsCenterX - diffX);
1365-
posY = y + boundsCenterY + diffY;
1367+
pos = this.bodyBounds.getTopRight(body, x, y);
13661368
break;
13671369

13681370
case ALIGN_CONST.LEFT_CENTER:
1369-
posX = x + boundsCenterX + diffX;
1370-
posY = y + diffY;
1371+
pos = this.bodyBounds.getLeftCenter(body, x, y);
1372+
break;
1373+
1374+
case ALIGN_CONST.CENTER:
1375+
pos = this.bodyBounds.getCenter(body, x, y);
1376+
break;
1377+
1378+
case ALIGN_CONST.RIGHT_CENTER:
1379+
pos = this.bodyBounds.getRightCenter(body, x, y);
13711380
break;
13721381

13731382
case ALIGN_CONST.LEFT_BOTTOM:
13741383
case ALIGN_CONST.BOTTOM_LEFT:
1375-
posX = x + boundsCenterX + diffX;
1376-
posY = y - (boundsCenterY - diffY);
1384+
pos = this.bodyBounds.getBottomLeft(body, x, y);
13771385
break;
13781386

13791387
case ALIGN_CONST.BOTTOM_CENTER:
1380-
posX = x + diffX;
1381-
posY = y - (boundsCenterY - diffY);
1388+
pos = this.bodyBounds.getBottomCenter(body, x, y);
13821389
break;
13831390

13841391
case ALIGN_CONST.BOTTOM_RIGHT:
13851392
case ALIGN_CONST.RIGHT_BOTTOM:
1386-
posX = x - (boundsCenterX - diffX);
1387-
posY = y - (boundsCenterY - diffY);
1388-
break;
1389-
1390-
case ALIGN_CONST.RIGHT_CENTER:
1391-
posX = x - (boundsCenterX - diffX);
1392-
posY = y + diffY;
1393+
pos = this.bodyBounds.getBottomRight(body, x, y);
13931394
break;
13941395
}
13951396

1396-
Body.setPosition(body, { x: posX, y: posY });
1397+
Body.setPosition(body, pos);
13971398

13981399
return this;
13991400
},

0 commit comments

Comments
 (0)