phaser/src/physics/arcade/ArcadePhysics.js

1382 lines
52 KiB
JavaScript
Raw Normal View History

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
*/
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.
*/
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-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-10-09 03:31:08 +00:00
this.gravity = new Phaser.Point;
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-10-09 03:31:08 +00:00
/**
2013-10-25 15:54:40 +00:00
* @property {number} maxObjects - Used by the QuadTree to set the maximum number of objects per quad.
2013-10-09 03:31:08 +00:00
*/
this.maxObjects = 10;
2013-10-09 03:31:08 +00:00
/**
2013-10-25 15:54:40 +00:00
* @property {number} maxLevels - Used by the QuadTree to set the maximum number of iteration levels.
2013-10-09 03:31:08 +00:00
*/
this.maxLevels = 4;
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-10-25 15:54:40 +00:00
/**
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
*/
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
2013-10-25 15:54:40 +00:00
/**
* @property {number} quadTreeID - The QuadTree ID.
*/
2013-10-09 03:31:08 +00:00
this.quadTreeID = 0;
// 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-10-09 03:31:08 +00:00
this._bounds1 = new Phaser.Rectangle;
2013-10-25 15:54:40 +00:00
/**
* @property {Phaser.Rectangle} _bounds2 - Internal cache var.
* @private
*/
2013-10-09 03:31:08 +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
*/
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-10-25 15:54:40 +00:00
/**
* @property {number} _angle - Internal cache var.
* @private
*/
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;
};
Phaser.Physics.Arcade.prototype = {
2013-10-25 15:54:40 +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
2013-10-09 03:31:08 +00:00
// Rotation
this._velocityDelta = (this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) * this.game.time.physicsElapsed * 0.5 * 60;
body.angularVelocity += this._velocityDelta;
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
body.angularVelocity += this._velocityDelta;
2013-10-09 03:31:08 +00:00
// Horizontal
this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x) - body.velocity.x) * this.game.time.physicsElapsed * 0.5 * 60;
body.velocity.x += this._velocityDelta;
body.x += (body.velocity.x * this.game.time.physicsElapsed);
body.velocity.x += this._velocityDelta;
2013-10-09 03:31:08 +00:00
// Vertical
this._velocityDelta = (this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y) - body.velocity.y) * this.game.time.physicsElapsed * 0.5 * 60;
body.velocity.y += this._velocityDelta;
body.y += (body.velocity.y * this.game.time.physicsElapsed);
body.velocity.y += this._velocityDelta;
},
2013-10-09 03:31:08 +00:00
/**
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
*
2013-10-25 15:54:40 +00:00
* @method Phaser.Physics.Arcade#computeVelocity
* @param {number} axis - 1 for horizontal, 2 for vertical.
* @param {Phaser.Physics.Arcade.Body} body - The Body object to be updated.
* @param {number} velocity - Any component of velocity (e.g. 20).
* @param {number} acceleration - Rate at which the velocity is changing.
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
* @param {number} mMax - An absolute value cap for the velocity.
* @return {number} The altered Velocity value.
*/
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
2013-10-09 03:31:08 +00:00
max = max || 10000;
2013-10-09 03:31:08 +00:00
if (axis == 1 && body.allowGravity)
{
2013-10-09 03:31:08 +00:00
velocity += this.gravity.x + body.gravity.x;
}
2013-10-09 03:31:08 +00:00
else if (axis == 2 && body.allowGravity)
{
2013-10-09 03:31:08 +00:00
velocity += this.gravity.y + body.gravity.y;
}
if (acceleration !== 0)
{
velocity += acceleration * this.game.time.physicsElapsed;
}
else if (drag !== 0)
{
this._drag = drag * this.game.time.physicsElapsed;
if (velocity - this._drag > 0)
{
velocity -= this._drag;
}
else if (velocity + this._drag < 0)
{
velocity += this._drag;
}
else
{
velocity = 0;
}
}
if (velocity > max)
{
velocity = max;
}
else if (velocity < -max)
{
velocity = -max;
}
return velocity;
},
2013-10-25 15:54:40 +00:00
/**
* Called automatically by the core game loop.
*
* @method Phaser.Physics.Arcade#preUpdate
* @protected
*/
preUpdate: function () {
// Clear the tree
this.quadTree.clear();
2013-10-09 03:31:08 +00:00
// Create our tree which all of the Physics bodies will add themselves to
this.quadTreeID = 0;
2013-10-09 03:31:08 +00:00
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
},
2013-10-25 15:54:40 +00:00
/**
* Called automatically by the core game loop.
*
* @method Phaser.Physics.Arcade#postUpdate
* @protected
*/
postUpdate: function () {
2013-10-09 03:31:08 +00:00
// Clear the tree ready for the next update
this.quadTree.clear();
},
/**
2013-10-25 15:54:40 +00:00
* Checks if two Sprite objects overlap.
*
2013-10-25 15:54:40 +00:00
* @method Phaser.Physics.Arcade#overlap
* @param {Phaser.Sprite} object1 - The first object to check. Can be an instance of Phaser.Sprite or anything that extends it.
* @param {Phaser.Sprite} object2 - The second object to check. Can be an instance of Phaser.Sprite or anything that extends it.
* @returns {boolean} true if the two objects overlap.
2013-10-25 15:54:40 +00:00
*/
overlap: function (object1, object2) {
// Only test valid objects
if (object1 && object2 && object1.exists && object2.exists)
{
return (Phaser.Rectangle.intersects(object1.body, object2.body));
}
return false;
},
2013-09-13 01:07:58 +00:00
/**
* Checks for collision between two game objects. The objects can be Sprites, Groups, Emitters or Tilemap Layers.
* You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
2013-10-25 15:54:40 +00:00
* The objects are also automatically separated.
2013-09-13 01:07:58 +00:00
*
2013-10-25 15:54:40 +00:00
* @method Phaser.Physics.Arcade#collide
* @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
* @param {function} [collideCallback=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 passed them to Collision.overlap.
* @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 collideCallback will only be called if processCallback returns true.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @returns {number} The number of collisions that were processed.
*/
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;
// Only collide valid objects
if (object1 && object2 && object1.exists && object2.exists)
{
// Can expand to support Buttons, Text, etc at a later date. For now these are the essentials.
// SPRITES
if (object1.type == Phaser.SPRITE)
{
if (object2.type == Phaser.SPRITE)
{
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.TILEMAPLAYER)
{
this.collideSpriteVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
}
2013-09-13 01:07:58 +00:00
}
// GROUPS
else if (object1.type == Phaser.GROUP)
2013-09-13 01:07:58 +00:00
{
if (object2.type == Phaser.SPRITE)
{
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.TILEMAPLAYER)
{
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
}
2013-09-13 01:07:58 +00:00
}
// TILEMAP LAYERS
else if (object1.type == Phaser.TILEMAPLAYER)
2013-09-13 01:07:58 +00:00
{
if (object2.type == Phaser.SPRITE)
{
this.collideSpriteVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.collideGroupVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext);
}
2013-09-13 01:07:58 +00:00
}
// EMITTER
else if (object1.type == Phaser.EMITTER)
2013-09-13 01:07:58 +00:00
{
if (object2.type == Phaser.SPRITE)
{
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.TILEMAPLAYER)
{
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
}
}
}
2013-09-13 01:07:58 +00:00
return (this._total > 0);
},
2013-10-25 15:54:40 +00:00
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideSpriteVsTilemapLayer
* @private
*/
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) {
this._mapData = tilemapLayer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
2013-09-13 01:07:58 +00:00
if (this._mapData.length === 0)
{
return;
}
for (var i = 0; i < this._mapData.length; i++)
{
if (this.separateTile(sprite.body, this._mapData[i]))
{
// They collided, is there a custom process callback?
if (processCallback)
{
if (processCallback.call(callbackContext, sprite, this._mapData[i]))
{
this._total++;
2013-09-13 01:07:58 +00:00
if (collideCallback)
{
collideCallback.call(callbackContext, sprite, this._mapData[i]);
}
}
}
else
{
this._total++;
if (collideCallback)
{
collideCallback.call(callbackContext, sprite, this._mapData[i]);
}
}
}
}
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.
*
* @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer
* @private
*/
collideGroupVsTilemapLayer: function (group, tilemapLayer, collideCallback, processCallback, callbackContext) {
if (group.length === 0)
{
return;
}
if (group.length === 0)
2013-09-24 14:49:21 +00:00
{
return;
}
if (group._container.first._iNext)
{
var currentNode = group._container.first._iNext;
do
{
2013-09-13 01:07:58 +00:00
if (currentNode.exists)
{
this.collideSpriteVsTilemapLayer(currentNode, tilemapLayer, collideCallback, processCallback, callbackContext);
}
currentNode = currentNode._iNext;
}
while (currentNode != group._container.last._iNext);
}
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.
*
* @method Phaser.Physics.Arcade#collideSpriteVsSprite
* @private
*/
collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext) {
2013-09-13 01:07:58 +00:00
this.separate(sprite1.body, sprite2.body);
2013-09-13 01:07:58 +00:00
if (this._result)
2013-09-13 01:07:58 +00:00
{
// They collided, is there a custom process callback?
2013-09-13 01:07:58 +00:00
if (processCallback)
{
if (processCallback.call(callbackContext, sprite1, sprite2))
2013-09-13 01:07:58 +00:00
{
this._total++;
if (collideCallback)
{
collideCallback.call(callbackContext, sprite1, sprite2);
2013-09-13 01:07:58 +00:00
}
}
}
else
{
this._total++;
if (collideCallback)
{
collideCallback.call(callbackContext, sprite1, sprite2);
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.
*
* @method Phaser.Physics.Arcade#collideSpriteVsGroup
* @private
*/
2013-09-13 01:07:58 +00:00
collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext) {
if (group.length === 0)
2013-09-24 14:49:21 +00:00
{
return;
}
2013-09-13 01:07:58 +00:00
// What is the sprite colliding with in the quadtree?
this._potentials = this.quadTree.retrieve(sprite);
2013-09-13 01:07:58 +00:00
for (var i = 0, len = this._potentials.length; i < len; i++)
{
2013-09-13 01:07:58 +00:00
// We have our potential suspects, are they in this group?
if (this._potentials[i].sprite.group == group)
{
2013-09-13 01:07:58 +00:00
this.separate(sprite.body, this._potentials[i]);
if (this._result && processCallback)
{
2013-09-13 01:07:58 +00:00
this._result = processCallback.call(callbackContext, sprite, this._potentials[i].sprite);
}
if (this._result)
{
this._total++;
if (collideCallback)
{
2013-09-13 01:07:58 +00:00
collideCallback.call(callbackContext, sprite, this._potentials[i].sprite);
}
}
}
}
},
2013-10-25 15:54:40 +00:00
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideGroupVsGroup
* @private
*/
2013-09-13 01:07:58 +00:00
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext) {
if (group1.length === 0 || group2.length === 0)
2013-09-24 14:49:21 +00:00
{
return;
}
2013-09-13 01:07:58 +00:00
if (group1._container.first._iNext)
{
var currentNode = group1._container.first._iNext;
do
{
if (currentNode.exists)
{
this.collideSpriteVsGroup(currentNode, group2, collideCallback, processCallback, callbackContext);
}
currentNode = currentNode._iNext;
}
while (currentNode != group1._container.last._iNext);
}
},
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.
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
*/
separate: function (body1, body2) {
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
},
/**
2013-10-25 15:54:40 +00:00
* The core separation function to separate two physics bodies on the x axis.
* @method Phaser.Physics.Arcade#separateX
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
* @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate.
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
*/
separateX: function (body1, body2) {
// Can't separate two immovable bodies
if (body1.immovable && body2.immovable)
{
return false;
}
this._overlap = 0;
// Check if the hulls actually overlap
if (Phaser.Rectangle.intersects(body1, body2))
{
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;
if (body1.customSeparateX || body2.customSeparateX)
{
return true;
}
this._velocity1 = body1.velocity.x;
this._velocity2 = body2.velocity.x;
if (!body1.immovable && !body2.immovable)
{
this._overlap *= 0.5;
body1.x = body1.x - this._overlap;
body2.x += 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;
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
}
else if (!body1.immovable)
{
body1.x = body1.x - this._overlap;
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
}
else if (!body2.immovable)
{
body2.x += this._overlap;
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
}
return true;
}
}
return false;
},
/**
2013-10-25 15:54:40 +00:00
* The core separation function to separate two physics bodies on the y axis.
* @method Phaser.Physics.Arcade#separateY
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
* @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate.
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
*/
separateY: function (body1, body2) {
// Can't separate two immovable or non-existing bodys
if (body1.immovable && body2.immovable)
{
return false;
}
this._overlap = 0;
// Check if the hulls actually overlap
if (Phaser.Rectangle.intersects(body1, body2))
{
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())
{
// 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;
if (body1.customSeparateY || body2.customSeparateY)
{
return true;
}
this._velocity1 = body1.velocity.y;
this._velocity2 = body2.velocity.y;
if (!body1.immovable && !body2.immovable)
{
this._overlap *= 0.5;
body1.y = body1.y - this._overlap;
body2.y += 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;
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
}
else if (!body1.immovable)
{
body1.y = body1.y - this._overlap;
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.active && body2.moves && (body1.deltaY() > body2.deltaY()))
{
body1.x += body2.x - body2.lastX;
}
}
else if (!body2.immovable)
{
body2.y += this._overlap;
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.sprite.active && body1.moves && (body1.deltaY() < body2.deltaY()))
{
body2.x += body1.x - body1.lastX;
}
}
return true;
}
}
return false;
},
2013-10-25 15:54:40 +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.
*/
separateTile: function (body, tile) {
this._result = (this.separateTileX(body, tile, true) || this.separateTileY(body, tile, true));
return this._result;
},
/**
2013-10-25 15:54:40 +00:00
* The core separation function to separate a physics body and a tile on the x axis.
* @method Phaser.Physics.Arcade#separateTileX
* @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.
*/
separateTileX: function (body, tile, separate) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable || body.deltaX() === 0 || Phaser.Rectangle.intersects(body.hullX, tile) === false)
{
return false;
}
this._overlap = 0;
// The hulls overlap, let's process it
// this._maxOverlap = body.deltaAbsX() + this.OVERLAP_BIAS;
if (body.deltaX() < 0)
{
// Moving left
this._overlap = tile.right - body.hullX.x;
// if ((this._overlap > this._maxOverlap) || body.allowCollision.left === false || tile.tile.collideRight === false)
if (body.allowCollision.left === false || tile.tile.collideRight === false)
{
this._overlap = 0;
}
else
{
body.touching.left = true;
}
}
else
{
// Moving right
this._overlap = body.hullX.right - tile.x;
// if ((this._overlap > this._maxOverlap) || body.allowCollision.right === false || tile.tile.collideLeft === false)
if (body.allowCollision.right === false || tile.tile.collideLeft === false)
{
this._overlap = 0;
}
else
{
body.touching.right = true;
}
}
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap != 0)
{
if (separate)
{
if (body.deltaX() < 0)
{
body.x = body.x + this._overlap;
}
else
{
body.x = body.x - this._overlap;
}
if (body.bounce.x === 0)
{
body.velocity.x = 0;
}
else
{
body.velocity.x = -body.velocity.x * body.bounce.x;
}
body.updateHulls();
}
return true;
}
else
{
return false;
}
},
/**
2013-10-25 15:54:40 +00:00
* The core separation function to separate a physics body and a tile on the x axis.
* @method Phaser.Physics.Arcade#separateTileY
* @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.
*/
separateTileY: function (body, tile, separate) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable || body.deltaY() === 0 || Phaser.Rectangle.intersects(body.hullY, tile) === false)
{
return false;
}
this._overlap = 0;
// The hulls overlap, let's process it
// this._maxOverlap = body.deltaAbsY() + this.OVERLAP_BIAS;
if (body.deltaY() < 0)
{
// Moving up
this._overlap = tile.bottom - body.hullY.y;
// if ((this._overlap > this._maxOverlap) || body.allowCollision.up === false || tile.tile.collideDown === false)
if (body.allowCollision.up === false || tile.tile.collideDown === false)
{
this._overlap = 0;
}
else
{
body.touching.up = true;
}
}
else
{
// Moving down
this._overlap = body.hullY.bottom - tile.y;
// if ((this._overlap > this._maxOverlap) || body.allowCollision.down === false || tile.tile.collideUp === false)
if (body.allowCollision.down === false || tile.tile.collideUp === false)
{
this._overlap = 0;
}
else
{
body.touching.down = true;
}
}
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap != 0)
{
if (separate)
{
if (body.deltaY() < 0)
{
body.y = body.y + this._overlap;
}
else
{
body.y = body.y - this._overlap;
}
if (body.bounce.y === 0)
{
body.velocity.y = 0;
}
else
{
body.velocity.y = -body.velocity.y * body.bounce.y;
}
body.updateHulls();
}
return true;
}
else
{
return false;
}
},
/**
2013-10-09 06:11:36 +00:00
* Move the given display object towards the destination object at a steady velocity.
* 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.
* 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#moveToObject
* @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-10-09 03:31:08 +00:00
moveToObject: function (displayObject, destination, speed, maxTime) {
if (typeof speed === 'undefined') { speed = 60; }
if (typeof maxTime === 'undefined') { maxTime = 0; }
2013-10-09 06:11:36 +00:00
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displayObject.x);
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);
}
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
return this._angle;
},
/**
* 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.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
*
2013-10-09 03:31:08 +00:00
* @method Phaser.Physics.Arcade#moveToPointer
* @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-10-09 03:31:08 +00:00
moveToPointer: function (displayObject, speed, pointer, maxTime) {
if (typeof speed === 'undefined') { speed = 60; }
pointer = pointer || this.game.input.activePointer;
if (typeof maxTime === 'undefined') { maxTime = 0; }
2013-10-09 03:31:08 +00:00
this._angle = this.angleToPointer(displayObject, pointer);
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);
}
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
return this._angle;
},
/**
2013-10-09 03:31:08 +00:00
* Move the given display object towards the x/y coordinates at a steady velocity.
* 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.
* 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
* @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.
* @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) {
if (typeof speed === 'undefined') { speed = 60; }
if (typeof maxTime === 'undefined') { maxTime = 0; }
2013-10-09 03:31:08 +00:00
this._angle = Math.atan2(y - displayObject.y, x - displayObject.x);
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);
}
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
return this._angle;
},
/**
* 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) {
if (typeof speed === 'undefined') { speed = 60; }
point = point || new Phaser.Point;
return point.setTo((Math.cos(this.game.math.degToRad(angle)) * speed), (Math.sin(this.game.math.degToRad(angle)) * speed));
},
/**
* 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) {
if (typeof speed === 'undefined') { speed = 60; }
point = point || new Phaser.Point;
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
},
/**
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.
* 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#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) {
if (typeof speed === 'undefined') { speed = 60; }
2013-10-09 03:31:08 +00:00
point = point || new Phaser.Point;
2013-10-09 03:31:08 +00:00
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
},
/**
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-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-10-09 03:31:08 +00:00
this._angle = this.angleBetween(displayObject, destination);
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-10-09 03:31:08 +00:00
return this._angle;
},
/**
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-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-10-09 03:31:08 +00:00
this._angle = this.angleToPointer(displayObject, pointer);
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-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-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-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-10-09 03:31:08 +00:00
this._dx = source.x - target.x;
this._dy = source.y - target.y;
2013-10-09 03:31:08 +00:00
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
},
/**
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-10-09 03:31:08 +00:00
this._dx = displayObject.x - x;
this._dy = displayObject.y - y;
2013-10-09 03:31:08 +00:00
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
},
/**
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-10-09 03:31:08 +00:00
pointer = pointer || this.game.input.activePointer;
this._dx = displayObject.x - pointer.x;
this._dy = displayObject.y - pointer.y;
2013-10-09 03:31:08 +00:00
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
},
/**
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-10-09 03:31:08 +00:00
this._dx = target.x - source.x;
this._dy = target.y - source.y;
2013-10-09 03:31:08 +00:00
return Math.atan2(this._dy, this._dx);
},
/**
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-10-09 03:31:08 +00:00
this._dx = x - displayObject.x;
this._dy = y - displayObject.y;
2013-10-09 03:31:08 +00:00
return Math.atan2(this._dy, this._dx);
},
/**
* 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
* @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-09 03:31:08 +00:00
angleToPointer: function (displayObject, pointer) {
pointer = pointer || this.game.input.activePointer;
this._dx = pointer.worldX - displayObject.x;
this._dy = pointer.worldY - displayObject.y;
2013-10-09 03:31:08 +00:00
return Math.atan2(this._dy, this._dx);
}
};