phaser/src/physics/impact/SeparateX.js

51 lines
1.5 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.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2018-02-09 15:23:33 +00:00
/**
* [description]
*
2019-01-17 14:57:24 +00:00
* @function Phaser.Physics.Impact.SeparateX
2018-02-09 15:23:33 +00:00
* @since 3.0.0
*
* @param {Phaser.Physics.Impact.World} world - [description]
* @param {Phaser.Physics.Impact.Body} left - [description]
* @param {Phaser.Physics.Impact.Body} right - [description]
* @param {Phaser.Physics.Impact.Body} [weak] - [description]
*/
2019-01-17 14:57:24 +00:00
var SeparateX = function (world, left, right, weak)
2017-06-21 23:47:35 +00:00
{
var nudge = left.pos.x + left.size.x - right.pos.x;
// We have a weak entity, so just move this one
if (weak)
{
var strong = (left === weak) ? right : left;
weak.vel.x = -weak.vel.x * weak.bounciness + strong.vel.x;
2017-06-22 01:40:10 +00:00
var resWeak = world.collisionMap.trace(weak.pos.x, weak.pos.y, weak === left ? -nudge : nudge, 0, weak.size.x, weak.size.y);
2017-06-21 23:47:35 +00:00
weak.pos.x = resWeak.pos.x;
}
else
{
var v2 = (left.vel.x - right.vel.x) / 2;
left.vel.x = -v2;
right.vel.x = v2;
2017-06-22 01:40:10 +00:00
var resLeft = world.collisionMap.trace(left.pos.x, left.pos.y, -nudge / 2, 0, left.size.x, left.size.y);
2017-06-21 23:47:35 +00:00
left.pos.x = Math.floor(resLeft.pos.x);
2017-06-22 01:40:10 +00:00
var resRight = world.collisionMap.trace(right.pos.x, right.pos.y, nudge / 2, 0, right.size.x, right.size.y);
2017-06-21 23:47:35 +00:00
right.pos.x = Math.ceil(resRight.pos.x);
}
};
2019-01-17 14:57:24 +00:00
module.exports = SeparateX;