phaser/src/physics/impact/SeparateY.js

80 lines
2.3 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}
*/
2018-02-09 15:23:33 +00:00
/**
* [description]
*
2019-01-17 14:57:24 +00:00
* @function Phaser.Physics.Impact.SeparateY
2018-02-09 15:23:33 +00:00
* @since 3.0.0
*
* @param {Phaser.Physics.Impact.World} world - [description]
* @param {Phaser.Physics.Impact.Body} top - [description]
* @param {Phaser.Physics.Impact.Body} bottom - [description]
* @param {Phaser.Physics.Impact.Body} [weak] - [description]
*/
2019-01-17 14:57:24 +00:00
var SeparateY = function (world, top, bottom, weak)
2017-06-21 23:47:35 +00:00
{
var nudge = (top.pos.y + top.size.y - bottom.pos.y);
var nudgeX;
var resTop;
2017-06-21 23:47:35 +00:00
if (weak)
{
var strong = (top === weak) ? bottom : top;
weak.vel.y = -weak.vel.y * weak.bounciness + strong.vel.y;
// Riding on a platform?
nudgeX = 0;
2017-06-21 23:47:35 +00:00
if (weak === top && Math.abs(weak.vel.y - strong.vel.y) < weak.minBounceVelocity)
{
weak.standing = true;
nudgeX = strong.vel.x * world.delta;
}
var resWeak = world.collisionMap.trace(weak.pos.x, weak.pos.y, nudgeX, weak === top ? -nudge : nudge, weak.size.x, weak.size.y);
2017-06-21 23:47:35 +00:00
weak.pos.y = resWeak.pos.y;
weak.pos.x = resWeak.pos.x;
}
else if (world.gravity && (bottom.standing || top.vel.y > 0))
{
resTop = world.collisionMap.trace(top.pos.x, top.pos.y, 0, -(top.pos.y + top.size.y - bottom.pos.y), top.size.x, top.size.y);
2017-06-21 23:47:35 +00:00
top.pos.y = resTop.pos.y;
if (top.bounciness > 0 && top.vel.y > top.minBounceVelocity)
{
top.vel.y *= -top.bounciness;
2017-06-21 23:47:35 +00:00
}
else
{
top.standing = true;
top.vel.y = 0;
}
}
else
{
var v2 = (top.vel.y - bottom.vel.y) / 2;
top.vel.y = -v2;
bottom.vel.y = v2;
nudgeX = bottom.vel.x * world.delta;
2017-06-21 23:47:35 +00:00
resTop = world.collisionMap.trace(top.pos.x, top.pos.y, nudgeX, -nudge / 2, top.size.x, top.size.y);
2017-06-21 23:47:35 +00:00
top.pos.y = resTop.pos.y;
var resBottom = world.collisionMap.trace(bottom.pos.x, bottom.pos.y, 0, nudge / 2, bottom.size.x, bottom.size.y);
2017-06-21 23:47:35 +00:00
bottom.pos.y = resBottom.pos.y;
}
};
2019-01-17 14:57:24 +00:00
module.exports = SeparateY;