mirror of
https://github.com/photonstorm/phaser
synced 2025-02-16 22:18:29 +00:00
Testing new Separation logic (Y done, X in progress)
This commit is contained in:
parent
2bcf1f70be
commit
7630b8e28d
6 changed files with 498 additions and 270 deletions
69
src/physics/arcade/BlockCheckX.js
Normal file
69
src/physics/arcade/BlockCheckX.js
Normal file
|
@ -0,0 +1,69 @@
|
|||
var BlockCheckX = function (body1, body2, overlap)
|
||||
{
|
||||
var v1 = body1.velocity.x;
|
||||
var v2 = body2.velocity.x;
|
||||
|
||||
var body1MovingUp = body1._dy < 0;
|
||||
var body1MovingDown = body1._dy > 0;
|
||||
|
||||
var body2MovingUp = body2._dy < 0;
|
||||
var body2MovingDown = body2._dy > 0;
|
||||
|
||||
var body1OnTop = Math.abs(body1.bottom - body2.x) <= Math.abs(body2.bottom - body1.x);
|
||||
var body2OnTop = !body1OnTop;
|
||||
|
||||
var body1FullImpact = v2 - v1 * body1.bounce.x;
|
||||
var body2FullImpact = v1 - v2 * body2.bounce.x;
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Blocked Checks - Doesn't matter if they're pushable or not, blocked is blocked
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
// Body1 is moving down and Body2 is blocked from going down any further
|
||||
if (body1MovingDown && body1OnTop && body2.blocked.down)
|
||||
{
|
||||
console.log('BlockX 1', body1.x, overlap);
|
||||
|
||||
body1.x -= overlap;
|
||||
body1.velocity.x = body1FullImpact;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Body2 is moving down and Body1 is blocked from going down any further
|
||||
if (body2MovingDown && body2OnTop && body1.blocked.down)
|
||||
{
|
||||
console.log('BlockX 2', body2.x, overlap);
|
||||
|
||||
body2.x -= overlap;
|
||||
body2.velocity.x = body2FullImpact;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Body1 is moving up and Body2 is blocked from going up any further
|
||||
if (body1MovingUp && body2OnTop && body2.blocked.up)
|
||||
{
|
||||
console.log('BlockX 3', body1.x, overlap);
|
||||
|
||||
body1.x += overlap;
|
||||
body1.velocity.x = body1FullImpact;
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Body2 is moving up and Body1 is blocked from going up any further
|
||||
if (body2MovingUp && body1OnTop && body1.blocked.up)
|
||||
{
|
||||
console.log('BlockX 4', body2.x, overlap);
|
||||
|
||||
body2.x += overlap;
|
||||
body2.velocity.x = body2FullImpact;
|
||||
|
||||
return 4;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
module.exports = BlockCheckX;
|
69
src/physics/arcade/BlockCheckY.js
Normal file
69
src/physics/arcade/BlockCheckY.js
Normal file
|
@ -0,0 +1,69 @@
|
|||
var BlockCheckY = function (body1, body2, overlap)
|
||||
{
|
||||
var v1 = body1.velocity.y;
|
||||
var v2 = body2.velocity.y;
|
||||
|
||||
var body1MovingUp = body1._dy < 0;
|
||||
var body1MovingDown = body1._dy > 0;
|
||||
|
||||
var body2MovingUp = body2._dy < 0;
|
||||
var body2MovingDown = body2._dy > 0;
|
||||
|
||||
var body1OnTop = Math.abs(body1.bottom - body2.y) <= Math.abs(body2.bottom - body1.y);
|
||||
var body2OnTop = !body1OnTop;
|
||||
|
||||
var body1FullImpact = v2 - v1 * body1.bounce.y;
|
||||
var body2FullImpact = v1 - v2 * body2.bounce.y;
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Blocked Checks - Doesn't matter if they're pushable or not, blocked is blocked
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
// Body1 is moving down and Body2 is blocked from going down any further
|
||||
if (body1MovingDown && body1OnTop && body2.blocked.down)
|
||||
{
|
||||
console.log('BlockY 1', body1.y, overlap);
|
||||
|
||||
body1.y -= overlap;
|
||||
body1.velocity.y = body1FullImpact;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Body2 is moving down and Body1 is blocked from going down any further
|
||||
if (body2MovingDown && body2OnTop && body1.blocked.down)
|
||||
{
|
||||
console.log('BlockY 2', body2.y, overlap);
|
||||
|
||||
body2.y -= overlap;
|
||||
body2.velocity.y = body2FullImpact;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Body1 is moving up and Body2 is blocked from going up any further
|
||||
if (body1MovingUp && body2OnTop && body2.blocked.up)
|
||||
{
|
||||
console.log('BlockY 3', body1.y, overlap);
|
||||
|
||||
body1.y += overlap;
|
||||
body1.velocity.y = body1FullImpact;
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Body2 is moving up and Body1 is blocked from going up any further
|
||||
if (body2MovingUp && body1OnTop && body1.blocked.up)
|
||||
{
|
||||
console.log('BlockY 4', body2.y, overlap);
|
||||
|
||||
body2.y += overlap;
|
||||
body2.velocity.y = body2FullImpact;
|
||||
|
||||
return 4;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
module.exports = BlockCheckY;
|
88
src/physics/arcade/ProcessX.js
Normal file
88
src/physics/arcade/ProcessX.js
Normal file
|
@ -0,0 +1,88 @@
|
|||
var body1Pushable;
|
||||
var body2Pushable;
|
||||
var body1MassImpact;
|
||||
var body2MassImpact;
|
||||
var body1FullImpact;
|
||||
var body2FullImpact;
|
||||
|
||||
var SetProcessX = function (b1Pushable, b2Pushable, b1MassImpact, b2MassImpact, b1FullImpact, b2FullImpact)
|
||||
{
|
||||
body1Pushable = b1Pushable;
|
||||
body2Pushable = b2Pushable;
|
||||
body1MassImpact = b1MassImpact;
|
||||
body2MassImpact = b2MassImpact;
|
||||
body1FullImpact = b1FullImpact;
|
||||
body2FullImpact = b2FullImpact;
|
||||
};
|
||||
|
||||
var RunProcessX = function (body1, body2, overlap1, overlap2, bodyStationary, body2Direction, debug)
|
||||
{
|
||||
if (body1Pushable && body2Pushable)
|
||||
{
|
||||
// Both pushable
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-0 :: body1', body1.x, 'body2', body2.x, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Both pushable, or both moving at the same time, so equal rebound
|
||||
overlap1 *= 0.5;
|
||||
overlap2 *= 0.5;
|
||||
|
||||
body1.x += overlap1;
|
||||
body2.x += overlap2;
|
||||
|
||||
body1.velocity.x = body1MassImpact;
|
||||
body2.velocity.x = body2MassImpact;
|
||||
}
|
||||
else if (body1Pushable && !body2Pushable)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-1 :: body1', body1.x, 'body2', body2.x, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Body1 pushable, Body2 not
|
||||
body1.x += overlap1;
|
||||
body1.velocity.x = body1FullImpact;
|
||||
}
|
||||
else if (!body1Pushable && body2Pushable)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-2 :: body1', body1.x, 'body2', body2.x, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Body2 pushable, Body1 not
|
||||
body2.x += overlap2;
|
||||
body2.velocity.x = body2FullImpact;
|
||||
}
|
||||
else if (bodyStationary || body2Direction)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-3 :: body1', body1.x, 'body2', body2.x, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Neither pushable, so base it on movement
|
||||
body1.velocity.x = 0;
|
||||
body2.velocity.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-4 :: body1', body1.x, 'body2', body2.x, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Body1 and Body2 both moving up, so slow Body1 to match Body2 speed
|
||||
body1.velocity.x = body2.velocity.x;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
SetProcessY: SetProcessX,
|
||||
RunProcessY: RunProcessX
|
||||
};
|
88
src/physics/arcade/ProcessY.js
Normal file
88
src/physics/arcade/ProcessY.js
Normal file
|
@ -0,0 +1,88 @@
|
|||
var body1Pushable;
|
||||
var body2Pushable;
|
||||
var body1MassImpact;
|
||||
var body2MassImpact;
|
||||
var body1FullImpact;
|
||||
var body2FullImpact;
|
||||
|
||||
var SetProcessY = function (b1Pushable, b2Pushable, b1MassImpact, b2MassImpact, b1FullImpact, b2FullImpact)
|
||||
{
|
||||
body1Pushable = b1Pushable;
|
||||
body2Pushable = b2Pushable;
|
||||
body1MassImpact = b1MassImpact;
|
||||
body2MassImpact = b2MassImpact;
|
||||
body1FullImpact = b1FullImpact;
|
||||
body2FullImpact = b2FullImpact;
|
||||
};
|
||||
|
||||
var RunProcessY = function (body1, body2, overlap1, overlap2, bodyStationary, body2Direction, debug)
|
||||
{
|
||||
if (body1Pushable && body2Pushable)
|
||||
{
|
||||
// Both pushable
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-0 :: body1', body1.y, 'body2', body2.y, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Both pushable, or both moving at the same time, so equal rebound
|
||||
overlap1 *= 0.5;
|
||||
overlap2 *= 0.5;
|
||||
|
||||
body1.y += overlap1;
|
||||
body2.y += overlap2;
|
||||
|
||||
body1.velocity.y = body1MassImpact;
|
||||
body2.velocity.y = body2MassImpact;
|
||||
}
|
||||
else if (body1Pushable && !body2Pushable)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-1 :: body1', body1.y, 'body2', body2.y, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Body1 pushable, Body2 not
|
||||
body1.y += overlap1;
|
||||
body1.velocity.y = body1FullImpact;
|
||||
}
|
||||
else if (!body1Pushable && body2Pushable)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-2 :: body1', body1.y, 'body2', body2.y, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Body2 pushable, Body1 not
|
||||
body2.y += overlap2;
|
||||
body2.velocity.y = body2FullImpact;
|
||||
}
|
||||
else if (bodyStationary || body2Direction)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-3 :: body1', body1.y, 'body2', body2.y, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Neither pushable, so base it on movement
|
||||
body1.velocity.y = 0;
|
||||
body2.velocity.y = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
console.log(debug + '-4 :: body1', body1.y, 'body2', body2.y, 'overlap', overlap1, overlap2);
|
||||
}
|
||||
|
||||
// Body1 and Body2 both moving up, so slow Body1 to match Body2 speed
|
||||
body1.velocity.y = body2.velocity.y;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
SetProcessY: SetProcessY,
|
||||
RunProcessY: RunProcessY
|
||||
};
|
|
@ -4,7 +4,9 @@
|
|||
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
||||
*/
|
||||
|
||||
var BlockCheckX = require('./BlockCheckX');
|
||||
var GetOverlapX = require('./GetOverlapX');
|
||||
var ProcessX = require('./ProcessX');
|
||||
|
||||
/**
|
||||
* Separates two overlapping bodies on the X-axis (horizontally).
|
||||
|
@ -21,7 +23,7 @@ var GetOverlapX = require('./GetOverlapX');
|
|||
* @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.
|
||||
*
|
||||
* @return {boolean} `true` if the two bodies overlap horizontally, otherwise `false`.
|
||||
* @return {boolean} `true` if the two bodies overlap vertically, otherwise `false`.
|
||||
*/
|
||||
var SeparateX = function (body1, body2, overlapOnly, bias)
|
||||
{
|
||||
|
@ -39,79 +41,35 @@ var SeparateX = function (body1, body2, overlapOnly, bias)
|
|||
return (overlap !== 0) || (body1.embedded && body2.embedded);
|
||||
}
|
||||
|
||||
var blockedState = BlockCheckX(body1, body2, Math.abs(overlap));
|
||||
|
||||
// Adjust their positions and velocities accordingly (if there was any overlap)
|
||||
var v1 = body1.velocity.x;
|
||||
var v2 = body2.velocity.x;
|
||||
|
||||
var body1FullImpact = v2 - v1 * body1.bounce.x;
|
||||
var body2FullImpact = v1 - v2 * body2.bounce.x;
|
||||
|
||||
if (!body1Immovable && !body2Immovable)
|
||||
{
|
||||
if (body1Pushable === body2Pushable)
|
||||
if (blockedState > 0)
|
||||
{
|
||||
// Both equally pushable (true/false doesn't matter)
|
||||
overlap *= 0.5;
|
||||
|
||||
body1.x -= overlap;
|
||||
body2.x += overlap;
|
||||
}
|
||||
else if (body1Pushable && !body2Pushable)
|
||||
{
|
||||
// Only body1 is pushable
|
||||
body1.x -= overlap;
|
||||
}
|
||||
else if (body2Pushable && !body1Pushable)
|
||||
{
|
||||
// Only body2 is pushable
|
||||
body2.x += overlap;
|
||||
}
|
||||
}
|
||||
else if (!body1Immovable)
|
||||
{
|
||||
// Body2 is immovable, so 1 gets all the separation no matter what
|
||||
body1.x -= overlap;
|
||||
|
||||
body1.velocity.x = v2 - v1 * body1.bounce.x;
|
||||
|
||||
// This is special case code that handles things like vertically moving platforms you can ride
|
||||
if (body2.moves)
|
||||
{
|
||||
body1.y += (body2.y - body2.prev.y) * body2.friction.y;
|
||||
body1._dy = body1.y - body1.prev.y;
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else if (!body2Immovable)
|
||||
{
|
||||
// Body1 is immovable, so 2 gets all the separation no matter what
|
||||
body2.x += overlap;
|
||||
// negative delta = up, positive delta = down (inc. gravity)
|
||||
overlap = Math.abs(overlap);
|
||||
|
||||
body2.velocity.x = v1 - v2 * body2.bounce.x;
|
||||
var body1MovingLeft = body1._dx < 0;
|
||||
var body1MovingRight = body1._dx > 0;
|
||||
var body1Stationary = body1._dx === 0;
|
||||
|
||||
// This is special case code that handles things like vertically moving platforms you can ride
|
||||
if (body1.moves)
|
||||
{
|
||||
body2.y += (body1.y - body1.prev.y) * body1.friction.y;
|
||||
body2._dy = body2.y - body2.prev.y;
|
||||
}
|
||||
var body2MovingLeft = body2._dx < 0;
|
||||
var body2MovingRight = body2._dx > 0;
|
||||
var body2Stationary = body2._dx === 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Separation is over and we've 2 movable bodies, so now calculate velocity
|
||||
|
||||
if (body1._dx > body2._dx && (body2.blocked.right || !body2Pushable))
|
||||
{
|
||||
// Body1 is moving right and Body2 is blocked from going right any further, or isn't pushable
|
||||
body1.velocity.x = v2 - v1 * body1.bounce.x;
|
||||
}
|
||||
else if (body1._dx < body2._dx && (body2.blocked.left || !body2Pushable))
|
||||
{
|
||||
// Body1 is moving left and Body2 is blocked from going left any further, or isn't pushable
|
||||
body1.velocity.x = v2 - v1 * body1.bounce.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If neither body is pushable, or are both pushable, they should both rebound equally
|
||||
var body1OnLeft = Math.abs(body1.right - body2.x) <= Math.abs(body2.right - body1.x);
|
||||
var body2OnLeft = !body1OnLeft;
|
||||
|
||||
var nv1 = Math.sqrt((v2 * v2 * body2.mass) / body1.mass) * ((v2 > 0) ? 1 : -1);
|
||||
var nv2 = Math.sqrt((v1 * v1 * body1.mass) / body2.mass) * ((v1 > 0) ? 1 : -1);
|
||||
|
@ -120,8 +78,98 @@ var SeparateX = function (body1, body2, overlapOnly, bias)
|
|||
nv1 -= avg;
|
||||
nv2 -= avg;
|
||||
|
||||
body1.velocity.x = avg + nv1 * body1.bounce.x;
|
||||
body2.velocity.x = avg + nv2 * body2.bounce.x;
|
||||
var body1MassImpact = avg + nv1 * body1.bounce.y;
|
||||
var body2MassImpact = avg + nv2 * body2.bounce.y;
|
||||
|
||||
ProcessX.SetProcessX(
|
||||
body1Pushable,
|
||||
body2Pushable,
|
||||
body1MassImpact,
|
||||
body2MassImpact,
|
||||
body1FullImpact,
|
||||
body2FullImpact
|
||||
);
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Pushable Checks
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
// Body1 hits Body2 on its right side
|
||||
if (body1MovingLeft && body2OnLeft)
|
||||
{
|
||||
return ProcessX.RunProcessX(body1, body2, overlap, -overlap, body2Stationary, body2MovingRight, 'PushX1');
|
||||
}
|
||||
|
||||
// Body2 hits Body1 on its right side
|
||||
if (body2MovingLeft && body1OnLeft)
|
||||
{
|
||||
return ProcessX.RunProcessX(body1, body2, -overlap, overlap, body1Stationary, body1MovingRight, 'PushX2');
|
||||
}
|
||||
|
||||
// Body1 hits Body2 from above
|
||||
/*
|
||||
if (body1MovingDown && body1OnTop)
|
||||
{
|
||||
return ProcessX.RunProcessX(body1, body2, overlap, -overlap, body2Stationary, body2MovingUp, 'PushX3');
|
||||
}
|
||||
|
||||
// Body2 hits Body1 from above
|
||||
if (body2MovingDown && body2OnTop)
|
||||
{
|
||||
return ProcessX.RunProcessX(body1, body2, -overlap, overlap, body1Stationary, body1MovingUp, 'PushX4');
|
||||
}
|
||||
*/
|
||||
|
||||
console.log('uh oh');
|
||||
|
||||
// console.log('body1MovingUp', body1MovingUp, 'body2MovingUp', body2MovingUp, 'body1OnTop', body1OnTop, 'body2OnTop', body2OnTop);
|
||||
|
||||
}
|
||||
else if (body1Immovable)
|
||||
{
|
||||
console.log('SepX 1');
|
||||
|
||||
// Body1 is immovable
|
||||
if (blockedState === 1 || blockedState === 3)
|
||||
{
|
||||
// But Body2 cannot go anywhere either, so we cancel out velocity
|
||||
body2.velocity.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body2.x += overlap;
|
||||
body2.velocity.x = body2FullImpact;
|
||||
}
|
||||
|
||||
// This is special case code that handles things like vertically moving platforms you can ride
|
||||
if (body1.moves)
|
||||
{
|
||||
body2.y += (body1.y - body1.prev.y) * body1.friction.y;
|
||||
body2._dy = body2.y - body2.prev.y;
|
||||
}
|
||||
}
|
||||
else if (body2Immovable)
|
||||
{
|
||||
console.log('SepX 2');
|
||||
|
||||
// Body2 is immovable
|
||||
if (blockedState === 2 || blockedState === 4)
|
||||
{
|
||||
// But Body1 cannot go anywhere either, so we cancel out velocity
|
||||
body1.velocity.x = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.x -= overlap;
|
||||
body1.velocity.x = body1FullImpact;
|
||||
}
|
||||
|
||||
// This is special case code that handles things like vertically moving platforms you can ride
|
||||
if (body2.moves)
|
||||
{
|
||||
body1.y += (body2.y - body2.prev.y) * body2.friction.y;
|
||||
body1._dy = body1.y - body1.prev.y;
|
||||
}
|
||||
}
|
||||
|
||||
// If we got this far then there WAS overlap, and separation is complete, so return true
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
||||
*/
|
||||
|
||||
var BlockCheckY = require('./BlockCheckY');
|
||||
var GetOverlapY = require('./GetOverlapY');
|
||||
var ProcessY = require('./ProcessY');
|
||||
|
||||
/**
|
||||
* Separates two overlapping bodies on the Y-axis (vertically).
|
||||
|
@ -39,21 +41,32 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
|
|||
return (overlap !== 0) || (body1.embedded && body2.embedded);
|
||||
}
|
||||
|
||||
var blockedState = BlockCheckY(body1, body2, Math.abs(overlap));
|
||||
|
||||
// Adjust their positions and velocities accordingly (if there was any overlap)
|
||||
var v1 = body1.velocity.y;
|
||||
var v2 = body2.velocity.y;
|
||||
|
||||
var body1FullImpact = v2 - v1 * body1.bounce.y;
|
||||
var body2FullImpact = v1 - v2 * body2.bounce.y;
|
||||
|
||||
if (!body1Immovable && !body2Immovable)
|
||||
{
|
||||
// negative delta = up, positive delta = down (inc. gravity)
|
||||
if (blockedState > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// negative delta = up, positive delta = down (inc. gravity)
|
||||
overlap = Math.abs(overlap);
|
||||
|
||||
var body1MovingUp = body1._dy < 0;
|
||||
var body1MovingDown = body1._dy >= 0;
|
||||
var body1MovingDown = body1._dy > 0;
|
||||
var body1Stationary = body1._dy === 0;
|
||||
|
||||
var body2MovingUp = body2._dy < 0;
|
||||
var body2MovingDown = body2._dy >= 0;
|
||||
var body2MovingDown = body2._dy > 0;
|
||||
var body2Stationary = body2._dy === 0;
|
||||
|
||||
var body1OnTop = Math.abs(body1.bottom - body2.y) <= Math.abs(body2.bottom - body1.y);
|
||||
var body2OnTop = !body1OnTop;
|
||||
|
@ -65,235 +78,65 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
|
|||
nv1 -= avg;
|
||||
nv2 -= avg;
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Blocked / Ground Checks
|
||||
// -----------------------------------------------------------------------
|
||||
var body1MassImpact = avg + nv1 * body1.bounce.y;
|
||||
var body2MassImpact = avg + nv2 * body2.bounce.y;
|
||||
|
||||
// Body1 is moving down and Body2 is blocked from going down any further
|
||||
if (body1MovingDown && body1OnTop && body2.blocked.down)
|
||||
{
|
||||
console.log('BlockY 1', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
body1.y -= overlap;
|
||||
|
||||
body1.velocity.y = v2 - v1 * body1.bounce.y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Body2 is moving down and Body1 is blocked from going down any further
|
||||
if (body2MovingDown && body2OnTop && body1.blocked.down)
|
||||
{
|
||||
console.log('BlockY 2', body1.gameObject.name, 'vs', body2.gameObject.name, body2.y, overlap);
|
||||
|
||||
body2.y -= overlap;
|
||||
|
||||
body2.velocity.y = v1 - v2 * body2.bounce.y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Body1 is moving up and Body2 is blocked from going up any further
|
||||
if (body1MovingUp && body2OnTop && body2.blocked.up)
|
||||
{
|
||||
console.log('BlockY 3', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
body1.y += overlap;
|
||||
|
||||
body1.velocity.y = v2 - v1 * body1.bounce.y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Body2 is moving up and Body1 is blocked from going up any further
|
||||
if (body2MovingUp && body1OnTop && body1.blocked.up)
|
||||
{
|
||||
console.log('BlockY 4', body1.gameObject.name, 'vs', body2.gameObject.name, body2.y, overlap);
|
||||
|
||||
body2.y += overlap;
|
||||
|
||||
body2.velocity.y = v1 - v2 * body2.bounce.y;
|
||||
|
||||
return true;
|
||||
}
|
||||
ProcessY.SetProcessY(
|
||||
body1Pushable,
|
||||
body2Pushable,
|
||||
body1MassImpact,
|
||||
body2MassImpact,
|
||||
body1FullImpact,
|
||||
body2FullImpact
|
||||
);
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Pushable Checks
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
// Body1 is moving down and on top - and Body2 is pushable
|
||||
if (body1MovingDown && !body1Pushable && body1OnTop && body2Pushable)
|
||||
// Body1 hits Body2 from below
|
||||
if (body1MovingUp && body2OnTop)
|
||||
{
|
||||
console.log('PushY 1', body1.gameObject.name, 'vs', body2.gameObject.name, body2.y, overlap);
|
||||
|
||||
// Body 2 gets it all
|
||||
body2.y += overlap;
|
||||
body2.velocity.y = v1;
|
||||
|
||||
// body2.velocity.y = v1 - v2 * body2.bounce.y;
|
||||
|
||||
return true;
|
||||
return ProcessY.RunProcessY(body1, body2, overlap, -overlap, body2Stationary, body2MovingDown, 'PushY1');
|
||||
}
|
||||
|
||||
// Body1 is moving down and on top - and Body2 is pushable
|
||||
if (body2MovingDown && !body2Pushable && body2OnTop && body1Pushable)
|
||||
// Body2 hits Body1 from below
|
||||
if (body2MovingUp && body1OnTop)
|
||||
{
|
||||
console.log('PushY 2', body1.gameObject.name, 'vs', body2.gameObject.name, body2.y, overlap);
|
||||
|
||||
// Body 1 gets it all
|
||||
body1.y += overlap;
|
||||
body1.velocity.y = v2;
|
||||
|
||||
// body1.velocity.y = v2 - v1 * body1.bounce.y;
|
||||
|
||||
return true;
|
||||
return ProcessY.RunProcessY(body1, body2, -overlap, overlap, body1Stationary, body1MovingDown, 'PushY2');
|
||||
}
|
||||
|
||||
// Insert Up versions here ^^^
|
||||
|
||||
|
||||
|
||||
|
||||
// Body1 is moving down and on top - and Body2 is pushable
|
||||
if (body1MovingDown && body1OnTop && body2Pushable)
|
||||
// Body1 hits Body2 from above
|
||||
if (body1MovingDown && body1OnTop)
|
||||
{
|
||||
if (body1Pushable)
|
||||
{
|
||||
console.log('PushY 3', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
// They're both pushable? They can share the separation then
|
||||
overlap *= 0.5;
|
||||
|
||||
body1.y -= overlap;
|
||||
body2.y += overlap;
|
||||
|
||||
body1.velocity.y = avg + nv1 * body1.bounce.y;
|
||||
body2.velocity.y = avg + nv2 * body2.bounce.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('PushY 4', body1.gameObject.name, 'vs', body2.gameObject.name, body2.y, overlap);
|
||||
|
||||
// Body 2 gets it all
|
||||
body2.y += overlap;
|
||||
|
||||
// body2.velocity.y = v1 - v2 * body2.bounce.y;
|
||||
}
|
||||
|
||||
return true;
|
||||
return ProcessY.RunProcessY(body1, body2, overlap, -overlap, body2Stationary, body2MovingUp, 'PushY3');
|
||||
}
|
||||
|
||||
// Body2 is moving down and on top - and Body1 is pushable
|
||||
if (body2MovingDown && body2OnTop && body1Pushable)
|
||||
// Body2 hits Body1 from above
|
||||
if (body2MovingDown && body2OnTop)
|
||||
{
|
||||
if (body2Pushable)
|
||||
{
|
||||
console.log('PushY 5', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
// They're both pushable? They can share the separation then
|
||||
overlap *= 0.5;
|
||||
|
||||
body1.y += overlap;
|
||||
body2.y -= overlap;
|
||||
|
||||
body1.velocity.y = avg + nv1 * body1.bounce.y;
|
||||
body2.velocity.y = avg + nv2 * body2.bounce.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('PushY 6', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
// Body 1 gets it all
|
||||
body1.y += overlap;
|
||||
|
||||
// body1.velocity.y = v2 - v1 * body1.bounce.y;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Body1 is moving up and on the bottom - and Body2 is pushable
|
||||
if (body1MovingUp && body2OnTop && body2Pushable)
|
||||
{
|
||||
if (body1Pushable)
|
||||
{
|
||||
console.log('PushY 7', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
// They're both pushable? They can share the separation then
|
||||
overlap *= 0.5;
|
||||
|
||||
body1.y += overlap;
|
||||
body2.y -= overlap;
|
||||
|
||||
body1.velocity.y = avg + nv1 * body1.bounce.y;
|
||||
body2.velocity.y = avg + nv2 * body2.bounce.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('PushY 8', body1.gameObject.name, 'vs', body2.gameObject.name, body2.y, overlap);
|
||||
|
||||
// Body 2 gets it all
|
||||
body2.y -= overlap;
|
||||
|
||||
body2.velocity.y = v1 - v2 * body2.bounce.y;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Body2 is moving up and on the bottom - and Body1 is pushable
|
||||
if (body2MovingUp && body1OnTop && body1Pushable)
|
||||
{
|
||||
if (body2Pushable)
|
||||
{
|
||||
console.log('PushY 9', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
// They're both pushable? They can share the separation then
|
||||
overlap *= 0.5;
|
||||
|
||||
body1.y -= overlap;
|
||||
body2.y += overlap;
|
||||
|
||||
body1.velocity.y = avg + nv1 * body1.bounce.y;
|
||||
body2.velocity.y = avg + nv2 * body2.bounce.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('PushY 10', body1.gameObject.name, 'vs', body2.gameObject.name, body1.y, overlap);
|
||||
|
||||
// Body 1 gets it all
|
||||
body1.y -= overlap;
|
||||
|
||||
body1.velocity.y = v2 - v1 * body1.bounce.y;
|
||||
}
|
||||
|
||||
return true;
|
||||
return ProcessY.RunProcessY(body1, body2, -overlap, overlap, body1Stationary, body1MovingUp, 'PushY4');
|
||||
}
|
||||
|
||||
console.log('uh oh');
|
||||
console.log('body1MovingUp', body1MovingUp, 'body2MovingUp', body2MovingUp, 'body1OnTop', body1OnTop, 'body2OnTop', body2OnTop);
|
||||
|
||||
}
|
||||
else if (!body1Immovable)
|
||||
else if (body1Immovable)
|
||||
{
|
||||
// Body2 is immovable, so 1 gets all the separation no matter what
|
||||
body1.y -= overlap;
|
||||
console.log('SepY 1');
|
||||
|
||||
body1.velocity.y = v2 - v1 * body1.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontally moving platforms you can ride
|
||||
if (body2.moves)
|
||||
// Body1 is immovable
|
||||
if (blockedState === 1 || blockedState === 3)
|
||||
{
|
||||
body1.x += (body2.x - body2.prev.x) * body2.friction.x;
|
||||
body1._dx = body1.x - body1.prev.x;
|
||||
// But Body2 cannot go anywhere either, so we cancel out velocity
|
||||
body2.velocity.y = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body2.y += overlap;
|
||||
body2.velocity.y = body2FullImpact;
|
||||
}
|
||||
}
|
||||
else if (!body2Immovable)
|
||||
{
|
||||
// Body1 is immovable, so 2 gets all the separation no matter what
|
||||
body2.y += overlap;
|
||||
|
||||
body2.velocity.y = v1 - v2 * body2.bounce.y;
|
||||
|
||||
// This is special case code that handles things like horizontally moving platforms you can ride
|
||||
if (body1.moves)
|
||||
|
@ -302,6 +145,29 @@ var SeparateY = function (body1, body2, overlapOnly, bias)
|
|||
body2._dx = body2.x - body2.prev.x;
|
||||
}
|
||||
}
|
||||
else if (body2Immovable)
|
||||
{
|
||||
console.log('SepY 2');
|
||||
|
||||
// Body2 is immovable
|
||||
if (blockedState === 2 || blockedState === 4)
|
||||
{
|
||||
// But Body1 cannot go anywhere either, so we cancel out velocity
|
||||
body1.velocity.y = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
body1.y -= overlap;
|
||||
body1.velocity.y = body1FullImpact;
|
||||
}
|
||||
|
||||
// This is special case code that handles things like horizontally moving platforms you can ride
|
||||
if (body2.moves)
|
||||
{
|
||||
body1.x += (body2.x - body2.prev.x) * body2.friction.x;
|
||||
body1._dx = body1.x - body1.prev.x;
|
||||
}
|
||||
}
|
||||
|
||||
// If we got this far then there WAS overlap, and separation is complete, so return true
|
||||
return true;
|
||||
|
|
Loading…
Add table
Reference in a new issue