2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-06-03 20:17:33 +00:00
|
|
|
var CONST = require('./const');
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-10-19 16:45:05 +00:00
|
|
|
* Calculates and returns the horizontal overlap between two arcade physics bodies and sets their properties
|
|
|
|
* accordingly, including: `touching.left`, `touching.right`, `touching.none` and `overlapX'.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Physics.Arcade.GetOverlapX
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 11:32:43 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
|
|
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
|
|
|
|
* @param {boolean} overlapOnly - Is this an overlap only check, or part of separation?
|
|
|
|
* @param {number} bias - A value added to the delta values during collision checks. Increase it to prevent sprite tunneling(sprites passing through another instead of colliding).
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-10-19 16:45:05 +00:00
|
|
|
* @return {number} The amount of overlap.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2017-11-09 13:02:55 +00:00
|
|
|
var GetOverlapX = function (body1, body2, overlapOnly, bias)
|
|
|
|
{
|
|
|
|
var overlap = 0;
|
|
|
|
var maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + bias;
|
|
|
|
|
2018-06-01 14:41:40 +00:00
|
|
|
if (body1._dx === 0 && body2._dx === 0)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
|
|
|
// They overlap but neither of them are moving
|
|
|
|
body1.embedded = true;
|
|
|
|
body2.embedded = true;
|
|
|
|
}
|
2018-06-01 14:41:40 +00:00
|
|
|
else if (body1._dx > body2._dx)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
|
|
|
// Body1 is moving right and / or Body2 is moving left
|
|
|
|
overlap = body1.right - body2.x;
|
|
|
|
|
|
|
|
if ((overlap > maxOverlap && !overlapOnly) || body1.checkCollision.right === false || body2.checkCollision.left === false)
|
|
|
|
{
|
|
|
|
overlap = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.touching.none = false;
|
|
|
|
body1.touching.right = true;
|
2018-06-03 20:17:33 +00:00
|
|
|
|
2017-11-09 13:02:55 +00:00
|
|
|
body2.touching.none = false;
|
|
|
|
body2.touching.left = true;
|
2018-06-03 20:17:33 +00:00
|
|
|
|
|
|
|
if (body2.physicsType === CONST.STATIC_BODY)
|
|
|
|
{
|
2018-06-19 21:48:07 +00:00
|
|
|
body1.blocked.none = false;
|
2018-06-03 20:17:33 +00:00
|
|
|
body1.blocked.right = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body1.physicsType === CONST.STATIC_BODY)
|
|
|
|
{
|
2018-06-19 21:48:07 +00:00
|
|
|
body2.blocked.none = false;
|
2018-06-03 20:17:33 +00:00
|
|
|
body2.blocked.left = true;
|
|
|
|
}
|
2017-11-09 13:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-01 14:41:40 +00:00
|
|
|
else if (body1._dx < body2._dx)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
|
|
|
// Body1 is moving left and/or Body2 is moving right
|
|
|
|
overlap = body1.x - body2.width - body2.x;
|
|
|
|
|
|
|
|
if ((-overlap > maxOverlap && !overlapOnly) || body1.checkCollision.left === false || body2.checkCollision.right === false)
|
|
|
|
{
|
|
|
|
overlap = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.touching.none = false;
|
|
|
|
body1.touching.left = true;
|
2018-06-03 20:17:33 +00:00
|
|
|
|
2017-11-09 13:02:55 +00:00
|
|
|
body2.touching.none = false;
|
|
|
|
body2.touching.right = true;
|
2018-06-03 20:17:33 +00:00
|
|
|
|
|
|
|
if (body2.physicsType === CONST.STATIC_BODY)
|
|
|
|
{
|
2018-06-19 21:48:07 +00:00
|
|
|
body1.blocked.none = false;
|
2018-06-03 20:17:33 +00:00
|
|
|
body1.blocked.left = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body1.physicsType === CONST.STATIC_BODY)
|
|
|
|
{
|
2018-06-19 21:48:07 +00:00
|
|
|
body2.blocked.none = false;
|
2018-06-03 20:17:33 +00:00
|
|
|
body2.blocked.right = true;
|
|
|
|
}
|
2017-11-09 13:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resets the overlapX to zero if there is no overlap, or to the actual pixel value if there is
|
|
|
|
body1.overlapX = overlap;
|
|
|
|
body2.overlapX = overlap;
|
|
|
|
|
|
|
|
return overlap;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetOverlapX;
|