mirror of
https://github.com/photonstorm/phaser
synced 2025-02-16 22:18:29 +00:00
New BodyBounds class for getting body bounds based coordinates
This commit is contained in:
parent
fceef3ddf0
commit
a8f3c3b9c0
1 changed files with 381 additions and 0 deletions
381
src/physics/matter-js/BodyBounds.js
Normal file
381
src/physics/matter-js/BodyBounds.js
Normal file
|
@ -0,0 +1,381 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2019 Photon Storm Ltd.
|
||||
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
||||
*/
|
||||
|
||||
var Class = require('../../utils/Class');
|
||||
var Vec2 = require('../../math/Vector2');
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
*
|
||||
* The Body Bounds class contains methods to help you extract the world coordinates from various points around
|
||||
* the bounds of a Matter Body. Because Matter bodies are positioned based on their center of mass, and not a
|
||||
* dimension based center, you often need to get the bounds coordinates in order to properly align them in the world.
|
||||
*
|
||||
* You can access this class via the MatterPhysics class from a Scene, i.e.:
|
||||
*
|
||||
* ```javascript
|
||||
* this.matter.bodyBounds.getTopLeft(body);
|
||||
* ```
|
||||
*
|
||||
* See also the `MatterPhysics.alignBody` method.
|
||||
*
|
||||
* @class BodyBounds
|
||||
* @memberof Phaser.Physics.Matter
|
||||
* @constructor
|
||||
* @since 3.22.0
|
||||
*/
|
||||
var BodyBounds = new Class({
|
||||
|
||||
initialize:
|
||||
|
||||
function BodyBounds ()
|
||||
{
|
||||
this.boundsCenter = new Vec2();
|
||||
this.centerDiff = new Vec2();
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the length of the given constraint, which is the distance between the two points.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#parseBody
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the bounds position from.
|
||||
*
|
||||
* @return {boolean} `true` if it was able to get the bounds, otherwise `false`.
|
||||
*/
|
||||
parseBody: function (body)
|
||||
{
|
||||
body = (body.hasOwnProperty('body')) ? body.body : body;
|
||||
|
||||
if (!body.hasOwnProperty('bounds') || !body.hasOwnProperty('centerOfMass'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var boundsCenter = this.boundsCenter;
|
||||
var centerDiff = this.centerDiff;
|
||||
|
||||
var boundsWidth = body.bounds.max.x - body.bounds.min.x;
|
||||
var boundsHeight = body.bounds.max.y - body.bounds.min.y;
|
||||
|
||||
var bodyCenterX = boundsWidth * body.centerOfMass.x;
|
||||
var bodyCenterY = boundsHeight * body.centerOfMass.y;
|
||||
|
||||
boundsCenter.set(boundsWidth / 2, boundsHeight / 2);
|
||||
centerDiff.set(bodyCenterX - boundsCenter.x, bodyCenterY - boundsCenter.y);
|
||||
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the top-left of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getTopLeft
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getTopLeft: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x + center.x + diff.x,
|
||||
y + center.y + diff.y
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the top-center of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getTopCenter
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getTopCenter: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x + diff.x,
|
||||
y + center.y + diff.y
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the top-right of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getTopRight
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getTopRight: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x - (center.x - diff.x),
|
||||
y + center.y + diff.y
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the left-center of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getLeftCenter
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getLeftCenter: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x + center.x + diff.x,
|
||||
y + diff.y
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the center of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getCenter
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getCenter: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x + diff.x,
|
||||
y + diff.y
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the right-center of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getRightCenter
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getRightCenter: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x - (center.x - diff.x),
|
||||
y + diff.y
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the bottom-left of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getBottomLeft
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getBottomLeft: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x + center.x + diff.x,
|
||||
y - (center.y - diff.y)
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the bottom-center of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getBottomCenter
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getBottomCenter: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x + diff.x,
|
||||
y - (center.y - diff.y)
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes a Body and returns the world coordinates of the bottom-right of its _bounds_.
|
||||
*
|
||||
* Body bounds are updated by Matter each step and factor in scale and rotation.
|
||||
* This will return the world coordinate based on the bodies _current_ position and bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Matter.BodyBounds#getBottomRight
|
||||
* @since 3.22.0
|
||||
*
|
||||
* @param {Phaser.Types.Physics.Matter.MatterBody} body - The Body to get the position from.
|
||||
* @param {number} [x=0] - Optional horizontal offset to add to the returned coordinates.
|
||||
* @param {number} [y=0] - Optional vertical offset to add to the returned coordinates.
|
||||
*
|
||||
* @return {(Phaser.Math.Vector2|false)} A Vector2 containing the coordinates, or `false` if it was unable to parse the body.
|
||||
*/
|
||||
getBottomRight: function (body, x, y)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
|
||||
if (this.parseBody(body))
|
||||
{
|
||||
var center = this.boundsCenter;
|
||||
var diff = this.centerDiff;
|
||||
|
||||
return new Vec2(
|
||||
x - (center.x - diff.x),
|
||||
y - (center.y - diff.y)
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
module.exports = BodyBounds;
|
Loading…
Add table
Reference in a new issue