2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Phaser.Physics
|
|
|
|
*/
|
2013-09-03 14:35:40 +00:00
|
|
|
Phaser.Physics = {};
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* Arcade Physics constructor.
|
|
|
|
*
|
|
|
|
* @class Phaser.Physics.Arcade
|
|
|
|
* @classdesc Arcade Physics Constructor
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game reference to the current game instance.
|
|
|
|
*/
|
2013-09-03 14:35:40 +00:00
|
|
|
Phaser.Physics.Arcade = function (game) {
|
2013-10-09 03:31:08 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this.game = game;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} gravity - The World gravity setting. Defaults to x: 0, y: 0, or no gravity.
|
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.gravity = new Phaser.Point();
|
2013-09-03 14:35:40 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Rectangle} bounds - The bounds inside of which the physics world exists. Defaults to match the world bounds.
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
|
2013-09-04 02:48:15 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @property {number} OVERLAP_BIAS - A value added to the delta values during collision checks.
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this.OVERLAP_BIAS = 4;
|
2013-09-03 18:34:38 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
|
|
|
|
*/
|
2014-01-14 03:33:03 +00:00
|
|
|
this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} maxObjects - Used by the QuadTree to set the maximum number of objects per quad.
|
|
|
|
*/
|
|
|
|
this.maxObjects = 10;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
2014-01-14 03:33:03 +00:00
|
|
|
* @property {number} maxLevels - Used by the QuadTree to set the maximum number of iteration levels.
|
2013-10-25 15:54:40 +00:00
|
|
|
*/
|
2014-01-14 03:33:03 +00:00
|
|
|
this.maxLevels = 4;
|
2013-10-09 03:31:08 +00:00
|
|
|
|
|
|
|
// Avoid gc spikes by caching these values for re-use
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Rectangle} _bounds1 - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this._bounds1 = new Phaser.Rectangle();
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Rectangle} _bounds2 - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this._bounds2 = new Phaser.Rectangle();
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _overlap - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._overlap = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _maxOverlap - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._maxOverlap = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _velocity1 - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._velocity1 = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _velocity2 - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._velocity2 = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _newVelocity1 - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._newVelocity1 = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _newVelocity2 - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._newVelocity2 = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _average - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._average = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Array} _mapData - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-13 01:07:58 +00:00
|
|
|
this._mapData = [];
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _mapTiles - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
this._mapTiles = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _result - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-13 01:07:58 +00:00
|
|
|
this._result = false;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _total - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-09-13 01:07:58 +00:00
|
|
|
this._total = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _angle - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-08 20:09:46 +00:00
|
|
|
this._angle = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _dx - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._dx = 0;
|
2013-10-25 15:54:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _dy - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
this._dy = 0;
|
2013-09-03 16:28:12 +00:00
|
|
|
|
2014-01-14 03:33:03 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _gravityX - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._gravityX = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _gravityY - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._gravityY = 0;
|
2013-09-03 14:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.Arcade.prototype = {
|
|
|
|
|
2014-01-02 23:28:22 +00:00
|
|
|
/**
|
|
|
|
* Called automatically by a Physics body, it updates all motion related values on the Body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#updateMotion
|
|
|
|
* @param {Phaser.Physics.Arcade.Body} The Body object to be updated.
|
|
|
|
*/
|
|
|
|
updateMotion: function (body) {
|
|
|
|
|
|
|
|
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
|
|
|
|
|
2014-01-15 03:17:26 +00:00
|
|
|
// World gravity is allowed
|
2014-01-02 23:28:22 +00:00
|
|
|
if (body.allowGravity)
|
|
|
|
{
|
2014-01-14 03:33:03 +00:00
|
|
|
this._gravityX = this.gravity.x + body.gravity.x;
|
|
|
|
this._gravityY = this.gravity.y + body.gravity.y;
|
2014-01-02 23:28:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-14 03:33:03 +00:00
|
|
|
this._gravityX = body.gravity.x;
|
|
|
|
this._gravityY = body.gravity.y;
|
2014-01-02 23:28:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 03:17:26 +00:00
|
|
|
// Don't apply gravity to any body that is blocked
|
|
|
|
if ((this._gravityX < 0 && body.blocked.left) || (this._gravityX > 0 && body.blocked.right))
|
|
|
|
{
|
|
|
|
this._gravityX = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((this._gravityY < 0 && body.blocked.up) || (this._gravityY > 0 && body.blocked.down))
|
|
|
|
{
|
|
|
|
this._gravityY = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-02 23:28:22 +00:00
|
|
|
// Rotation
|
2014-01-03 04:50:31 +00:00
|
|
|
if (body.allowRotation)
|
2014-01-02 23:28:22 +00:00
|
|
|
{
|
2014-01-03 04:50:31 +00:00
|
|
|
this._velocityDelta = body.angularAcceleration * this.game.time.physicsElapsed;
|
2014-01-02 23:28:22 +00:00
|
|
|
|
2014-01-03 04:50:31 +00:00
|
|
|
if (body.angularDrag !== 0 && body.angularAcceleration === 0)
|
2014-01-02 23:28:22 +00:00
|
|
|
{
|
2014-01-03 04:50:31 +00:00
|
|
|
this._drag = body.angularDrag * this.game.time.physicsElapsed;
|
|
|
|
|
|
|
|
if (body.angularVelocity > 0)
|
|
|
|
{
|
|
|
|
body.angularVelocity -= this._drag;
|
|
|
|
}
|
|
|
|
else if (body.angularVelocity < 0)
|
|
|
|
{
|
|
|
|
body.angularVelocity += this._drag;
|
|
|
|
}
|
2014-01-02 23:28:22 +00:00
|
|
|
}
|
2014-01-03 04:50:31 +00:00
|
|
|
|
|
|
|
body.rotation += this.game.time.physicsElapsed * (body.angularVelocity + this._velocityDelta / 2);
|
|
|
|
body.angularVelocity += this._velocityDelta;
|
|
|
|
|
|
|
|
if (body.angularVelocity > body.maxAngular)
|
2014-01-02 23:28:22 +00:00
|
|
|
{
|
2014-01-03 04:50:31 +00:00
|
|
|
body.angularVelocity = body.maxAngular;
|
2014-01-02 23:28:22 +00:00
|
|
|
}
|
2014-01-03 04:50:31 +00:00
|
|
|
else if (body.angularVelocity < -body.maxAngular)
|
2014-01-02 23:28:22 +00:00
|
|
|
{
|
2014-01-03 04:50:31 +00:00
|
|
|
body.angularVelocity = -body.maxAngular;
|
2014-01-02 23:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 04:50:31 +00:00
|
|
|
// temp = acc*dt
|
|
|
|
// pos = pos + dt*(vel + temp/2)
|
|
|
|
// vel = vel + temp
|
|
|
|
|
|
|
|
body.motionVelocity.x = (body.acceleration.x + this._gravityX) * this.game.time.physicsElapsed;
|
|
|
|
body.motionVelocity.y = (body.acceleration.y + this._gravityY) * this.game.time.physicsElapsed;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-23 12:30:22 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* Checks for overlaps between two game objects. The objects can be Sprites, Groups or Emitters.
|
|
|
|
* You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks.
|
|
|
|
* Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
|
2013-10-23 12:30:22 +00:00
|
|
|
*
|
2013-10-25 15:54:40 +00:00
|
|
|
* @method Phaser.Physics.Arcade#overlap
|
2013-11-27 16:33:49 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
|
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
|
|
|
|
* @param {function} [overlapCallback=null] - An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them.
|
|
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then overlapCallback will only be called if processCallback returns true.
|
|
|
|
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
|
|
|
* @returns {boolean} True if an overlap occured otherwise false.
|
2013-10-25 15:54:40 +00:00
|
|
|
*/
|
2013-11-27 16:33:49 +00:00
|
|
|
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
|
|
|
|
|
|
|
|
overlapCallback = overlapCallback || null;
|
|
|
|
processCallback = processCallback || null;
|
|
|
|
callbackContext = callbackContext || overlapCallback;
|
|
|
|
|
|
|
|
this._result = false;
|
|
|
|
this._total = 0;
|
2013-10-23 12:30:22 +00:00
|
|
|
|
|
|
|
// Only test valid objects
|
|
|
|
if (object1 && object2 && object1.exists && object2.exists)
|
|
|
|
{
|
2013-11-27 16:33:49 +00:00
|
|
|
// SPRITES
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object1.type == Phaser.SPRITE || object1.type == Phaser.TILESPRITE)
|
2013-11-27 16:33:49 +00:00
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-11-27 16:33:49 +00:00
|
|
|
{
|
2014-01-14 21:51:55 +00:00
|
|
|
this.collideSpriteVsSprite(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
2013-11-27 16:33:49 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsGroup(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
2013-11-27 16:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// GROUPS
|
|
|
|
else if (object1.type == Phaser.GROUP)
|
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-11-27 16:33:49 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsGroup(object2, object1, overlapCallback, processCallback, callbackContext, true);
|
2013-11-27 16:33:49 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideGroupVsGroup(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
2013-11-27 16:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// EMITTER
|
|
|
|
else if (object1.type == Phaser.EMITTER)
|
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-11-27 16:33:49 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsGroup(object2, object1, overlapCallback, processCallback, callbackContext, true);
|
2013-11-27 16:33:49 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideGroupVsGroup(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
2013-11-27 16:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (this._total > 0);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-13 01:07:58 +00:00
|
|
|
/**
|
2014-01-14 01:18:46 +00:00
|
|
|
* Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
|
2014-01-20 20:14:34 +00:00
|
|
|
* If you'd like to collide an array of objects see Phaser.Physics.Arcade#collideArray.
|
2014-01-14 01:18:46 +00:00
|
|
|
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
|
|
|
|
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
|
|
|
|
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
|
|
|
|
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
|
2013-09-13 01:07:58 +00:00
|
|
|
*
|
2013-10-25 15:54:40 +00:00
|
|
|
* @method Phaser.Physics.Arcade#collide
|
2014-01-20 20:14:34 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
|
2014-01-14 01:18:46 +00:00
|
|
|
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
2013-10-25 15:54:40 +00:00
|
|
|
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @returns {boolean} True if a collision occured otherwise false.
|
2013-10-25 15:54:40 +00:00
|
|
|
*/
|
2013-09-12 23:53:03 +00:00
|
|
|
collide: function (object1, object2, collideCallback, processCallback, callbackContext) {
|
|
|
|
|
|
|
|
collideCallback = collideCallback || null;
|
|
|
|
processCallback = processCallback || null;
|
|
|
|
callbackContext = callbackContext || collideCallback;
|
|
|
|
|
2013-09-13 01:07:58 +00:00
|
|
|
this._result = false;
|
|
|
|
this._total = 0;
|
2013-09-12 23:53:03 +00:00
|
|
|
|
2014-01-20 20:14:34 +00:00
|
|
|
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext);
|
|
|
|
|
|
|
|
return (this._total > 0);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for collision between a game object and an array of game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
|
|
|
|
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
|
|
|
|
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
|
|
|
|
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
|
|
|
|
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#collideArray
|
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
|
|
|
* @param {<Phaser.Sprite>array|<Phaser.Group>array|<Phaser.Particles.Emitter>array|<Phaser.Tilemap>array} objectArray - An array of objects to check. Can contain instances of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
|
|
|
|
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
|
|
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
|
|
|
* @returns {boolean} True if a collision occured otherwise false.
|
|
|
|
*/
|
|
|
|
collideArray: function (object1, objectArray, collideCallback, processCallback, callbackContext) {
|
|
|
|
|
|
|
|
collideCallback = collideCallback || null;
|
|
|
|
processCallback = processCallback || null;
|
|
|
|
callbackContext = callbackContext || collideCallback;
|
|
|
|
|
|
|
|
this._result = false;
|
|
|
|
this._total = 0;
|
|
|
|
|
|
|
|
for (var i = 0, len = objectArray.length; i < len; i++)
|
|
|
|
{
|
|
|
|
this.collideHandler(object1, objectArray[i], collideCallback, processCallback, callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (this._total > 0);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal collision handler.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#collideHandler
|
|
|
|
* @private
|
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
|
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap. Can also be an array of objects to check.
|
|
|
|
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
|
|
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
|
|
|
*/
|
|
|
|
collideHandler: function (object1, object2, collideCallback, processCallback, callbackContext) {
|
|
|
|
|
2013-09-12 23:53:03 +00:00
|
|
|
// Only collide valid objects
|
|
|
|
if (object1 && object2 && object1.exists && object2.exists)
|
|
|
|
{
|
2013-09-13 03:22:12 +00:00
|
|
|
// SPRITES
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object1.type == Phaser.SPRITE || object1.type == Phaser.TILESPRITE)
|
2013-09-12 23:53:03 +00:00
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, false);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext, false);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-10-16 05:33:39 +00:00
|
|
|
else if (object2.type == Phaser.TILEMAPLAYER)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
this.collideSpriteVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
2013-09-13 03:22:12 +00:00
|
|
|
// GROUPS
|
|
|
|
else if (object1.type == Phaser.GROUP)
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, false);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, false);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-10-16 05:33:39 +00:00
|
|
|
else if (object2.type == Phaser.TILEMAPLAYER)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
2013-10-16 05:33:39 +00:00
|
|
|
// TILEMAP LAYERS
|
|
|
|
else if (object1.type == Phaser.TILEMAPLAYER)
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
this.collideSpriteVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
this.collideGroupVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
2013-09-13 03:22:12 +00:00
|
|
|
// EMITTER
|
|
|
|
else if (object1.type == Phaser.EMITTER)
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2013-12-17 16:48:03 +00:00
|
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, false);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
|
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, false);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-10-16 05:33:39 +00:00
|
|
|
else if (object2.type == Phaser.TILEMAPLAYER)
|
2013-09-13 03:22:12 +00:00
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
|
2013-09-13 03:22:12 +00:00
|
|
|
}
|
2013-09-12 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
|
|
*
|
2014-01-14 02:43:09 +00:00
|
|
|
* @method Phaser.Physics.Arcade#collideSpriteVsSprite
|
2013-10-25 15:54:40 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-01-14 02:43:09 +00:00
|
|
|
collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext, overlapOnly) {
|
2013-10-16 05:33:39 +00:00
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (this.separate(sprite1.body, sprite2.body, processCallback, callbackContext, overlapOnly))
|
2013-12-06 02:34:28 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
if (collideCallback)
|
2013-10-28 10:17:36 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
collideCallback.call(callbackContext, sprite1, sprite2);
|
2013-09-12 23:53:03 +00:00
|
|
|
}
|
2014-01-14 02:43:09 +00:00
|
|
|
|
|
|
|
this._total++;
|
2013-09-12 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 01:07:58 +00:00
|
|
|
},
|
2013-09-12 23:53:03 +00:00
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
|
|
*
|
2014-01-14 02:43:09 +00:00
|
|
|
* @method Phaser.Physics.Arcade#collideSpriteVsGroup
|
2013-10-25 15:54:40 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-01-14 02:43:09 +00:00
|
|
|
collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext, overlapOnly) {
|
2013-09-12 23:53:03 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (group.length === 0)
|
2013-09-24 14:28:29 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-12 23:53:03 +00:00
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
// What is the sprite colliding with in the quadtree?
|
|
|
|
this.quadTree.clear();
|
|
|
|
|
|
|
|
this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
|
|
|
|
|
|
|
|
this.quadTree.populate(group);
|
|
|
|
|
|
|
|
this._potentials = this.quadTree.retrieve(sprite);
|
|
|
|
|
|
|
|
for (var i = 0, len = this._potentials.length; i < len; i++)
|
2013-09-12 23:53:03 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
// We have our potential suspects, are they in this group?
|
|
|
|
if (this.separate(sprite.body, this._potentials[i], processCallback, callbackContext, overlapOnly))
|
2013-09-12 23:53:03 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
if (collideCallback)
|
2013-09-12 23:53:03 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
collideCallback.call(callbackContext, sprite, this._potentials[i].sprite);
|
2013-09-12 23:53:03 +00:00
|
|
|
}
|
2014-01-14 02:43:09 +00:00
|
|
|
|
|
|
|
this._total++;
|
2013-09-12 23:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 01:07:58 +00:00
|
|
|
},
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
|
|
*
|
2014-01-14 02:43:09 +00:00
|
|
|
* @method Phaser.Physics.Arcade#collideGroupVsGroup
|
2013-10-25 15:54:40 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-01-14 02:43:09 +00:00
|
|
|
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext, overlapOnly) {
|
2013-09-13 01:07:58 +00:00
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (group1.length === 0 || group2.length === 0)
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group1._container.first._iNext)
|
|
|
|
{
|
|
|
|
var currentNode = group1._container.first._iNext;
|
|
|
|
|
|
|
|
do
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
if (currentNode.exists)
|
|
|
|
{
|
|
|
|
this.collideSpriteVsGroup(currentNode, group2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
|
|
}
|
|
|
|
currentNode = currentNode._iNext;
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
2014-01-14 02:43:09 +00:00
|
|
|
while (currentNode != group1._container.last._iNext);
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
|
|
*
|
2014-01-14 02:43:09 +00:00
|
|
|
* @method Phaser.Physics.Arcade#collideSpriteVsTilemapLayer
|
2013-10-25 15:54:40 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-01-14 02:43:09 +00:00
|
|
|
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
this._mapData = tilemapLayer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
|
|
|
|
|
|
|
|
if (this._mapData.length === 0)
|
2013-09-24 14:49:21 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (this._mapData.length > 1)
|
2013-09-04 15:12:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
// console.log(' multi sep ---------------------------------------------------------------------------------------------');
|
|
|
|
this.separateTiles(sprite.body, this._mapData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
if (this.separateTile(sprite.body, this._mapData[i]))
|
2013-09-04 15:12:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
// They collided, is there a custom process callback?
|
|
|
|
if (processCallback)
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
if (processCallback.call(callbackContext, sprite, this._mapData[i]))
|
2013-09-04 15:12:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this._total++;
|
2014-01-14 01:18:46 +00:00
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (collideCallback)
|
|
|
|
{
|
|
|
|
collideCallback.call(callbackContext, sprite, this._mapData[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-14 01:18:46 +00:00
|
|
|
this._total++;
|
2014-01-14 02:43:09 +00:00
|
|
|
|
|
|
|
if (collideCallback)
|
|
|
|
{
|
|
|
|
collideCallback.call(callbackContext, sprite, this._mapData[i]);
|
|
|
|
}
|
2013-09-04 15:12:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
|
|
*
|
2014-01-14 02:43:09 +00:00
|
|
|
* @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer
|
2013-10-25 15:54:40 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-01-14 02:43:09 +00:00
|
|
|
collideGroupVsTilemapLayer: function (group, tilemapLayer, collideCallback, processCallback, callbackContext) {
|
2013-09-13 01:07:58 +00:00
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (group.length === 0)
|
2013-09-24 14:49:21 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (group._container.first._iNext)
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
var currentNode = group._container.first._iNext;
|
2013-09-13 01:07:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
do
|
2013-09-13 01:07:58 +00:00
|
|
|
{
|
|
|
|
if (currentNode.exists)
|
|
|
|
{
|
2014-01-14 02:43:09 +00:00
|
|
|
this.collideSpriteVsTilemapLayer(currentNode, tilemapLayer, collideCallback, processCallback, callbackContext);
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
|
|
|
currentNode = currentNode._iNext;
|
|
|
|
}
|
2014-01-14 02:43:09 +00:00
|
|
|
while (currentNode != group._container.last._iNext);
|
2013-09-13 01:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
|
|
|
* The core separation function to separate two physics bodies.
|
|
|
|
* @method Phaser.Physics.Arcade#separate
|
|
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
|
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate.
|
2014-01-14 01:18:46 +00:00
|
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this function is set then the sprites will only be collided if it returns true.
|
|
|
|
* @param {object} [callbackContext] - The context in which to run the process callback.
|
|
|
|
* @returns {boolean} Returns true if the bodies collided, otherwise false.
|
2013-10-25 15:54:40 +00:00
|
|
|
*/
|
2014-01-14 02:43:09 +00:00
|
|
|
separate: function (body1, body2, processCallback, callbackContext, overlapOnly) {
|
2014-01-08 01:53:28 +00:00
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
// Can't separate two immovable bodies and the same body cannot collide with itself
|
|
|
|
if (body1 === body2 || (body1.immovable && body2.immovable) || Phaser.Rectangle.intersects(body1, body2) === false)
|
2013-12-05 05:26:03 +00:00
|
|
|
{
|
2014-01-14 01:18:46 +00:00
|
|
|
return false;
|
2014-01-08 01:53:28 +00:00
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
|
|
|
|
// They overlap. Is there a custom process callback? If it returns true then we can carry on, otherwise we should abort.
|
|
|
|
if (processCallback && processCallback.call(callbackContext, body1.sprite, body2.sprite) === false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-20 20:14:34 +00:00
|
|
|
var r = false;
|
|
|
|
|
|
|
|
if (body1.deltaX() === 0 && body2.deltaX() === 0)
|
|
|
|
{
|
|
|
|
// They overlap but neither of them are moving
|
|
|
|
body1.embedded = true;
|
|
|
|
body2.embedded = true;
|
|
|
|
}
|
|
|
|
else if (body1.deltaX() != 0 && body2.deltaX() === 0)
|
|
|
|
{
|
|
|
|
r = body1.separate(body2);
|
|
|
|
}
|
|
|
|
else if (body2.deltaX() != 0 && body1.deltaX() === 0)
|
|
|
|
{
|
|
|
|
r = body2.separate(body1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Dual motion thingy
|
|
|
|
r = body1.separate(body2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return body2.separate(body1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
// We got this far, so the bodies overlap and our process was good, which means we can separate ...
|
|
|
|
this._overlap = 0;
|
|
|
|
|
|
|
|
this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS;
|
|
|
|
|
|
|
|
if (body1.deltaX() === 0 && body2.deltaX() === 0)
|
|
|
|
{
|
|
|
|
// They overlap but neither of them are moving
|
|
|
|
body1.embedded = true;
|
|
|
|
body2.embedded = true;
|
|
|
|
}
|
|
|
|
else if (body1.deltaX() > body2.deltaX())
|
|
|
|
{
|
|
|
|
// Body1 is moving right and/or Body2 is moving left
|
|
|
|
this._overlap = body1.x + body1.width - body2.x;
|
|
|
|
|
|
|
|
if ((this._overlap > this._maxOverlap) || body1.allowCollision.right === false || body2.allowCollision.left === false)
|
|
|
|
{
|
|
|
|
this._overlap = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.touching.right = true;
|
|
|
|
body2.touching.left = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body1.deltaX() < body2.deltaX())
|
|
|
|
{
|
|
|
|
// Body1 is moving left and/or Body2 is moving right
|
|
|
|
this._overlap = body1.x - body2.width - body2.x;
|
|
|
|
|
|
|
|
if ((-this._overlap > this._maxOverlap) || body1.allowCollision.left === false || body2.allowCollision.right === false)
|
|
|
|
{
|
|
|
|
this._overlap = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.touching.left = true;
|
|
|
|
body2.touching.right = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
|
|
|
if (this._overlap !== 0)
|
|
|
|
{
|
|
|
|
body1.overlapX = this._overlap;
|
|
|
|
body2.overlapX = this._overlap;
|
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (!overlapOnly && !body1.customSeparateX && !body2.customSeparateX)
|
2014-01-14 01:18:46 +00:00
|
|
|
{
|
|
|
|
this._velocity1 = body1.velocity.x;
|
|
|
|
this._velocity2 = body2.velocity.x;
|
|
|
|
|
|
|
|
if (!body1.immovable && !body2.immovable)
|
|
|
|
{
|
2014-01-15 03:17:26 +00:00
|
|
|
// If either of the bodies are blocked on either side then we don't exchange velocity but treat it like an immovable collision
|
|
|
|
if (body1.blocked.left || body1.blocked.right)
|
|
|
|
{
|
|
|
|
if (body1.blocked.left && body2.deltaX() < 0)
|
|
|
|
{
|
|
|
|
body2.x += this._overlap;
|
|
|
|
body2.velocity.x *= -body2.bounce.x;
|
|
|
|
}
|
|
|
|
else if (body1.blocked.right && body2.deltaX() > 0)
|
|
|
|
{
|
|
|
|
body2.x -= this._overlap;
|
|
|
|
body2.velocity.x *= -body2.bounce.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body2.blocked.left || body2.blocked.right)
|
|
|
|
{
|
|
|
|
if (body2.blocked.left && body1.deltaX() < 0)
|
|
|
|
{
|
|
|
|
body1.x += this._overlap;
|
|
|
|
body1.velocity.x *= -body1.bounce.x;
|
|
|
|
}
|
|
|
|
else if (body2.blocked.right && body1.deltaX() > 0)
|
|
|
|
{
|
|
|
|
body1.x -= this._overlap;
|
|
|
|
body1.velocity.x *= -body1.bounce.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Exchange velocities
|
|
|
|
this._overlap *= 0.5;
|
|
|
|
body1.overlapX = this._overlap;
|
|
|
|
body2.overlapX = this._overlap;
|
|
|
|
|
|
|
|
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
|
|
|
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
|
|
|
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
|
|
|
this._newVelocity1 -= this._average;
|
|
|
|
this._newVelocity2 -= this._average;
|
|
|
|
|
|
|
|
if (body1.deltaX() < 0)
|
|
|
|
{
|
|
|
|
body1.x += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.x -= this._overlap;
|
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
|
2014-01-15 03:17:26 +00:00
|
|
|
if (body2.deltaX() < 0)
|
|
|
|
{
|
|
|
|
body2.x += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body2.x -= this._overlap;
|
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
|
2014-01-15 03:17:26 +00:00
|
|
|
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
|
|
|
|
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
|
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
}
|
|
|
|
else if (!body1.immovable)
|
|
|
|
{
|
2014-01-15 03:17:26 +00:00
|
|
|
if (body1.deltaX() < 0)
|
|
|
|
{
|
|
|
|
body1.x += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.x -= this._overlap;
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
|
|
|
|
}
|
|
|
|
else if (!body2.immovable)
|
|
|
|
{
|
2014-01-15 03:17:26 +00:00
|
|
|
if (body2.deltaX() < 0)
|
|
|
|
{
|
|
|
|
body2.x += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body2.x -= this._overlap;
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now for the vertical
|
|
|
|
this._overlap = 0;
|
|
|
|
|
|
|
|
this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS;
|
|
|
|
|
|
|
|
if (body1.deltaY() === 0 && body2.deltaY() === 0)
|
|
|
|
{
|
|
|
|
// They overlap but neither of them are moving
|
|
|
|
body1.embedded = true;
|
|
|
|
body2.embedded = true;
|
|
|
|
}
|
|
|
|
else if (body1.deltaY() > body2.deltaY())
|
|
|
|
{
|
|
|
|
// Body1 is moving down and/or Body2 is moving up
|
|
|
|
this._overlap = body1.y + body1.height - body2.y;
|
|
|
|
|
|
|
|
if ((this._overlap > this._maxOverlap) || body1.allowCollision.down === false || body2.allowCollision.up === false)
|
|
|
|
{
|
|
|
|
this._overlap = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.touching.down = true;
|
|
|
|
body2.touching.up = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body1.deltaY() < body2.deltaY())
|
2014-01-08 01:53:28 +00:00
|
|
|
{
|
2014-01-14 01:18:46 +00:00
|
|
|
// Body1 is moving up and/or Body2 is moving down
|
|
|
|
this._overlap = body1.y - body2.height - body2.y;
|
|
|
|
|
|
|
|
if ((-this._overlap > this._maxOverlap) || body1.allowCollision.up === false || body2.allowCollision.down === false)
|
|
|
|
{
|
|
|
|
this._overlap = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.touching.up = true;
|
|
|
|
body2.touching.down = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
|
|
|
if (this._overlap !== 0)
|
|
|
|
{
|
|
|
|
body1.overlapY = this._overlap;
|
|
|
|
body2.overlapY = this._overlap;
|
|
|
|
|
2014-01-14 02:43:09 +00:00
|
|
|
if (!overlapOnly && !body1.customSeparateY && !body2.customSeparateY)
|
2014-01-14 01:18:46 +00:00
|
|
|
{
|
|
|
|
this._velocity1 = body1.velocity.y;
|
|
|
|
this._velocity2 = body2.velocity.y;
|
|
|
|
|
|
|
|
if (!body1.immovable && !body2.immovable)
|
|
|
|
{
|
2014-01-15 03:17:26 +00:00
|
|
|
// If either of the bodies are blocked on either side then we don't exchange velocity but treat it like an immovable collision
|
|
|
|
if (body1.blocked.up || body1.blocked.down)
|
|
|
|
{
|
|
|
|
if (body1.blocked.up && body2.deltaY() < 0)
|
|
|
|
{
|
|
|
|
body2.y += this._overlap;
|
|
|
|
body2.velocity.y *= -body2.bounce.y;
|
|
|
|
}
|
|
|
|
else if (body1.blocked.down && body2.deltaY() > 0)
|
|
|
|
{
|
|
|
|
body2.y -= this._overlap;
|
|
|
|
body2.velocity.y *= -body2.bounce.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body2.blocked.up || body2.blocked.down)
|
|
|
|
{
|
|
|
|
if (body2.blocked.up && body1.deltaY() < 0)
|
|
|
|
{
|
|
|
|
body1.y += this._overlap;
|
|
|
|
body1.velocity.y *= -body1.bounce.y;
|
|
|
|
}
|
|
|
|
else if (body2.blocked.down && body1.deltaY() > 0)
|
|
|
|
{
|
|
|
|
body1.y -= this._overlap;
|
|
|
|
body1.velocity.y *= -body1.bounce.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Exchange velocities
|
|
|
|
this._overlap *= 0.5;
|
|
|
|
body1.overlapY = this._overlap;
|
|
|
|
body2.overlapY = this._overlap;
|
|
|
|
|
|
|
|
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
|
|
|
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
|
|
|
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
|
|
|
this._newVelocity1 -= this._average;
|
|
|
|
this._newVelocity2 -= this._average;
|
|
|
|
|
|
|
|
if (body1.deltaY() < 0)
|
|
|
|
{
|
|
|
|
body1.y += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.y -= this._overlap;
|
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
|
2014-01-15 03:17:26 +00:00
|
|
|
if (body2.deltaY() < 0)
|
|
|
|
{
|
|
|
|
body2.y += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body2.y -= this._overlap;
|
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
|
2014-01-15 03:17:26 +00:00
|
|
|
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
|
|
|
|
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
|
|
|
|
}
|
2014-01-14 01:18:46 +00:00
|
|
|
}
|
|
|
|
else if (!body1.immovable)
|
|
|
|
{
|
2014-01-15 03:17:26 +00:00
|
|
|
if (body1.deltaY() < 0)
|
|
|
|
{
|
|
|
|
body1.y += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body1.y -= this._overlap;
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
|
|
|
|
|
|
|
|
// This is special case code that handles things like horizontal moving platforms you can ride
|
|
|
|
if (body2.moves)
|
|
|
|
{
|
|
|
|
body1.x += body2.x - body2.preX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!body2.immovable)
|
|
|
|
{
|
2014-01-15 03:17:26 +00:00
|
|
|
if (body2.deltaY() < 0)
|
|
|
|
{
|
|
|
|
body2.y += this._overlap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body2.y -= this._overlap;
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
|
|
|
|
|
|
|
|
// This is special case code that handles things like horizontal moving platforms you can ride
|
|
|
|
if (body1.moves)
|
|
|
|
{
|
|
|
|
body2.x += body1.x - body1.preX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 05:26:03 +00:00
|
|
|
}
|
2014-01-08 01:53:28 +00:00
|
|
|
|
2014-01-14 01:18:46 +00:00
|
|
|
return true;
|
2014-01-20 20:14:34 +00:00
|
|
|
*/
|
2014-01-14 01:18:46 +00:00
|
|
|
|
2013-09-03 16:28:12 +00:00
|
|
|
},
|
|
|
|
|
2013-10-25 15:54:40 +00:00
|
|
|
/**
|
2013-12-06 04:34:27 +00:00
|
|
|
* The core separation function to separate a physics body and an array of tiles.
|
|
|
|
* @method Phaser.Physics.Arcade#separateTiles
|
2014-01-14 00:31:58 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
|
|
|
|
* @param {<Phaser.Tile>array} tiles - The array of tiles to collide against.
|
2013-10-25 15:54:40 +00:00
|
|
|
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
|
|
|
*/
|
2013-12-06 02:34:28 +00:00
|
|
|
separateTiles: function (body, tiles) {
|
|
|
|
|
|
|
|
// Can't separate two immovable objects (tiles are always immovable)
|
|
|
|
if (body.immovable)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
body.overlapX = 0;
|
|
|
|
body.overlapY = 0;
|
|
|
|
|
|
|
|
var tile;
|
|
|
|
var localOverlapX = 0;
|
|
|
|
var localOverlapY = 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < tiles.length; i++)
|
|
|
|
{
|
|
|
|
tile = tiles[i];
|
|
|
|
|
|
|
|
if (Phaser.Rectangle.intersects(body, tile))
|
|
|
|
{
|
2014-01-14 22:34:41 +00:00
|
|
|
// They overlap. Any custom callbacks?
|
|
|
|
if (tile.tile.callback || tile.layer.callbacks[tile.tile.index])
|
|
|
|
{
|
|
|
|
// A local callback takes priority over a global callback.
|
|
|
|
if (tile.tile.callback && tile.tile.callback.call(tile.tile.callbackContext, body.sprite, tile) === false)
|
|
|
|
{
|
|
|
|
// Is there a tile specific collision callback? If it returns true then we can carry on, otherwise we should abort.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (tile.layer.callbacks[tile.tile.index] && tile.layer.callbacks[tile.tile.index].callback.call(tile.layer.callbacks[tile.tile.index].callbackContext, body.sprite, tile) === false)
|
|
|
|
{
|
|
|
|
// Is there a tile index collision callback? If it returns true then we can carry on, otherwise we should abort.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 02:34:28 +00:00
|
|
|
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
|
|
|
|
{
|
|
|
|
// LEFT
|
|
|
|
localOverlapX = body.x - tile.right;
|
|
|
|
|
|
|
|
if (localOverlapX >= body.deltaX())
|
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('m left overlapX', localOverlapX, body.deltaX());
|
2013-12-06 02:34:28 +00:00
|
|
|
// use touching instead of blocked?
|
|
|
|
body.blocked.left = true;
|
|
|
|
body.touching.left = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body.deltaX() > 0 && body.allowCollision.right && tile.tile.faceLeft)
|
|
|
|
{
|
|
|
|
// RIGHT
|
|
|
|
localOverlapX = body.right - tile.x;
|
|
|
|
|
|
|
|
// Distance check
|
|
|
|
if (localOverlapX <= body.deltaX())
|
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('m right overlapX', localOverlapX, body.deltaX());
|
2013-12-06 02:34:28 +00:00
|
|
|
body.blocked.right = true;
|
|
|
|
body.touching.right = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
|
|
|
|
{
|
|
|
|
// UP
|
|
|
|
localOverlapY = body.y - tile.bottom;
|
|
|
|
|
|
|
|
// Distance check
|
|
|
|
if (localOverlapY >= body.deltaY())
|
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('m up overlapY', localOverlapY, body.deltaY());
|
2013-12-06 02:34:28 +00:00
|
|
|
body.blocked.up = true;
|
|
|
|
body.touching.up = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
|
|
|
|
{
|
|
|
|
// DOWN
|
|
|
|
localOverlapY = body.bottom - tile.y;
|
|
|
|
|
|
|
|
if (localOverlapY <= body.deltaY())
|
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('m down overlapY', localOverlapY, body.deltaY());
|
2013-12-06 02:34:28 +00:00
|
|
|
body.blocked.down = true;
|
|
|
|
body.touching.down = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localOverlapX !== 0)
|
|
|
|
{
|
|
|
|
body.overlapX = localOverlapX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localOverlapY !== 0)
|
|
|
|
{
|
|
|
|
body.overlapY = localOverlapY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.touching.none)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
if (body.touching.left || body.touching.right)
|
2013-12-06 02:34:28 +00:00
|
|
|
{
|
|
|
|
body.x -= body.overlapX;
|
|
|
|
body.preX -= body.overlapX;
|
|
|
|
|
|
|
|
if (body.bounce.x === 0)
|
|
|
|
{
|
|
|
|
body.velocity.x = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body.velocity.x = -body.velocity.x * body.bounce.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
if (body.touching.up || body.touching.down)
|
2013-12-06 02:34:28 +00:00
|
|
|
{
|
|
|
|
body.y -= body.overlapY;
|
|
|
|
body.preY -= body.overlapY;
|
|
|
|
|
|
|
|
if (body.bounce.y === 0)
|
|
|
|
{
|
|
|
|
body.velocity.y = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body.velocity.y = -body.velocity.y * body.bounce.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
/**
|
|
|
|
* The core separation function to separate a physics body and a tile.
|
|
|
|
* @method Phaser.Physics.Arcade#separateTile
|
|
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
|
|
|
* @param {Phaser.Tile} tile - The tile to collide against.
|
|
|
|
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
separateTile: function (body, tile) {
|
|
|
|
|
|
|
|
// Can't separate two immovable objects (tiles are always immovable)
|
|
|
|
if (body.immovable || Phaser.Rectangle.intersects(body, tile) === false)
|
|
|
|
{
|
2013-12-06 02:34:28 +00:00
|
|
|
// console.log('no intersects');
|
|
|
|
// console.log('tx', tile.x, 'ty', tile.y, 'tw', tile.width, 'th', tile.height, 'tr', tile.right, 'tb', tile.bottom);
|
|
|
|
// console.log('bx', body.x, 'by', body.y, 'bw', body.width, 'bh', body.height, 'br', body.right, 'bb', body.bottom);
|
2013-12-05 18:12:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:34:41 +00:00
|
|
|
// They overlap. Any custom callbacks?
|
|
|
|
if (tile.tile.callback || tile.layer.callbacks[tile.tile.index])
|
|
|
|
{
|
|
|
|
// A local callback takes priority over a global callback.
|
|
|
|
if (tile.tile.callback && tile.tile.callback.call(tile.tile.callbackContext, body.sprite, tile) === false)
|
|
|
|
{
|
|
|
|
// Is there a tile specific collision callback? If it returns true then we can carry on, otherwise we should abort.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (tile.layer.callbacks[tile.tile.index] && tile.layer.callbacks[tile.tile.index].callback.call(tile.layer.callbacks[tile.tile.index].callbackContext, body.sprite, tile) === false)
|
|
|
|
{
|
|
|
|
// Is there a tile index collision callback? If it returns true then we can carry on, otherwise we should abort.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:07:25 +00:00
|
|
|
// use body var instead
|
|
|
|
body.overlapX = 0;
|
|
|
|
body.overlapY = 0;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-12-06 01:07:25 +00:00
|
|
|
// Remember - this happens AFTER the body has been moved by the motion update, so it needs moving back again
|
|
|
|
// console.log('---------------------------------------------------------------------------------------------');
|
2014-01-14 22:34:41 +00:00
|
|
|
// console.log(tile);
|
2013-12-06 01:07:25 +00:00
|
|
|
// console.log('tx', tile.x, 'ty', tile.y, 'tw', tile.width, 'th', tile.height, 'tr', tile.right, 'tb', tile.bottom);
|
|
|
|
// console.log('bx', body.x, 'by', body.y, 'bw', body.width, 'bh', body.height, 'br', body.right, 'bb', body.bottom);
|
|
|
|
// console.log(Phaser.Rectangle.intersects(body, tile));
|
|
|
|
// console.log('dx', body.deltaX(), 'dy', body.deltaY(), 'dax', body.deltaAbsX(), 'day', body.deltaAbsY(), 'cax', Math.ceil(body.deltaAbsX()), 'cay', Math.ceil(body.deltaAbsY()));
|
2013-12-05 18:12:16 +00:00
|
|
|
|
|
|
|
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
|
|
|
|
{
|
|
|
|
// LEFT
|
2013-12-06 01:07:25 +00:00
|
|
|
body.overlapX = body.x - tile.right;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-12-06 02:34:28 +00:00
|
|
|
if (body.overlapX >= body.deltaX())
|
2013-12-06 01:07:25 +00:00
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('left overlapX', body.overlapX, body.deltaX());
|
2013-12-06 01:07:25 +00:00
|
|
|
// use touching instead of blocked?
|
|
|
|
body.blocked.left = true;
|
2013-12-05 18:12:16 +00:00
|
|
|
body.touching.left = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body.deltaX() > 0 && body.allowCollision.right && tile.tile.faceLeft)
|
|
|
|
{
|
|
|
|
// RIGHT
|
2013-12-06 01:07:25 +00:00
|
|
|
body.overlapX = body.right - tile.x;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-12-06 01:07:25 +00:00
|
|
|
// Distance check
|
2013-12-06 02:34:28 +00:00
|
|
|
if (body.overlapX <= body.deltaX())
|
2013-12-06 01:07:25 +00:00
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('right overlapX', body.overlapX, body.deltaX());
|
2013-12-06 01:07:25 +00:00
|
|
|
body.blocked.right = true;
|
2013-12-05 18:12:16 +00:00
|
|
|
body.touching.right = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
|
|
|
|
{
|
|
|
|
// UP
|
2013-12-06 01:07:25 +00:00
|
|
|
body.overlapY = body.y - tile.bottom;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-12-06 01:07:25 +00:00
|
|
|
// Distance check
|
2013-12-06 02:34:28 +00:00
|
|
|
if (body.overlapY >= body.deltaY())
|
2013-12-06 01:07:25 +00:00
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('up overlapY', body.overlapY, body.deltaY());
|
2013-12-06 01:07:25 +00:00
|
|
|
body.blocked.up = true;
|
2013-12-05 18:12:16 +00:00
|
|
|
body.touching.up = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
|
|
|
|
{
|
|
|
|
// DOWN
|
2013-12-06 01:07:25 +00:00
|
|
|
body.overlapY = body.bottom - tile.y;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-12-06 02:34:28 +00:00
|
|
|
if (body.overlapY <= body.deltaY())
|
2013-12-06 01:07:25 +00:00
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('down overlapY', body.overlapY, body.deltaY());
|
2013-12-06 01:07:25 +00:00
|
|
|
body.blocked.down = true;
|
2013-12-05 18:12:16 +00:00
|
|
|
body.touching.down = true;
|
|
|
|
body.touching.none = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Separate in a single sweep
|
|
|
|
|
2013-12-06 01:07:25 +00:00
|
|
|
if (body.touching.none)
|
2013-12-05 18:12:16 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
// if (body.overlapX !== 0)
|
|
|
|
if (body.touching.left || body.touching.right)
|
2013-12-05 18:12:16 +00:00
|
|
|
{
|
2013-12-06 01:07:25 +00:00
|
|
|
body.x -= body.overlapX;
|
|
|
|
body.preX -= body.overlapX;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
|
|
|
if (body.bounce.x === 0)
|
|
|
|
{
|
2013-12-06 02:34:28 +00:00
|
|
|
body.velocity.x = 0;
|
2013-12-05 18:12:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-06 02:34:28 +00:00
|
|
|
body.velocity.x = -body.velocity.x * body.bounce.x;
|
2013-12-05 18:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
// if (body.overlapY !== 0)
|
|
|
|
if (body.touching.up || body.touching.down)
|
2013-12-05 18:12:16 +00:00
|
|
|
{
|
2013-12-06 01:07:25 +00:00
|
|
|
body.y -= body.overlapY;
|
|
|
|
body.preY -= body.overlapY;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
|
|
|
if (body.bounce.y === 0)
|
|
|
|
{
|
2013-12-06 02:34:28 +00:00
|
|
|
body.velocity.y = 0;
|
2013-12-05 18:12:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-06 02:34:28 +00:00
|
|
|
body.velocity.y = -body.velocity.y * body.bounce.y;
|
2013-12-05 18:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-04 15:12:58 +00:00
|
|
|
/**
|
2013-10-09 06:11:36 +00:00
|
|
|
* Move the given display object towards the destination object at a steady velocity.
|
2013-10-08 20:09:46 +00:00
|
|
|
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
|
|
|
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
2013-10-09 03:31:08 +00:00
|
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
2013-10-08 20:09:46 +00:00
|
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
2013-09-04 15:12:58 +00:00
|
|
|
*
|
2013-10-09 03:31:08 +00:00
|
|
|
* @method Phaser.Physics.Arcade#moveToObject
|
2013-10-08 20:09:46 +00:00
|
|
|
* @param {any} displayObject - The display object to move.
|
|
|
|
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
|
|
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
2013-09-04 15:12:58 +00:00
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
moveToObject: function (displayObject, destination, speed, maxTime) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
|
|
if (typeof maxTime === 'undefined') { maxTime = 0; }
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 06:11:36 +00:00
|
|
|
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displayObject.x);
|
2013-10-08 20:09:46 +00:00
|
|
|
|
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
// We know how many pixels we need to move, but how fast?
|
2013-10-09 03:31:08 +00:00
|
|
|
speed = this.distanceBetween(displayObject, destination) / (maxTime / 1000);
|
2013-10-08 20:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
|
|
|
|
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
return this._angle;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-08 20:09:46 +00:00
|
|
|
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
|
|
|
|
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
|
|
|
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
2013-10-09 03:31:08 +00:00
|
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
2013-10-08 20:09:46 +00:00
|
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
2013-09-04 15:12:58 +00:00
|
|
|
*
|
2013-10-09 03:31:08 +00:00
|
|
|
* @method Phaser.Physics.Arcade#moveToPointer
|
2013-10-08 20:09:46 +00:00
|
|
|
* @param {any} displayObject - The display object to move.
|
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
|
|
* @param {Phaser.Pointer} [pointer] - The pointer to move towards. Defaults to Phaser.Input.activePointer.
|
|
|
|
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
2013-09-04 15:12:58 +00:00
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
moveToPointer: function (displayObject, speed, pointer, maxTime) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
2013-10-08 20:09:46 +00:00
|
|
|
pointer = pointer || this.game.input.activePointer;
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof maxTime === 'undefined') { maxTime = 0; }
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._angle = this.angleToPointer(displayObject, pointer);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
// We know how many pixels we need to move, but how fast?
|
2013-10-09 03:31:08 +00:00
|
|
|
speed = this.distanceToPointer(displayObject, pointer) / (maxTime / 1000);
|
2013-09-04 15:12:58 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
|
|
|
|
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
return this._angle;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Move the given display object towards the x/y coordinates at a steady velocity.
|
2013-10-08 20:09:46 +00:00
|
|
|
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
|
|
|
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
2013-10-09 03:31:08 +00:00
|
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
2013-10-08 20:09:46 +00:00
|
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
|
|
|
*
|
2013-10-09 03:31:08 +00:00
|
|
|
* @method Phaser.Physics.Arcade#moveToXY
|
2013-10-08 20:09:46 +00:00
|
|
|
* @param {any} displayObject - The display object to move.
|
2013-10-09 03:31:08 +00:00
|
|
|
* @param {number} x - The x coordinate to move towards.
|
|
|
|
* @param {number} y - The y coordinate to move towards.
|
2013-10-08 20:09:46 +00:00
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
|
|
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
moveToXY: function (displayObject, x, y, speed, maxTime) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
|
|
if (typeof maxTime === 'undefined') { maxTime = 0; }
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._angle = Math.atan2(y - displayObject.y, x - displayObject.x);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
// We know how many pixels we need to move, but how fast?
|
2013-10-09 03:31:08 +00:00
|
|
|
speed = this.distanceToXY(displayObject, x, y) / (maxTime / 1000);
|
2013-09-04 15:12:58 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
|
|
|
|
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
|
|
|
|
|
|
|
|
return this._angle;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-08 20:09:46 +00:00
|
|
|
* Given the angle (in degrees) and speed calculate the velocity and return it as a Point object, or set it to the given point object.
|
|
|
|
* One way to use this is: velocityFromAngle(angle, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#velocityFromAngle
|
|
|
|
* @param {number} angle - The angle in degrees calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
|
|
|
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated velocity.
|
|
|
|
* @return {Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
|
|
|
|
*/
|
|
|
|
velocityFromAngle: function (angle, speed, point) {
|
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
2013-11-25 04:40:04 +00:00
|
|
|
point = point || new Phaser.Point();
|
2013-10-08 20:09:46 +00:00
|
|
|
|
|
|
|
return point.setTo((Math.cos(this.game.math.degToRad(angle)) * speed), (Math.sin(this.game.math.degToRad(angle)) * speed));
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-08 20:09:46 +00:00
|
|
|
* Given the rotation (in radians) and speed calculate the velocity and return it as a Point object, or set it to the given point object.
|
|
|
|
* One way to use this is: velocityFromRotation(rotation, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#velocityFromRotation
|
|
|
|
* @param {number} rotation - The angle in radians.
|
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
|
|
|
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated velocity.
|
|
|
|
* @return {Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
|
|
|
|
*/
|
|
|
|
velocityFromRotation: function (rotation, speed, point) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
2013-11-25 04:40:04 +00:00
|
|
|
point = point || new Phaser.Point();
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Given the rotation (in radians) and speed calculate the acceleration and return it as a Point object, or set it to the given point object.
|
2013-12-06 04:34:27 +00:00
|
|
|
* One way to use this is: accelerationFromRotation(rotation, 200, sprite.acceleration) which will set the values directly to the sprites acceleration and not create a new Point object.
|
2013-10-09 03:31:08 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#accelerationFromRotation
|
|
|
|
* @param {number} rotation - The angle in radians.
|
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
|
|
|
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated acceleration.
|
|
|
|
* @return {Phaser.Point} - A Point where point.x contains the acceleration x value and point.y contains the acceleration y value.
|
|
|
|
*/
|
|
|
|
accelerationFromRotation: function (rotation, speed, point) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
2013-11-25 04:40:04 +00:00
|
|
|
point = point || new Phaser.Point();
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.)
|
|
|
|
* You must give a maximum speed value, beyond which the display object won't go any faster.
|
|
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#accelerateToObject
|
|
|
|
* @param {any} displayObject - The display object to move.
|
|
|
|
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
|
|
|
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
|
|
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
|
|
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
|
|
|
|
*/
|
|
|
|
accelerateToObject: function (displayObject, destination, speed, xSpeedMax, ySpeedMax) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
|
|
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
|
|
|
|
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._angle = this.angleBetween(displayObject, destination);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
|
|
|
|
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return this._angle;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.)
|
|
|
|
* You must give a maximum speed value, beyond which the display object won't go any faster.
|
|
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#accelerateToPointer
|
|
|
|
* @param {any} displayObject - The display object to move.
|
|
|
|
* @param {Phaser.Pointer} [pointer] - The pointer to move towards. Defaults to Phaser.Input.activePointer.
|
|
|
|
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
|
|
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
|
|
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
|
|
|
|
*/
|
|
|
|
accelerateToPointer: function (displayObject, pointer, speed, xSpeedMax, ySpeedMax) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
|
|
if (typeof pointer === 'undefined') { pointer = this.game.input.activePointer; }
|
|
|
|
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
|
|
|
|
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._angle = this.angleToPointer(displayObject, pointer);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
|
|
|
|
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
|
|
|
|
|
|
|
|
return this._angle;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Sets the acceleration.x/y property on the display object so it will move towards the x/y coordinates at the given speed (in pixels per second sq.)
|
|
|
|
* You must give a maximum speed value, beyond which the display object won't go any faster.
|
|
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#accelerateToXY
|
|
|
|
* @param {any} displayObject - The display object to move.
|
|
|
|
* @param {number} x - The x coordinate to accelerate towards.
|
|
|
|
* @param {number} y - The y coordinate to accelerate towards.
|
|
|
|
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
|
|
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
|
|
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
|
|
|
|
*/
|
|
|
|
accelerateToXY: function (displayObject, x, y, speed, xSpeedMax, ySpeedMax) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
|
|
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
|
|
|
|
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
|
|
|
|
|
|
|
|
this._angle = this.angleToXY(displayObject, x, y);
|
|
|
|
|
|
|
|
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
|
|
|
|
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
|
|
|
|
|
|
|
|
return this._angle;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Find the distance between two display objects (like Sprites).
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#distanceBetween
|
|
|
|
* @param {any} source - The Display Object to test from.
|
|
|
|
* @param {any} target - The Display Object to test to.
|
|
|
|
* @return {number} The distance between the source and target objects.
|
|
|
|
*/
|
|
|
|
distanceBetween: function (source, target) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._dx = source.x - target.x;
|
|
|
|
this._dy = source.y - target.y;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Find the distance between a display object (like a Sprite) and the given x/y coordinates.
|
|
|
|
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
|
|
|
|
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#distanceToXY
|
|
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
|
|
* @param {number} x - The x coordinate to move towards.
|
|
|
|
* @param {number} y - The y coordinate to move towards.
|
|
|
|
* @return {number} The distance between the object and the x/y coordinates.
|
|
|
|
*/
|
|
|
|
distanceToXY: function (displayObject, x, y) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._dx = displayObject.x - x;
|
|
|
|
this._dy = displayObject.y - y;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Find the distance between a display object (like a Sprite) and a Pointer. If no Pointer is given the Input.activePointer is used.
|
|
|
|
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
|
|
|
|
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#distanceToPointer
|
|
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
|
|
* @param {Phaser.Pointer} [pointer] - The Phaser.Pointer to test to. If none is given then Input.activePointer is used.
|
|
|
|
* @return {number} The distance between the object and the Pointer.
|
|
|
|
*/
|
|
|
|
distanceToPointer: function (displayObject, pointer) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
pointer = pointer || this.game.input.activePointer;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-28 10:17:36 +00:00
|
|
|
this._dx = displayObject.x - pointer.x;
|
|
|
|
this._dy = displayObject.y - pointer.y;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Find the angle in radians between two display objects (like Sprites).
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#angleBetween
|
|
|
|
* @param {any} source - The Display Object to test from.
|
|
|
|
* @param {any} target - The Display Object to test to.
|
|
|
|
* @return {number} The angle in radians between the source and target display objects.
|
|
|
|
*/
|
|
|
|
angleBetween: function (source, target) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._dx = target.x - source.x;
|
|
|
|
this._dy = target.y - source.y;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return Math.atan2(this._dy, this._dx);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-09 03:31:08 +00:00
|
|
|
* Find the angle in radians between a display object (like a Sprite) and the given x/y coordinate.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#angleToXY
|
|
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
|
|
* @param {number} x - The x coordinate to get the angle to.
|
|
|
|
* @param {number} y - The y coordinate to get the angle to.
|
|
|
|
* @return {number} The angle in radians between displayObject.x/y to Pointer.x/y
|
|
|
|
*/
|
|
|
|
angleToXY: function (displayObject, x, y) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
this._dx = x - displayObject.x;
|
|
|
|
this._dy = y - displayObject.y;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return Math.atan2(this._dy, this._dx);
|
2013-09-04 15:12:58 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-08 20:09:46 +00:00
|
|
|
* Find the angle in radians between a display object (like a Sprite) and a Pointer, taking their x/y and center into account.
|
|
|
|
*
|
2013-10-09 03:31:08 +00:00
|
|
|
* @method Phaser.Physics.Arcade#angleToPointer
|
2013-10-08 20:09:46 +00:00
|
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
|
|
* @param {Phaser.Pointer} [pointer] - The Phaser.Pointer to test to. If none is given then Input.activePointer is used.
|
2013-10-09 03:31:08 +00:00
|
|
|
* @return {number} The angle in radians between displayObject.x/y to Pointer.x/y
|
2013-10-08 20:09:46 +00:00
|
|
|
*/
|
2013-10-09 03:31:08 +00:00
|
|
|
angleToPointer: function (displayObject, pointer) {
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
pointer = pointer || this.game.input.activePointer;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 04:13:38 +00:00
|
|
|
this._dx = pointer.worldX - displayObject.x;
|
|
|
|
this._dy = pointer.worldY - displayObject.y;
|
2013-09-04 15:12:58 +00:00
|
|
|
|
2013-10-09 03:31:08 +00:00
|
|
|
return Math.atan2(this._dy, this._dx);
|
2013-10-08 20:09:46 +00:00
|
|
|
|
2013-09-04 15:12:58 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade;
|