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}
|
|
|
|
*/
|
|
|
|
|
2019-03-11 17:28:23 +00:00
|
|
|
var CONST = require('./const');
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2019-03-08 20:11:27 +00:00
|
|
|
* Calculates and returns the vertical overlap between two arcade physics bodies.
|
2019-03-11 09:19:41 +00:00
|
|
|
*
|
|
|
|
* We know the bodies are intersecting based on a previous check, so the point of this function
|
|
|
|
* is to determine which face the overlap is occurring on and at what depth.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Physics.Arcade.GetOverlapY
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 16:45:05 +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?
|
2019-03-08 20:11:27 +00:00
|
|
|
* @param {number} bias - A value added to the delta values during collision checks. Increase it to prevent sprite tunneling (sprites passing through each other instead of colliding).
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2019-03-11 09:19:41 +00:00
|
|
|
* @return {number[]} An array containing the amount of overlap in element 0 and the face of body1 in element 1 (true = bottom, false = top).
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2017-11-09 15:30:44 +00:00
|
|
|
var GetOverlapY = function (body1, body2, overlapOnly, bias)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
|
|
|
var overlap = 0;
|
2017-11-09 15:30:44 +00:00
|
|
|
var maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + bias;
|
2017-11-09 13:02:55 +00:00
|
|
|
|
2019-03-11 17:28:23 +00:00
|
|
|
var body1Immovable = (body1.physicsType === CONST.STATIC_BODY || body1.immovable);
|
|
|
|
var body2Immovable = (body2.physicsType === CONST.STATIC_BODY || body2.immovable);
|
|
|
|
|
2019-03-11 09:19:41 +00:00
|
|
|
var distance1 = body1.bottom - body2.y;
|
|
|
|
var distance2 = body2.bottom - body1.y;
|
2019-03-13 02:27:11 +00:00
|
|
|
var prevDistance1 = (body1.prev.y + body1.height) - body2.y;
|
|
|
|
var prevDistance2 = body2.bottom - body2.prev.y;
|
2019-03-11 17:28:23 +00:00
|
|
|
|
|
|
|
var blocked1 = body1.blocked;
|
|
|
|
var blocked2 = body2.blocked;
|
|
|
|
|
2019-03-13 02:27:11 +00:00
|
|
|
var topFace = (distance1 > distance2 && prevDistance1 > prevDistance2);
|
2019-03-08 20:11:27 +00:00
|
|
|
|
2019-03-13 02:27:11 +00:00
|
|
|
if (!topFace)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
2019-03-13 02:27:11 +00:00
|
|
|
// body1 bottom is touching body2 top
|
2019-03-11 09:19:41 +00:00
|
|
|
overlap = distance1;
|
2019-03-08 20:11:27 +00:00
|
|
|
|
2019-03-11 09:19:41 +00:00
|
|
|
if ((overlap > maxOverlap && !overlapOnly) || !body1.checkCollision.down || !body2.checkCollision.up)
|
|
|
|
{
|
|
|
|
overlap = 0;
|
|
|
|
}
|
2019-03-11 17:28:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.setTouchingDown();
|
|
|
|
body2.setTouchingUp();
|
|
|
|
|
|
|
|
if (blocked2.down || body2Immovable)
|
|
|
|
{
|
|
|
|
body1.setBlockedDown(body2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blocked1.up || body1Immovable)
|
|
|
|
{
|
|
|
|
body2.setBlockedUp(body1);
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 09:19:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-13 02:27:11 +00:00
|
|
|
// body1 top is touching body2 bottom
|
2019-03-11 09:19:41 +00:00
|
|
|
overlap = distance2;
|
|
|
|
|
|
|
|
if ((overlap > maxOverlap && !overlapOnly) || !body1.checkCollision.up || !body2.checkCollision.down)
|
|
|
|
{
|
|
|
|
overlap = 0;
|
|
|
|
}
|
2019-03-11 17:28:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.setTouchingUp();
|
|
|
|
body2.setTouchingDown();
|
|
|
|
|
|
|
|
if (blocked2.up || body2Immovable)
|
|
|
|
{
|
|
|
|
body1.setBlockedUp(body2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blocked1.down || body1Immovable)
|
|
|
|
{
|
|
|
|
body2.setBlockedDown(body1);
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 09:19:41 +00:00
|
|
|
}
|
2019-03-11 17:28:23 +00:00
|
|
|
|
2019-03-11 12:26:23 +00:00
|
|
|
if (overlap > maxOverlap && !overlapOnly)
|
2019-03-11 09:19:41 +00:00
|
|
|
{
|
2019-03-11 12:26:23 +00:00
|
|
|
body1.embedded = true;
|
|
|
|
body2.embedded = true;
|
2017-11-09 13:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resets the overlapY to zero if there is no overlap, or to the actual pixel value if there is
|
|
|
|
body1.overlapY = overlap;
|
|
|
|
body2.overlapY = overlap;
|
|
|
|
|
2019-03-11 12:26:23 +00:00
|
|
|
return [ overlap, topFace ];
|
2017-11-09 13:02:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetOverlapY;
|