phaser/src/physics/arcade/SeparateY.js

302 lines
8.4 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-08 20:12:49 +00:00
var CONST = require('./const');
var GetOverlapY = require('./GetOverlapY');
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
*/
var SeparateY = function (body1, body2, overlapOnly, bias)
{
var result = GetOverlapY(body1, body2, overlapOnly, bias);
2019-03-08 20:12:49 +00:00
var overlap = result[0];
var faceTop = result[1];
var faceBottom = !faceTop;
2019-03-08 20:12:49 +00:00
var velocity1 = body1.velocity;
var velocity2 = body2.velocity;
var blocked1 = body1.blocked;
var blocked2 = body2.blocked;
2019-03-11 11:05:51 +00:00
// console.log('----------> SY', overlap, 'faceTop?', faceTop);
// console.log('SY', body1.gameObject.name, body2.gameObject.name, overlap, 'faceTop?', faceTop);
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);
// Can't separate two immovable bodies, or a body with its own custom separation logic
2019-03-08 20:12:49 +00:00
if (overlapOnly || (body1Immovable && body2Immovable) || body1.customSeparateY || body2.customSeparateY)
{
2019-03-08 20:12:49 +00:00
// return true if there was some overlap, otherwise false.
return (overlap !== 0) || (body1.embedded && body2.embedded);
}
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;
if (faceTop)
{
body1.setTouchingUp();
body2.setTouchingDown();
if (blocked2.up)
{
2019-03-11 11:05:51 +00:00
// console.log('a1');
body1.setBlockedUp(body2);
}
else if (blocked1.up)
{
2019-03-11 11:05:51 +00:00
// console.log('a2');
body2.setBlockedUp(body1);
}
2019-03-11 11:05:51 +00:00
else if (blocked1.down)
{
// console.log('a3');
body2.setBlockedDown(body1);
2019-03-11 11:05:51 +00:00
}
else if (blocked2.down)
{
// console.log('a4');
body1.setBlockedDown(body2);
2019-03-11 11:05:51 +00:00
}
else if (body1Immovable)
{
if (v2 < 0)
{
// console.log('a5');
body2.setBlockedUp(body1);
2019-03-11 11:05:51 +00:00
}
else if (v2 > 0)
{
// console.log('a6');
body2.setBlockedDown(body1);
2019-03-11 11:05:51 +00:00
}
}
else if (body2Immovable)
{
if (v1 < 0)
{
// console.log('a7');
body1.setBlockedUp(body2);
2019-03-11 11:05:51 +00:00
}
else if (v1 > 0)
{
// console.log('a8');
body1.setBlockedDown(body2);
2019-03-11 11:05:51 +00:00
}
}
else
{
console.log('buma', blocked1.up, blocked2.up, blocked1.down, blocked2.down);
}
}
else
{
body1.setTouchingDown();
body2.setTouchingUp();
if (blocked2.down)
{
2019-03-11 11:05:51 +00:00
// console.log('b1');
body1.setBlockedDown(body2);
}
else if (blocked1.down)
{
2019-03-11 11:05:51 +00:00
// console.log('b2');
body2.setBlockedDown(body1);
}
else if (blocked1.up)
{
2019-03-11 11:05:51 +00:00
// console.log('b3');
body2.setBlockedUp(body1);
}
else if (blocked2.up)
{
2019-03-11 11:05:51 +00:00
// console.log('b4');
body1.setBlockedUp(body2);
}
2019-03-11 11:05:51 +00:00
else if (body1Immovable)
{
if (v2 < 0)
{
// console.log('b5');
body2.setBlockedUp(body1);
2019-03-11 11:05:51 +00:00
}
else if (v2 > 0)
{
// console.log('b6');
body2.setBlockedDown(body1);
2019-03-11 11:05:51 +00:00
}
}
else if (body2Immovable)
{
if (v1 < 0)
{
// console.log('b7');
body1.setBlockedUp(body2);
2019-03-11 11:05:51 +00:00
}
else if (v1 > 0)
{
// console.log('b8');
body1.setBlockedDown(body2);
2019-03-11 11:05:51 +00:00
}
}
else
{
2019-03-11 11:05:51 +00:00
console.log('bumb', blocked1.up, blocked2.up, blocked1.down, blocked2.down);
}
}
2019-03-08 20:12:49 +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.
if (!body1Immovable && !body2Immovable)
{
2019-03-08 20:12:49 +00:00
// Neither body is immovable, so they get an equal amount of separation and a new velocity based on mass
// Share the overlap equally if both bodies are unblocked
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);
var avg = (nv1 + nv2) * 0.5;
nv1 -= avg;
nv2 -= avg;
var ny1 = avg + nv1 * body1.bounce.y;
var ny2 = avg + nv2 * body2.bounce.y;
if (faceBottom && blocked2.down)
{
if (blocked1.by === body2)
{
body1.bottom = body2.y;
if (body1.bounce.y === 0)
{
ny1 = 0;
}
}
}
else if (faceTop && blocked1.down)
{
if (blocked2.by === body1)
{
body2.bottom = body1.y;
if (body2.bounce.y === 0)
{
ny2 = 0;
}
}
}
else if (faceBottom && blocked1.up)
{
if (blocked2.by === body1)
{
body2.y = body1.bottom;
if (body2.bounce.y === 0)
{
ny2 = 0;
}
}
}
else if (faceTop && blocked2.up)
{
if (blocked1.by === body2)
{
body1.y = body2.bottom;
if (body1.bounce.y === 0)
{
ny1 = 0;
}
}
2019-03-11 11:05:51 +00:00
}
else
{
overlap *= 0.5;
body1.y -= body1.getMoveY(overlap);
body2.y += body2.getMoveY(overlap);
}
velocity1.y = ny1;
velocity2.y = ny2;
}
2019-03-11 11:05:51 +00:00
else if (body1Immovable)
{
2019-03-11 11:05:51 +00:00
// Body1 is immovable, but Body2 can move, so it gets all the separation
2019-03-11 11:05:51 +00:00
if (faceBottom)
{
2019-03-11 11:05:51 +00:00
body2.y = body1.bottom;
// console.log('a');
}
else
{
body2.bottom = body1.y;
// console.log('b');
}
2019-03-08 20:12:49 +00:00
velocity2.y = v1 - v2 * body2.bounce.y;
// This is special case code that handles things like horizontal moving platforms you can ride
if (body1.moves)
{
2019-03-08 20:12:49 +00:00
body2.x += body2.getMoveX((body1.deltaX()) * body1.friction.x, true);
}
}
2019-03-11 11:05:51 +00:00
else if (body2Immovable)
2019-03-08 20:12:49 +00:00
{
2019-03-11 11:05:51 +00:00
// Body2 is immovable, but Body1 can move, so it gets all the separation
2019-03-08 20:12:49 +00:00
2019-03-11 11:05:51 +00:00
if (faceBottom)
2019-03-08 20:12:49 +00:00
{
2019-03-11 11:05:51 +00:00
body1.bottom = body2.y;
// console.log('a');
}
2019-03-11 11:05:51 +00:00
else
{
2019-03-11 11:05:51 +00:00
body1.y = body2.bottom;
// console.log('b');
2019-03-08 20:12:49 +00:00
}
2019-03-11 11:05:51 +00:00
velocity1.y = v2 - v1 * body1.bounce.y;
// This is special case code that handles things like horizontal moving platforms you can ride
if (body2.moves)
{
2019-03-11 11:05:51 +00:00
body1.x += body1.getMoveX((body2.deltaX()) * body2.friction.x, true);
}
2019-03-08 20:12:49 +00:00
}
// If we got this far then there WAS overlap, and separation is complete, so return true
return true;
};
module.exports = SeparateY;