phaser/src/physics/arcade/ArcadePhysics.js

1602 lines
61 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.
*/
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
*/
this._bounds1 = new Phaser.Rectangle();
2013-10-25 15:54:40 +00:00
/**
* @property {Phaser.Rectangle} _bounds2 - Internal cache var.
* @private
*/
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 = {
/**
* 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
if (body.allowGravity)
{
this._gravityX = (this.gravity.x + body.gravity.x);
this._gravityY = (this.gravity.y + body.gravity.y);
}
else
{
this._gravityX = 0;
this._gravityY = 0;
}
// Rotation
if (body.allowRotation)
{
this._velocityDelta = body.angularAcceleration * this.game.time.physicsElapsed;
if (body.angularDrag !== 0 && body.angularAcceleration === 0)
{
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;
}
}
body.rotation += this.game.time.physicsElapsed * (body.angularVelocity + this._velocityDelta / 2);
body.angularVelocity += this._velocityDelta;
if (body.angularVelocity > body.maxAngular)
{
body.angularVelocity = body.maxAngular;
}
else if (body.angularVelocity < -body.maxAngular)
{
body.angularVelocity = -body.maxAngular;
}
}
// 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-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();
},
/**
* 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-25 15:54:40 +00:00
* @method Phaser.Physics.Arcade#overlap
* @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
*/
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
overlapCallback = overlapCallback || null;
processCallback = processCallback || null;
callbackContext = callbackContext || overlapCallback;
this._result = false;
this._total = 0;
// Only test valid objects
if (object1 && object2 && object1.exists && object2.exists)
{
// SPRITES
if (object1.type == Phaser.SPRITE || object1.type == Phaser.TILESPRITE)
{
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
{
this.overlapSpriteVsSprite(object1, object2, overlapCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.overlapSpriteVsGroup(object1, object2, overlapCallback, processCallback, callbackContext);
}
}
// GROUPS
else if (object1.type == Phaser.GROUP)
{
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
{
this.overlapSpriteVsGroup(object2, object1, overlapCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.overlapGroupVsGroup(object1, object2, overlapCallback, processCallback, callbackContext);
}
}
// EMITTER
else if (object1.type == Phaser.EMITTER)
{
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
{
this.overlapSpriteVsGroup(object2, object1, overlapCallback, processCallback, callbackContext);
}
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
{
this.overlapGroupVsGroup(object1, object2, overlapCallback, processCallback, callbackContext);
}
}
}
return (this._total > 0);
},
/**
* An internal function. Use Phaser.Physics.Arcade.overlap instead.
*
* @method Phaser.Physics.Arcade#overlapSpriteVsSprite
* @private
*/
overlapSpriteVsSprite: function (sprite1, sprite2, overlapCallback, processCallback, callbackContext) {
this._result = Phaser.Rectangle.intersects(sprite1.body, sprite2.body);
if (this._result)
{
// They collided, is there a custom process callback?
if (processCallback)
{
if (processCallback.call(callbackContext, sprite1, sprite2))
{
this._total++;
if (overlapCallback)
{
overlapCallback.call(callbackContext, sprite1, sprite2);
}
}
}
else
{
this._total++;
if (overlapCallback)
{
overlapCallback.call(callbackContext, sprite1, sprite2);
}
}
}
},
/**
* An internal function. Use Phaser.Physics.Arcade.overlap instead.
*
* @method Phaser.Physics.Arcade#overlapSpriteVsGroup
* @private
*/
overlapSpriteVsGroup: function (sprite, group, overlapCallback, processCallback, callbackContext) {
if (group.length === 0)
{
return;
}
// What is the sprite colliding with in the quadtree?
this._potentials = this.quadTree.retrieve(sprite);
for (var i = 0, len = this._potentials.length; i < len; i++)
{
// We have our potential suspects, are they in this group?
if (this._potentials[i].sprite.group == group)
{
this._result = Phaser.Rectangle.intersects(sprite.body, this._potentials[i]);
if (this._result && processCallback)
{
this._result = processCallback.call(callbackContext, sprite, this._potentials[i].sprite);
}
if (this._result)
{
this._total++;
if (overlapCallback)
{
overlapCallback.call(callbackContext, sprite, this._potentials[i].sprite);
}
}
}
}
},
/**
* An internal function. Use Phaser.Physics.Arcade.overlap instead.
*
* @method Phaser.Physics.Arcade#overlapGroupVsGroup
* @private
*/
overlapGroupVsGroup: function (group1, group2, overlapCallback, processCallback, callbackContext) {
if (group1.length === 0 || group2.length === 0)
{
return;
}
if (group1._container.first._iNext)
{
var currentNode = group1._container.first._iNext;
do
{
if (currentNode.exists)
{
this.overlapSpriteVsGroup(currentNode, group2, overlapCallback, processCallback, callbackContext);
}
currentNode = currentNode._iNext;
}
while (currentNode != group1._container.last._iNext);
}
},
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 specified them.
2013-10-25 15:54:40 +00:00
* @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 {boolean} True if a collision occured otherwise false.
2013-10-25 15:54:40 +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;
// Only collide valid objects
if (object1 && object2 && object1.exists && object2.exists)
{
// SPRITES
if (object1.type == Phaser.SPRITE || object1.type == Phaser.TILESPRITE)
{
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
{
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 || object2.type == Phaser.TILESPRITE)
{
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 || object2.type == Phaser.TILESPRITE)
{
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 || object2.type == Phaser.TILESPRITE)
{
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;
}
if (this._mapData.length > 1)
{
// console.log(' multi sep ---------------------------------------------------------------------------------------------');
this.separateTiles(sprite.body, this._mapData);
}
else
{
var i = 0;
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._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
2013-09-13 01:07:58 +00:00
{
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) {
2014-01-08 01:53:28 +00:00
if (body1 !== body2)
2013-12-05 05:26:03 +00:00
{
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
2014-01-08 01:53:28 +00:00
}
else
{
2013-12-05 05:26:03 +00:00
this._result = false;
}
2014-01-08 01:53:28 +00:00
},
/**
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;
}
// body1.updateHulls();
// body2.updateHulls();
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)
{
2014-01-08 01:53:28 +00:00
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
2013-12-10 23:25:57 +00:00
if (body2.moves)
{
2013-12-10 23:25:57 +00:00
body1.x += body2.x - body2.preX;
}
}
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
2013-12-10 23:25:57 +00:00
if (body1.moves)
{
2013-12-10 23:25:57 +00:00
body2.x += body1.x - body1.preX;
}
}
// body1.updateHulls();
// body2.updateHulls();
return true;
}
}
return false;
},
2013-10-25 15:54:40 +00:00
/**
* The core separation function to separate a physics body and an array of tiles.
* @method Phaser.Physics.Arcade#separateTiles
* @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.
*/
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))
{
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
{
// LEFT
localOverlapX = body.x - tile.right;
if (localOverlapX >= body.deltaX())
{
// console.log('m left overlapX', localOverlapX, body.deltaX());
// 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())
{
// console.log('m right overlapX', localOverlapX, body.deltaX());
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())
{
// console.log('m up overlapY', localOverlapY, body.deltaY());
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())
{
// console.log('m down overlapY', localOverlapY, body.deltaY());
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;
}
if (body.touching.left || body.touching.right)
{
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;
}
}
if (body.touching.up || body.touching.down)
{
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;
},
/**
* 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) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable || Phaser.Rectangle.intersects(body, tile) === false)
{
// 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);
return false;
}
// use body var instead
body.overlapX = 0;
body.overlapY = 0;
// Remember - this happens AFTER the body has been moved by the motion update, so it needs moving back again
// console.log('---------------------------------------------------------------------------------------------');
// 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()));
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
{
// LEFT
body.overlapX = body.x - tile.right;
if (body.overlapX >= body.deltaX())
{
// console.log('left overlapX', body.overlapX, body.deltaX());
// 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
body.overlapX = body.right - tile.x;
// Distance check
if (body.overlapX <= body.deltaX())
{
// console.log('right overlapX', body.overlapX, body.deltaX());
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
}
}
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
{
// UP
body.overlapY = body.y - tile.bottom;
// Distance check
if (body.overlapY >= body.deltaY())
{
// console.log('up overlapY', body.overlapY, body.deltaY());
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
}
}
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
{
// DOWN
body.overlapY = body.bottom - tile.y;
if (body.overlapY <= body.deltaY())
{
// console.log('down overlapY', body.overlapY, body.deltaY());
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
}
}
// Separate in a single sweep
if (body.touching.none)
{
return false;
}
// if (body.overlapX !== 0)
if (body.touching.left || body.touching.right)
{
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;
}
}
// if (body.overlapY !== 0)
if (body.touching.up || body.touching.down)
{
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-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: 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) {
if (typeof speed === 'undefined') { speed = 60; }
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);
}
};
Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade;