2019-03-18 14:12:40 +00:00
|
|
|
/**
|
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-08 20:12:49 +00:00
|
|
|
var CONST = require('./const');
|
2017-11-09 13:02:55 +00:00
|
|
|
var GetOverlapY = require('./GetOverlapY');
|
2019-03-15 19:22:51 +00:00
|
|
|
var IntersectsRect = require('./IntersectsRect');
|
2017-11-09 13:02:55 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-10-19 11:32:43 +00:00
|
|
|
* Separates two overlapping bodies on the Y-axis (vertically).
|
|
|
|
*
|
2019-03-08 20:12:49 +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 11:32:43 +00:00
|
|
|
*
|
2019-03-08 20:12:49 +00:00
|
|
|
* The bodies won't be separated if there is no vertical 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.SeparateY
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-03-08 20:12:49 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate. This is our priority body.
|
2018-10-19 11:32:43 +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 vertically, otherwise `false`.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2017-11-09 13:02:55 +00:00
|
|
|
var SeparateY = function (body1, body2, overlapOnly, bias)
|
|
|
|
{
|
2019-03-11 09:19:41 +00:00
|
|
|
var result = GetOverlapY(body1, body2, overlapOnly, bias);
|
2019-03-08 20:12:49 +00:00
|
|
|
|
2019-03-18 08:08:46 +00:00
|
|
|
var overlap = result.overlap;
|
|
|
|
var topFace = result.topFace;
|
2019-03-15 19:22:51 +00:00
|
|
|
var bottomFace = !topFace;
|
2019-03-18 08:08:46 +00:00
|
|
|
var intersects = result.intersects;
|
2017-11-09 13:02:55 +00:00
|
|
|
|
2019-03-08 20:12:49 +00:00
|
|
|
var velocity1 = body1.velocity;
|
|
|
|
var velocity2 = body2.velocity;
|
|
|
|
|
2019-03-11 09:19:41 +00:00
|
|
|
var blocked1 = body1.blocked;
|
|
|
|
var blocked2 = body2.blocked;
|
|
|
|
|
2019-03-18 08:08:46 +00:00
|
|
|
var bounce1 = body1.bounce;
|
|
|
|
var bounce2 = body2.bounce;
|
|
|
|
|
2019-03-15 19:22:51 +00:00
|
|
|
var worldBlocked1 = body1.worldBlocked;
|
|
|
|
var worldBlocked2 = body2.worldBlocked;
|
2019-03-14 14:16:15 +00:00
|
|
|
|
2019-03-08 20:12:49 +00:00
|
|
|
var body1Immovable = (body1.physicsType === CONST.STATIC_BODY || body1.immovable);
|
|
|
|
var body2Immovable = (body2.physicsType === CONST.STATIC_BODY || 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
|
2019-03-14 14:16:15 +00:00
|
|
|
if (!intersects || overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateY || body2.customSeparateY)
|
2017-11-09 13:02:55 +00:00
|
|
|
{
|
2019-03-08 20:12:49 +00:00
|
|
|
// return true if there was some overlap, otherwise false.
|
2019-03-15 19:22:51 +00:00
|
|
|
return ((intersects && overlap !== 0) || (body1.embedded && body2.embedded));
|
2019-03-14 14:16:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:12:49 +00:00
|
|
|
// Adjust their positions and velocities accordingly based on the amount of overlap
|
|
|
|
var v1 = velocity1.y;
|
|
|
|
var v2 = velocity2.y;
|
2017-11-09 13:02:55 +00:00
|
|
|
|
2019-03-11 17:27:29 +00:00
|
|
|
var ny1 = v1;
|
|
|
|
var ny2 = v2;
|
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
console.log(body1.gameObject.name, 'overlaps', body2.gameObject.name, 'on the', ((topFace) ? 'top' : 'bottom'));
|
2019-03-15 19:22:51 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if (!body1Immovable && !body2Immovable)
|
2019-03-11 09:19:41 +00:00
|
|
|
{
|
2019-03-15 19:22:51 +00:00
|
|
|
// Neither body is immovable, so they get a new velocity based on mass
|
2019-03-13 02:27:51 +00:00
|
|
|
var mass1 = body1.mass;
|
|
|
|
var mass2 = body2.mass;
|
|
|
|
|
2019-03-18 17:17:59 +00:00
|
|
|
var bnv1 = Math.sqrt((v2 * v2 * mass2) / mass1) * ((v2 > 0) ? 1 : -1);
|
|
|
|
var bnv2 = Math.sqrt((v1 * v1 * mass1) / mass2) * ((v1 > 0) ? 1 : -1);
|
2019-03-13 02:27:51 +00:00
|
|
|
|
2019-03-18 17:17:59 +00:00
|
|
|
var avg = (bnv1 + bnv2) * 0.5;
|
|
|
|
|
|
|
|
var nv1 = bnv1;
|
|
|
|
var nv2 = bnv2;
|
2019-03-11 09:19:41 +00:00
|
|
|
|
2019-03-11 17:27:29 +00:00
|
|
|
nv1 -= avg;
|
|
|
|
nv2 -= avg;
|
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
ny1 = avg + nv1 * bounce1.y;
|
|
|
|
ny2 = avg + nv2 * bounce2.y;
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 17:17:59 +00:00
|
|
|
// var total = v1 - v2;
|
|
|
|
// ny1 = (((mass1 - mass2) * v1 + 2 * mass1 * v1) / (mass1 + mass2)) * bounce1.y;
|
|
|
|
// ny2 = (total + ny1) * bounce2.y;
|
|
|
|
// console.log('*1', ny1, ny2, 'vs', v1, v2, 'delta', body1.deltaY(), body2.deltaY());
|
|
|
|
|
|
|
|
console.log('resolution');
|
|
|
|
console.log('body1', ny1, 'body2', ny2);
|
|
|
|
console.log('v1', v1, 'v2', v2);
|
|
|
|
console.log('avg', avg);
|
|
|
|
console.log('nv', nv1, nv2);
|
|
|
|
console.log('sqrt', bnv1, bnv2);
|
|
|
|
console.log('delta', body1.deltaY(), body2.deltaY());
|
|
|
|
|
|
|
|
// console.log('*1', ny1, ny2, 'vs', v1, v2, 'avg', avg, 'nv', nv1, nv2, 'bounce', body1.bounce.y, body2.bounce.y, 'delta', body1.deltaY(), body2.deltaY());
|
|
|
|
// console.log('*1', ny1, ny2, 'vs', v1, v2, 'avg', avg, 'nv', nv1, nv2, 'bounce', body1.bounce.y, body2.bounce.y, 'delta', body1.deltaY(), body2.deltaY());
|
2019-03-11 17:27:29 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
else if (body1Immovable)
|
2019-03-13 17:27:11 +00:00
|
|
|
{
|
2019-03-15 19:22:51 +00:00
|
|
|
// Body1 is immovable, so adjust body2 speed
|
2019-03-18 14:12:40 +00:00
|
|
|
ny2 = v1 - v2 * bounce2.y;
|
2019-03-13 17:27:11 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
else if (body2Immovable)
|
2019-03-11 17:27:29 +00:00
|
|
|
{
|
2019-03-15 19:22:51 +00:00
|
|
|
// Body2 is immovable, so adjust body1 speed
|
2019-03-18 14:12:40 +00:00
|
|
|
ny1 = v2 - v1 * bounce1.y;
|
2019-03-11 17:27:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 08:08:46 +00:00
|
|
|
var totalA = 0;
|
|
|
|
var totalB = 0;
|
|
|
|
|
2019-03-11 17:27:29 +00:00
|
|
|
// Velocities calculated, time to work out what moves where
|
2019-03-13 17:27:11 +00:00
|
|
|
if (overlap !== 0)
|
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
// Try and give 50% separation to each body
|
2019-03-18 08:08:46 +00:00
|
|
|
var share = overlap * 0.5;
|
2019-03-13 17:27:11 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
if (topFace)
|
2019-03-13 17:27:11 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
totalA = body1.getMoveY(share);
|
|
|
|
|
|
|
|
if (totalA < share)
|
|
|
|
{
|
|
|
|
share += (share - totalA);
|
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
totalB = body2.getMoveY(-share);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalB = body2.getMoveY(share);
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
if (totalB < share)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
share += (share - totalB);
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
totalA = body1.getMoveY(-share);
|
2019-03-13 17:27:11 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
console.log('split at', totalA, totalB, 'of', overlap);
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
// console.log('d1', body1.deltaY(), 'd2', body2.deltaY());
|
|
|
|
|
|
|
|
// 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 world block state based on direction.
|
|
|
|
|
|
|
|
// Then, adjust for rebounded direction, if any.
|
|
|
|
|
|
|
|
console.log('preb', worldBlocked1.up, worldBlocked1.down, worldBlocked2.up, worldBlocked2.down);
|
|
|
|
|
|
|
|
if (ny1 < 0)
|
2019-03-18 08:08:46 +00:00
|
|
|
{
|
|
|
|
// Body1 is moving UP
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 08:08:46 +00:00
|
|
|
if (topFace)
|
2019-03-13 17:27:11 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// The top of Body1 overlaps with the bottom of Body2
|
|
|
|
if (worldBlocked2.up)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
body1.setWorldBlockedUp(body2.bottom);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 < 0 topface up', body1.y);
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
else
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
body1.y += totalA;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 < 0 topface add', body1.y);
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-13 17:27:11 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
else if (bottomFace)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// The bottom of Body1 overlaps with the top of Body2
|
|
|
|
if (worldBlocked2.down)
|
|
|
|
{
|
|
|
|
body1.setWorldBlockedDown(body2.y);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 < 0 bottomface down', body1.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.y += totalA;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 < 0 bottomface add', body1.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
2019-03-15 13:31:40 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
// If Body1 cannot move up, it doesn't matter what new velocity it has.
|
|
|
|
if (worldBlocked1.up && body1.sleeping)
|
|
|
|
{
|
|
|
|
ny1 = 0;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 < 0 zero sleep');
|
2019-03-18 14:12:40 +00:00
|
|
|
}
|
2019-03-13 17:27:11 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
else if (ny1 > 0)
|
2019-03-11 17:27:29 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// Body1 is moving DOWN
|
2019-03-15 19:22:51 +00:00
|
|
|
|
|
|
|
if (topFace)
|
2019-03-14 14:16:15 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// The top of Body1 overlaps with the bottom of Body2
|
2019-03-15 19:22:51 +00:00
|
|
|
if (worldBlocked2.up)
|
2019-03-15 12:54:25 +00:00
|
|
|
{
|
2019-03-15 19:22:51 +00:00
|
|
|
body1.setWorldBlockedUp(body2.bottom);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 > 0 topface up', body1.y);
|
2019-03-15 12:54:25 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
else
|
2019-03-15 12:54:25 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
body1.y += totalA;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 > 0 topface add', body1.y);
|
2019-03-15 12:54:25 +00:00
|
|
|
}
|
2019-03-14 14:16:15 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
else if (bottomFace)
|
2019-03-14 14:16:15 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// The bottom of Body1 overlaps with the top of Body2
|
|
|
|
if (worldBlocked2.down)
|
2019-03-15 12:54:25 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
body1.setWorldBlockedDown(body2.y);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 > 0 bottomface down', body1.y);
|
2019-03-15 12:54:25 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
else
|
2019-03-15 12:54:25 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
body1.y += totalA;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 > 0 bottomface add', body1.y);
|
2019-03-15 12:54:25 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
// If Body1 cannot move down, it doesn't matter what new velocity it has.
|
|
|
|
if (worldBlocked1.down && body1.sleeping)
|
|
|
|
{
|
|
|
|
ny1 = 0;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny1 > 0 zero sleep');
|
2019-03-18 14:12:40 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
else
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// Body1 is stationary
|
|
|
|
body1.y += totalA;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('body1 stationary', body1.y);
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
|
2019-03-18 17:17:59 +00:00
|
|
|
if (ny2 < 0)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
|
|
|
// Body2 is moving UP
|
2019-03-15 12:54:25 +00:00
|
|
|
|
2019-03-18 08:08:46 +00:00
|
|
|
if (topFace)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
// The bottom of Body2 overlaps with the top of Body1
|
|
|
|
if (worldBlocked1.down)
|
2019-03-18 08:08:46 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.setWorldBlockedDown(body1.y);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 < 0 topface down', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.y += totalB;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 < 0 topface add', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bottomFace)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
// The top of Body2 overlaps with the bottom of Body1
|
|
|
|
if (worldBlocked1.up)
|
2019-03-18 08:08:46 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.setWorldBlockedUp(body1.bottom);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 < 0 bottomface down', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.y += totalB;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 < 0 bottomface add', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
// If Body2 cannot move up, it doesn't matter what new velocity it has.
|
|
|
|
if (worldBlocked2.up && body2.sleeping)
|
|
|
|
{
|
|
|
|
ny2 = 0;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 < 0 zero sleep');
|
2019-03-18 14:12:40 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-18 17:17:59 +00:00
|
|
|
else if (ny2 > 0)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 08:08:46 +00:00
|
|
|
// Body2 is moving DOWN
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 08:08:46 +00:00
|
|
|
if (topFace)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
// The bottom of Body2 overlaps with the top of Body1
|
|
|
|
if (worldBlocked1.down)
|
2019-03-18 08:08:46 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.setWorldBlockedDown(body1.y);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 > 0 topface down', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.y += totalB;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 > 0 topface add', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
|
|
|
else if (bottomFace)
|
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
// The top of Body2 overlaps with the bottom of Body1
|
|
|
|
if (worldBlocked1.up)
|
2019-03-18 08:08:46 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
body2.setWorldBlockedUp(body1.bottom);
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 > 0 bottomface up', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-18 17:17:59 +00:00
|
|
|
body2.y += totalB;
|
|
|
|
console.log('ny2 > 0 bottomface add', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
2019-03-11 11:05:51 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
// If Body2 cannot move down, it doesn't matter what new velocity it has.
|
|
|
|
if (worldBlocked2.down && body2.sleeping)
|
|
|
|
{
|
|
|
|
ny2 = 0;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('ny2 > 0 zero sleep');
|
2019-03-18 14:12:40 +00:00
|
|
|
}
|
2019-03-11 17:27:29 +00:00
|
|
|
}
|
2019-03-18 08:08:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Body2 is stationary
|
|
|
|
body2.y += totalB;
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('body2 stationary', body2.y);
|
2019-03-18 08:08:46 +00:00
|
|
|
}
|
2019-03-11 17:27:29 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
console.log('postb', worldBlocked1.up, worldBlocked1.down, worldBlocked2.up, worldBlocked2.down);
|
|
|
|
|
|
|
|
// We disregard the new velocity when:
|
|
|
|
// Body is world blocked AND touching / blocked on the opposite face
|
2019-03-15 19:22:51 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
if (worldBlocked1.up && worldBlocked1.down)
|
2019-03-11 17:27:29 +00:00
|
|
|
{
|
2019-03-15 12:54:25 +00:00
|
|
|
ny1 = 0;
|
2019-03-11 09:19:41 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
if (worldBlocked2.up && worldBlocked2.down)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
ny2 = 0;
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-13 02:27:51 +00:00
|
|
|
|
2019-03-18 14:12:40 +00:00
|
|
|
if (body1.sleeping)
|
2019-03-15 12:54:25 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
if (Math.abs(ny1) < 10)
|
|
|
|
{
|
|
|
|
ny1 = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.log('waking body1 from', ny1, body1.prevVelocity.y);
|
|
|
|
body1.wake();
|
|
|
|
}
|
2019-03-11 17:27:29 +00:00
|
|
|
}
|
2019-03-18 14:12:40 +00:00
|
|
|
|
|
|
|
if (body2.sleeping)
|
2019-03-15 19:22:51 +00:00
|
|
|
{
|
2019-03-18 14:12:40 +00:00
|
|
|
if (Math.abs(ny2) < 10)
|
|
|
|
{
|
|
|
|
ny2 = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body2.wake();
|
|
|
|
}
|
2019-03-15 19:22:51 +00:00
|
|
|
}
|
2019-03-11 17:27:29 +00:00
|
|
|
|
|
|
|
velocity1.y = ny1;
|
|
|
|
velocity2.y = ny2;
|
2019-03-08 20:12:49 +00:00
|
|
|
|
2019-03-13 02:27:51 +00:00
|
|
|
// TODO: This is special case code that handles things like horizontal moving platforms you can ride
|
|
|
|
// if (body2.moves)
|
|
|
|
// {
|
|
|
|
// body1.x += body1.getMoveX((body2.deltaX()) * body2.friction.x, true);
|
|
|
|
// }
|
2019-03-08 20:12:49 +00:00
|
|
|
|
2019-03-18 17:17:59 +00:00
|
|
|
console.log('---', Date.now());
|
|
|
|
|
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 = SeparateY;
|