phaser/plugins/impact/Solver.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 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 16:04:43 +00:00
var COLLIDES = require('./COLLIDES');
2019-01-17 14:54:38 +00:00
var Events = require('./events');
2019-01-17 14:57:24 +00:00
var SeparateX = require('./SeparateX');
var SeparateY = require('./SeparateY');
2017-06-21 23:47:35 +00:00
2018-02-09 16:04:43 +00:00
/**
* Impact Physics Solver
*
* @function Phaser.Physics.Impact.Solver
2019-01-17 14:54:38 +00:00
* @fires Phaser.Physics.Impact.Events#COLLIDE
2018-02-09 16:04:43 +00:00
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {Phaser.Physics.Impact.World} world - The Impact simulation to run the solver in.
* @param {Phaser.Physics.Impact.Body} bodyA - The first body in the collision.
* @param {Phaser.Physics.Impact.Body} bodyB - The second body in the collision.
2018-02-09 16:04:43 +00:00
*/
2017-06-21 23:47:35 +00:00
var Solver = function (world, bodyA, bodyB)
{
var weak = null;
if (bodyA.collides === COLLIDES.LITE || bodyB.collides === COLLIDES.FIXED)
{
weak = bodyA;
}
else if (bodyB.collides === COLLIDES.LITE || bodyA.collides === COLLIDES.FIXED)
{
weak = bodyB;
}
if (bodyA.last.x + bodyA.size.x > bodyB.last.x && bodyA.last.x < bodyB.last.x + bodyB.size.x)
{
if (bodyA.last.y < bodyB.last.y)
{
2019-01-17 14:57:24 +00:00
SeparateY(world, bodyA, bodyB, weak);
2017-06-21 23:47:35 +00:00
}
else
{
2019-01-17 14:57:24 +00:00
SeparateY(world, bodyB, bodyA, weak);
2017-06-21 23:47:35 +00:00
}
bodyA.collideWith(bodyB, 'y');
bodyB.collideWith(bodyA, 'y');
2017-08-16 15:30:28 +00:00
2019-01-17 14:54:38 +00:00
world.emit(Events.COLLIDE, bodyA, bodyB, 'y');
2017-06-21 23:47:35 +00:00
}
else if (bodyA.last.y + bodyA.size.y > bodyB.last.y && bodyA.last.y < bodyB.last.y + bodyB.size.y)
{
if (bodyA.last.x < bodyB.last.x)
{
2019-01-17 14:57:24 +00:00
SeparateX(world, bodyA, bodyB, weak);
2017-06-21 23:47:35 +00:00
}
else
{
2019-01-17 14:57:24 +00:00
SeparateX(world, bodyB, bodyA, weak);
2017-06-21 23:47:35 +00:00
}
bodyA.collideWith(bodyB, 'x');
bodyB.collideWith(bodyA, 'x');
2017-08-16 15:30:28 +00:00
2019-01-17 14:54:38 +00:00
world.emit(Events.COLLIDE, bodyA, bodyB, 'x');
2017-06-21 23:47:35 +00:00
}
};
module.exports = Solver;