Now returns area of intersection in a temp rectangle

This commit is contained in:
Richard Davey 2019-04-01 14:58:51 +01:00
parent 3e5358dded
commit 4ca877b32e

View file

@ -5,24 +5,27 @@
*/
/**
* Checks if two rectangle bodies are intersecting, either with or without the optional padding.
* Checks if two rectangle bodies are intersecting, either with or without padding.
*
* @function Phaser.Physics.Arcade.IntersectsRect
* @since 3.17.0
*
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
* @param {number} [padding] -
* @param {number} [padding=0] - Amount of padding to add to the body border. Should be zero for intersection tests and 1 for touching tests.
* @param {Phaser.Geom.Rectangle} [out] - Rectangle to store the area of intersection within.
*
* @return {boolean}
*/
var IntersectsRect = function (body1, body2, padding)
var IntersectsRect = function (body1, body2, padding, out)
{
if (padding === undefined) { padding = 0; }
var intersects = false;
if (padding === 0)
{
return !(
intersects = !(
body1.right <= body2.x ||
body1.bottom <= body2.y ||
body1.x >= body2.right ||
@ -42,13 +45,30 @@ var IntersectsRect = function (body1, body2, padding)
var b2x = body2.x - padding;
var b2y = body2.y - padding;
return !(
intersects = !(
b1r <= b2x ||
b1b <= b2y ||
b1x >= b2r ||
b1y >= b2b
);
}
if (out)
{
if (intersects)
{
out.x = Math.max(body1.x, body2.x);
out.y = Math.max(body1.y, body2.y);
out.width = Math.min(body1.right, body2.right) - out.x;
out.height = Math.min(body1.bottom, body2.bottom) - out.y;
}
else
{
out.setEmpty();
}
}
return intersects;
};
module.exports = IntersectsRect;