phaser/src/physics/arcade/SeparateX.js

444 lines
15 KiB
JavaScript
Raw Normal View History

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-24 23:30:57 +00:00
var CONST = require('./const');
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-03-24 23:30:57 +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-03-24 23:30:57 +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-03-24 23:30:57 +00:00
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate. This is our priority body.
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.
2018-02-09 03:44:23 +00:00
*
2018-10-19 16:45:05 +00:00
* @return {boolean} `true` if the two bodies overlap horizontally, otherwise `false`.
2018-02-09 03:44:23 +00:00
*/
2019-03-25 12:55:41 +00:00
var SeparateX = function (collisionInfo)
{
2019-03-25 12:55:41 +00:00
var overlap = collisionInfo.overlapX;
var overlapOnly = collisionInfo.overlapOnly;
2019-03-24 23:30:57 +00:00
2019-03-25 12:55:41 +00:00
var leftFace = collisionInfo.faceX === CONST.FACING_LEFT;
2019-03-24 23:30:57 +00:00
var rightFace = !leftFace;
2019-03-25 12:55:41 +00:00
2019-03-24 23:30:57 +00:00
var intersects = collisionInfo.intersects;
2019-03-25 12:55:41 +00:00
var body1 = collisionInfo.body1;
var body2 = collisionInfo.body2;
2019-03-24 23:30:57 +00:00
var velocity1 = body1.velocity;
var velocity2 = body2.velocity;
var bounce1 = body1.bounce;
var bounce2 = body2.bounce;
var body1Immovable = (body1.physicsType === CONST.STATIC_BODY || body1.immovable);
var body2Immovable = (body2.physicsType === CONST.STATIC_BODY || body2.immovable);
2019-03-25 17:25:11 +00:00
// console.log('');
// console.log('%c X frame ' + body1.world._frame + ' ', 'background-color: orange');
// console.log('body1:', body1.gameObject.name, 'vs body2:', body2.gameObject.name);
// console.log('pre-GetOverlap bx = body1', body1.x, 'body2', body2.x);
console.log('x compare (PRE-SX):', body1.right, 'body2', body2.x, '=', (body1.right - body2.x));
2019-03-25 12:55:41 +00:00
// console.log('pre-GetOverlap by = body1', body1.y, 'body2', body2.y);
// console.log('pre-GetOverlap gy = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
// console.log(collisionInfo);
// console.log('post-GetOverlap by = body1', body1.y, 'body2', body2.y);
// console.log('post-GetOverlap gy = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
2019-03-25 17:25:11 +00:00
// console.log('body1 overlaps body2 across the', ((leftFace) ? 'left' : 'right'), 'by', overlap, 'px');
// Can't separate two immovable bodies, or a body with its own custom separation logic
2019-03-24 23:30:57 +00:00
if (!intersects || overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateX || body2.customSeparateX)
{
2019-03-24 23:30:57 +00:00
console.log(this.world._frame, 'SeparateX aborted');
// return true if there was some overlap, otherwise false.
return ((intersects && overlap !== 0) || (body1.embedded && body2.embedded));
}
2019-03-24 23:30:57 +00:00
// Adjust their positions and velocities accordingly based on the amount of overlap
var x1 = velocity1.x;
var x2 = velocity2.x;
var nx1 = x1;
var nx2 = x2;
// At this point, the velocity from gravity, world rebounds, etc has been factored in.
// The body is moving the direction it wants to, but may be blocked and rebound.
2019-03-24 23:30:57 +00:00
var moving1 = body1.movingX();
var moving2 = body2.movingX();
if (moving1 && moving2)
{
2019-03-24 23:30:57 +00:00
// Neither body is immovable, so they get a new velocity based on mass
var mass1 = body1.mass;
var mass2 = body2.mass;
2019-03-24 23:30:57 +00:00
var bnx1;
var bnx2;
2019-03-24 23:30:57 +00:00
// We don't need costly sqrts if both masses are the same
if (mass1 === mass2)
{
bnx1 = Math.abs(x2) * ((x2 > 0) ? 1 : -1);
bnx2 = Math.abs(x1) * ((x1 > 0) ? 1 : -1);
}
else
{
bnx1 = Math.sqrt((x2 * x2 * mass2) / mass1) * ((x2 > 0) ? 1 : -1);
bnx2 = Math.sqrt((x1 * x1 * mass1) / mass2) * ((x1 > 0) ? 1 : -1);
}
var avg = (bnx1 + bnx2) * 0.5;
var nv1 = bnx1 - avg;
var nv2 = bnx2 - avg;
nx1 = avg + nv1 * bounce1.x;
nx2 = avg + nv2 * bounce2.x;
2019-03-25 17:25:11 +00:00
// console.log('resolution 1');
// console.log('pre-impact v = body1', x1, 'body2', x2);
// console.log('post-impact v = body1', nx1, 'body2', nx2);
// console.log('pre-impact x = body1', body1.gameObject.x, 'body2', body2.gameObject.x);
2019-03-25 12:55:41 +00:00
// console.log('wb = body1', body1.worldBlocked.right, 'body2', body2.worldBlocked.left);
2019-03-24 23:30:57 +00:00
// console.log('avg', avg);
// console.log('nv', nv1, nv2);
// console.log('sqrt mult', bnx1, bnx2);
// console.log('delta', body1.deltaY(), body2.deltaY());
}
else if (!moving1 && moving2)
{
// Body1 is immovable, so adjust body2 speed
if (body1.rideable)
{
nx2 = (leftFace) ? 1 : -1;
}
else
{
nx2 = x1 - x2 * bounce2.x;
}
2019-03-25 12:55:41 +00:00
// console.log('%cresolution 2', 'background-color: red; color: white');
2019-03-24 23:30:57 +00:00
// console.log('pre-impact v = body1', x1, 'body2', x2);
// console.log('post-impact v = body1', nx1, 'body2', nx2);
// console.log('pre-impact by = body1', body1.y, 'body2', body2.y);
// console.log('pre-impact gy = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
// console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
// console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
}
2019-03-24 23:30:57 +00:00
else if (moving1 && !moving2)
{
2019-03-24 23:30:57 +00:00
// Body2 is immovable, so adjust body1 speed
2019-03-24 23:30:57 +00:00
if (body2.rideable)
{
nx1 = (leftFace) ? 1 : -1;
}
else
{
2019-03-24 23:30:57 +00:00
nx1 = x2 - x1 * bounce1.x;
}
2019-03-24 23:30:57 +00:00
// console.log('resolution 4');
// console.log('pre-impact v = body1', x1, 'body2', x2);
// console.log('post-impact v = body1', nx1, 'body2', nx2);
// console.log('pre-impact y = body1', body1.gameObject.y, 'body2', body2.gameObject.y);
// console.log('wb = body1', body1.worldBlocked.down, 'body2', body2.worldBlocked.down);
// console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
}
else
{
2019-03-24 23:30:57 +00:00
// console.log('neither moving, or both immovable');
// console.log('resolution 5');
// console.log('pre-impact v = body1', x1, 'body2', x2);
// console.log('post-impact v = body1', nx1, 'body2', nx2);
// console.log('sleeping? = body1', body1.sleeping, 'body2', body2.sleeping);
nx1 = 0;
nx2 = 0;
}
2019-03-25 12:55:41 +00:00
var totalA = collisionInfo.shareX1;
var totalB = collisionInfo.shareX2;
2019-03-24 23:30:57 +00:00
2019-03-25 17:25:11 +00:00
// console.log('split at', totalA, totalB, 'of', overlap);
2019-03-24 23:30:57 +00:00
if (totalA === 0 && totalB === 0 && overlap !== 0)
{
velocity1.x = 0;
velocity2.x = 0;
return true;
}
// Flip flop?
var flip1 = (x1 < 0 && nx1 > 0) || (x1 > 0 && nx1 < 0);
var flip2 = (x2 < 0 && nx2 > 0) || (x2 > 0 && nx2 < 0);
if (!body1.sleeping && flip1 && Math.abs(nx1) < body1.minVelocity.x)
{
// console.log('body1 flip vel too small, zeroing');
nx1 = 0;
}
if (!body2.sleeping && flip2 && Math.abs(nx2) < body2.minVelocity.x)
{
// console.log('body2 flip vel too small, zeroing');
nx2 = 0;
}
// By this stage the bodies have their separation distance calculated (stored in totalA/B)
// and they have their new post-impact velocity. So now we need to work out block state based on direction.
if (nx1 !== 0)
{
2019-03-25 17:25:11 +00:00
// nx1 < 0 = Body1 is now moving LEFT
// nx1 > 0 = Body1 is now moving RIGHT
2019-03-24 23:30:57 +00:00
if (leftFace)
{
2019-03-25 12:55:41 +00:00
// The left of Body1 overlaps with the right of Body2
2019-03-24 23:30:57 +00:00
if (body2.isBlockedLeft())
{
2019-03-25 17:25:11 +00:00
console.log('nx1 <> 0 left block left', body1.x);
2019-03-24 23:30:57 +00:00
body1.setBlockedLeft(collisionInfo, body2);
}
else
{
2019-03-25 17:25:11 +00:00
console.log('nx1 <> 0 left add', body1.x);
2019-03-25 12:55:41 +00:00
body1.x += body1.getMoveX(totalA);
2019-03-24 23:30:57 +00:00
}
}
else if (rightFace)
{
2019-03-25 12:55:41 +00:00
// The right of Body1 overlaps with the left of Body2
2019-03-24 23:30:57 +00:00
if (body2.isBlockedRight())
{
2019-03-25 17:25:11 +00:00
console.log('nx1 <> 0 right block right', body1.x);
2019-03-24 23:30:57 +00:00
body1.setBlockedRight(collisionInfo, body2);
}
else
{
2019-03-25 17:25:11 +00:00
console.log('nx1 <> 0 right add', body1.x);
2019-03-25 12:55:41 +00:00
body1.x += body1.getMoveX(totalA);
2019-03-24 23:30:57 +00:00
}
}
// If Body1 cannot move up, it doesn't matter what new velocity it has.
if (body1.sleeping && (nx1 < 0 && body1.isBlockedLeft()) || (nx1 > 0 && body1.isBlockedRight()))
{
2019-03-25 12:55:41 +00:00
console.log('nx1 <> 0 zero sleep');
2019-03-24 23:30:57 +00:00
nx1 = 0;
}
}
else if (body1.moves)
{
2019-03-25 17:25:11 +00:00
console.log('body1 stationary', body1.right);
// Body1 is now stationary
2019-03-24 23:30:57 +00:00
if (leftFace)
{
2019-03-25 17:25:11 +00:00
// console.log('The left of Body1 overlaps with the right of Body2');
// console.log(totalA, body1.isBlockedLeft(), body1.isBlockedRight());
// console.log(totalB, body2.isBlockedLeft(), body2.isBlockedRight());
// The left of Body1 overlaps with the right of Body2
// if (totalA !== 0 && !body1.isBlockedRight())
// {
// console.log('body1 stationary leftface add', body1.x, 'totalA', totalA);
2019-03-25 12:55:41 +00:00
body1.x += body1.getMoveX(totalA);
2019-03-25 17:25:11 +00:00
// }
2019-03-24 23:30:57 +00:00
if (body2.isBlockedLeft())
{
2019-03-25 17:25:11 +00:00
// console.log('body1 stationary leftface left', body1.x);
2019-03-24 23:30:57 +00:00
body1.setBlockedLeft(collisionInfo, body2);
}
}
else if (rightFace)
{
2019-03-25 17:25:11 +00:00
// console.log('The right of Body1 overlaps with the left of Body2');
// console.log(totalA, body1.isBlockedLeft(), body1.isBlockedRight());
// console.log(totalB, body2.isBlockedLeft(), body2.isBlockedRight());
// The right of Body1 overlaps with the left of Body2
// if (totalA !== 0 && !body1.isBlockedLeft())
// {
// console.log('body1 stationary rightface add', body1.x);
2019-03-25 12:55:41 +00:00
body1.x += body1.getMoveX(totalA);
2019-03-25 17:25:11 +00:00
// }
2019-03-24 23:30:57 +00:00
if (body2.isBlockedRight())
{
2019-03-25 17:25:11 +00:00
// console.log('body1 stationary rightface down', body1.x);
2019-03-24 23:30:57 +00:00
body1.setBlockedRight(collisionInfo, body2);
}
}
}
if (nx2 !== 0)
{
// nx2 < 0 = Body2 is moving LEFT
// nx2 > 0 = Body2 is moving RIGHT
if (leftFace)
{
// The bottom of Body2 overlaps with the top of Body1
if (body1.isBlockedRight())
{
2019-03-25 12:55:41 +00:00
console.log('nx2 <> 0 leftface down', body2.x);
2019-03-24 23:30:57 +00:00
body2.setBlockedRight(collisionInfo, body1);
}
else
{
2019-03-25 12:55:41 +00:00
console.log('nx2 <> 0 leftface add', body2.x);
body2.x += body2.getMoveX(totalB);
2019-03-24 23:30:57 +00:00
}
}
else if (rightFace)
{
// The top of Body2 overlaps with the bottom of Body1
if (body1.isBlockedLeft())
{
2019-03-25 12:55:41 +00:00
console.log('nx2 <> 0 rightface down', body2.x);
2019-03-24 23:30:57 +00:00
body2.setBlockedLeft(collisionInfo, body1);
}
else
{
2019-03-25 12:55:41 +00:00
console.log('nx2 <> 0 rightface add', body2.x);
body2.x += body2.getMoveX(totalB);
2019-03-24 23:30:57 +00:00
}
}
// If Body2 cannot move up or down, it doesn't matter what new velocity it has
if (body2.sleeping && (nx2 < 0 && body2.isBlockedLeft()) || (nx2 > 0 && body2.isBlockedRight()))
{
2019-03-25 12:55:41 +00:00
console.log('nx2 <> 0 zero sleep');
2019-03-24 23:30:57 +00:00
nx2 = 0;
}
}
else if (body2.moves)
{
2019-03-25 17:25:11 +00:00
console.log('body2 stationary', body2.x);
2019-03-24 23:30:57 +00:00
// Body2 is stationary
if (leftFace)
{
2019-03-25 17:25:11 +00:00
// console.log('The left of Body1 overlaps with the right of Body2');
// console.log(totalA, body1.isBlockedLeft(), body1.isBlockedRight());
// console.log(totalB, body2.isBlockedLeft(), body2.isBlockedRight());
2019-03-24 23:30:57 +00:00
// The bottom of Body2 overlaps with the top of Body1
2019-03-25 17:25:11 +00:00
// if (totalB !== 0 && !body2.isBlockedRight())
// {
// console.log('body2 stationary leftface add', body2.x);
2019-03-25 12:55:41 +00:00
body2.x += body2.getMoveX(totalB);
2019-03-25 17:25:11 +00:00
// }
2019-03-24 23:30:57 +00:00
if (body1.isBlockedRight())
{
2019-03-25 17:25:11 +00:00
// console.log('body2 stationary leftface down', body2.x);
2019-03-24 23:30:57 +00:00
body2.setBlockedRight(collisionInfo, body1);
}
}
else if (rightFace)
{
2019-03-25 17:25:11 +00:00
// console.log('The right of Body1 overlaps with the left of Body2');
// console.log(totalA, body1.isBlockedLeft(), body1.isBlockedRight());
// console.log(totalB, body2.isBlockedLeft(), body2.isBlockedRight());
2019-03-24 23:30:57 +00:00
// The top of Body2 overlaps with the bottom of Body1
2019-03-25 17:25:11 +00:00
// if (totalB !== 0 && !body2.isBlockedLeft())
// {
// console.log('body2 stationary rightface add', body2.x);
2019-03-25 12:55:41 +00:00
body2.x += body2.getMoveX(totalB);
2019-03-25 17:25:11 +00:00
// }
2019-03-24 23:30:57 +00:00
if (body1.isBlockedLeft())
{
2019-03-25 17:25:11 +00:00
// console.log('body2 stationary rightface down', body2.x);
2019-03-24 23:30:57 +00:00
body2.setBlockedLeft(collisionInfo, body1);
}
}
}
// We disregard the new velocity when a Body is world blocked AND blocked by something on the opposite face
if (body1.isBlockedX())
{
nx1 = 0;
}
if (body2.isBlockedX())
{
nx2 = 0;
}
// Wakey, wakey?
2019-03-24 23:30:57 +00:00
if (body1.sleeping)
{
if (Math.abs(nx1) < body1.minVelocity.x)
{
// Not enough new velocity to get out of bed for
nx1 = 0;
}
else
{
// console.log('waking body1 from', nx1, body1.prevVelocity.y);
body1.wake();
}
}
if (body2.sleeping)
{
if (Math.abs(nx2) < body2.minVelocity.x)
{
// Not enough new velocity to get out of bed for
nx2 = 0;
}
else
{
2019-03-24 23:30:57 +00:00
// console.log('waking body2 from', nx2, body2.prevVelocity.y);
body2.wake();
}
}
2019-03-24 23:30:57 +00:00
// console.log('SepX End', nx1, nx2);
velocity1.x = nx1;
velocity2.x = nx2;
2019-03-25 17:25:11 +00:00
// console.log('post-GetOverlap bx = body1', body1.x, 'body2', body2.x);
// console.log('post-GetOverlap bs = body1', body1.right, 'body2', body2.right);
console.log('x compare (POST-SX):', body1.right, 'body2', body2.x, '=', (body1.right - body2.x));
// If we got this far then there WAS overlap, and separation is complete, so return true
return true;
};
module.exports = SeparateX;