2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2023-01-02 17:36:27 +00:00
|
|
|
* @copyright 2013-2023 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-03 16:28:06 +00:00
|
|
|
var GetOverlapX = require('./GetOverlapX');
|
2020-09-25 17:01:40 +00:00
|
|
|
var ProcessX = require('./ProcessX');
|
2017-11-09 13:02:55 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-10-19 16:45:05 +00:00
|
|
|
* Separates two overlapping bodies on the X-axis (horizontally).
|
|
|
|
*
|
2019-05-03 16:28:06 +00:00
|
|
|
* Separation involves moving two overlapping bodies so they don't overlap anymore and adjusting their velocities based on their mass. This is a core part of collision detection.
|
2018-10-19 16:45:05 +00:00
|
|
|
*
|
2019-05-03 16:28:06 +00:00
|
|
|
* The bodies won't be separated if there is no horizontal overlap between them, if they are static, or if either one uses custom logic for its separation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Physics.Arcade.SeparateX
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-05-03 16:28:06 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
|
2018-10-19 16:45:05 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
|
|
|
|
* @param {boolean} overlapOnly - If `true`, the bodies will only have their overlap data set and no separation will take place.
|
|
|
|
* @param {number} bias - A value to add to the delta value during overlap checking. Used to prevent sprite tunneling.
|
2022-12-13 01:56:23 +00:00
|
|
|
* @param {number} [overlap] - If given then this value will be used as the overlap and no check will be run.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2020-09-25 17:01:40 +00:00
|
|
|
* @return {boolean} `true` if the two bodies overlap vertically, otherwise `false`.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2022-12-13 01:56:23 +00:00
|
|
|
var SeparateX = function (body1, body2, overlapOnly, bias, overlap)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
2022-12-13 01:56:23 +00:00
|
|
|
if (overlap === undefined) { overlap = GetOverlapX(body1, body2, overlapOnly, bias); }
|
2017-11-09 13:02:55 +00:00
|
|
|
|
2020-09-24 17:10:11 +00:00
|
|
|
var body1Immovable = body1.immovable;
|
|
|
|
var body2Immovable = body2.immovable;
|
|
|
|
|
2017-11-09 13:02:55 +00:00
|
|
|
// Can't separate two immovable bodies, or a body with its own custom separation logic
|
2020-09-24 17:10:11 +00:00
|
|
|
if (overlapOnly || overlap === 0 || (body1Immovable && body2Immovable) || body1.customSeparateX || body2.customSeparateX)
|
2019-03-24 23:30:57 +00:00
|
|
|
{
|
2019-05-03 16:28:06 +00:00
|
|
|
// return true if there was some overlap, otherwise false
|
|
|
|
return (overlap !== 0) || (body1.embedded && body2.embedded);
|
2019-03-24 23:30:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 12:07:32 +00:00
|
|
|
var blockedState = ProcessX.Set(body1, body2, overlap);
|
2020-09-25 17:01:40 +00:00
|
|
|
|
2020-09-24 17:10:11 +00:00
|
|
|
if (!body1Immovable && !body2Immovable)
|
2019-03-24 23:30:57 +00:00
|
|
|
{
|
2020-09-25 17:01:40 +00:00
|
|
|
if (blockedState > 0)
|
2020-09-24 17:10:11 +00:00
|
|
|
{
|
2020-09-25 17:01:40 +00:00
|
|
|
return true;
|
2020-09-24 17:10:11 +00:00
|
|
|
}
|
2020-09-25 17:01:40 +00:00
|
|
|
|
2020-09-28 14:25:43 +00:00
|
|
|
return ProcessX.Check();
|
2019-03-24 23:30:57 +00:00
|
|
|
}
|
2020-09-25 17:01:40 +00:00
|
|
|
else if (body1Immovable)
|
2019-03-24 23:30:57 +00:00
|
|
|
{
|
2020-09-28 12:07:32 +00:00
|
|
|
ProcessX.RunImmovableBody1(blockedState);
|
2020-09-24 17:10:11 +00:00
|
|
|
}
|
2020-09-25 17:01:40 +00:00
|
|
|
else if (body2Immovable)
|
2020-09-24 17:10:11 +00:00
|
|
|
{
|
2020-09-28 12:07:32 +00:00
|
|
|
ProcessX.RunImmovableBody2(blockedState);
|
2017-11-09 13:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far then there WAS overlap, and separation is complete, so return true
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SeparateX;
|