2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-17 03:41:58 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2018-03-28 13:15:32 +00:00
|
|
|
var DegToRad = require('../../math/DegToRad');
|
|
|
|
var DistanceBetween = require('../../math/distance/DistanceBetween');
|
2018-08-13 19:59:58 +00:00
|
|
|
var DistanceSquared = require('../../math/distance/DistanceSquared');
|
2018-01-17 03:41:58 +00:00
|
|
|
var Factory = require('./Factory');
|
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
|
|
|
var Merge = require('../../utils/object/Merge');
|
2018-05-15 11:51:50 +00:00
|
|
|
var PluginCache = require('../../plugins/PluginCache');
|
2019-01-18 13:41:43 +00:00
|
|
|
var SceneEvents = require('../../scene/events');
|
2018-03-28 13:15:32 +00:00
|
|
|
var Vector2 = require('../../math/Vector2');
|
2018-01-17 03:41:58 +00:00
|
|
|
var World = require('./World');
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-13 16:41:50 +00:00
|
|
|
* The Arcade Physics Plugin belongs to a Scene and sets up and manages the Scene's physics simulation.
|
|
|
|
* It also holds some useful methods for moving and rotating Arcade Physics Bodies.
|
|
|
|
*
|
|
|
|
* You can access it from within a Scene using `this.physics`.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @class ArcadePhysics
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Physics.Arcade
|
2018-02-09 03:44:23 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-13 16:41:50 +00:00
|
|
|
* @param {Phaser.Scene} scene - The Scene that this Plugin belongs to.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
var ArcadePhysics = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function ArcadePhysics (scene)
|
|
|
|
{
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* The Scene that this Plugin belongs to.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.ArcadePhysics#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
this.scene = scene;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* The Scene's Systems.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.ArcadePhysics#systems
|
|
|
|
* @type {Phaser.Scenes.Systems}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
this.systems = scene.sys;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* A configuration object. Union of the `physics.arcade.*` properties of the GameConfig and SceneConfig objects.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.ArcadePhysics#config
|
|
|
|
* @type {object}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
this.config = this.getConfig();
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* The physics simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.ArcadePhysics#world
|
|
|
|
* @type {Phaser.Physics.Arcade.World}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
this.world;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* An object holding the Arcade Physics factory methods.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.ArcadePhysics#add
|
|
|
|
* @type {Phaser.Physics.Arcade.Factory}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
this.add;
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
|
|
|
|
scene.sys.events.on(SceneEvents.START, this.start, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
},
|
|
|
|
|
2018-04-17 01:34:07 +00:00
|
|
|
/**
|
|
|
|
* This method is called automatically, only once, when the Scene is first created.
|
|
|
|
* Do not invoke it directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#boot
|
|
|
|
* @private
|
|
|
|
* @since 3.5.1
|
|
|
|
*/
|
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
this.world = new World(this.scene, this.config);
|
|
|
|
this.add = new Factory(this.world);
|
2018-04-17 11:25:45 +00:00
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
|
2018-04-17 01:34:07 +00:00
|
|
|
},
|
|
|
|
|
2018-04-13 16:12:17 +00:00
|
|
|
/**
|
|
|
|
* This method is called automatically by the Scene when it is starting up.
|
|
|
|
* It is responsible for creating local systems, properties and listening for Scene events.
|
|
|
|
* Do not invoke it directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#start
|
|
|
|
* @private
|
2018-04-15 11:44:47 +00:00
|
|
|
* @since 3.5.0
|
2018-04-13 16:12:17 +00:00
|
|
|
*/
|
|
|
|
start: function ()
|
|
|
|
{
|
2018-04-17 01:34:07 +00:00
|
|
|
if (!this.world)
|
|
|
|
{
|
|
|
|
this.world = new World(this.scene, this.config);
|
|
|
|
this.add = new Factory(this.world);
|
|
|
|
}
|
2018-04-13 16:12:17 +00:00
|
|
|
|
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
eventEmitter.on(SceneEvents.UPDATE, this.world.update, this.world);
|
|
|
|
eventEmitter.on(SceneEvents.POST_UPDATE, this.world.postUpdate, this.world);
|
|
|
|
eventEmitter.once(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2018-01-17 03:41:58 +00:00
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Creates the physics configuration for the current Scene.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#getConfig
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-13 16:41:50 +00:00
|
|
|
* @return {object} The physics configuration.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
getConfig: function ()
|
|
|
|
{
|
|
|
|
var gameConfig = this.systems.game.config.physics;
|
|
|
|
var sceneConfig = this.systems.settings.physics;
|
|
|
|
|
|
|
|
var config = Merge(
|
|
|
|
GetFastValue(sceneConfig, 'arcade', {}),
|
|
|
|
GetFastValue(gameConfig, 'arcade', {})
|
|
|
|
);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Tests if Game Objects overlap. See {@link Phaser.Physics.Arcade.World#overlap}
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#overlap
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-02-13 14:45:36 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Types.ArcadeColliderType} object1 - The first object or array of objects to check.
|
|
|
|
* @param {Phaser.Physics.Arcade.Types.ArcadeColliderType} [object2] - The second object or array of objects to check, or `undefined`.
|
2018-06-13 16:41:50 +00:00
|
|
|
* @param {ArcadePhysicsCallback} [collideCallback] - An optional callback function that is called if the objects collide.
|
|
|
|
* @param {ArcadePhysicsCallback} [processCallback] - An optional 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 this callback returns `true`.
|
2018-03-21 13:15:25 +00:00
|
|
|
* @param {*} [callbackContext] - The context in which to run the callbacks.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-06-13 16:41:50 +00:00
|
|
|
* @return {boolean} True if at least one Game Object overlaps another.
|
|
|
|
*
|
|
|
|
* @see Phaser.Physics.Arcade.World#overlap
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext)
|
|
|
|
{
|
|
|
|
if (overlapCallback === undefined) { overlapCallback = null; }
|
|
|
|
if (processCallback === undefined) { processCallback = null; }
|
|
|
|
if (callbackContext === undefined) { callbackContext = overlapCallback; }
|
|
|
|
|
|
|
|
return this.world.collideObjects(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Tests if Game Objects overlap and separates them (if possible). See {@link Phaser.Physics.Arcade.World#collide}.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#collide
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-02-13 14:45:36 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.Types.ArcadeColliderType} object1 - The first object or array of objects to check.
|
|
|
|
* @param {Phaser.Physics.Arcade.Types.ArcadeColliderType} [object2] - The second object or array of objects to check, or `undefined`.
|
2018-06-13 16:41:50 +00:00
|
|
|
* @param {ArcadePhysicsCallback} [collideCallback] - An optional callback function that is called if the objects collide.
|
|
|
|
* @param {ArcadePhysicsCallback} [processCallback] - An optional callback function that lets you perform additional checks against the two objects if they collide. If this is set then `collideCallback` will only be called if this callback returns `true`.
|
2018-03-21 13:15:25 +00:00
|
|
|
* @param {*} [callbackContext] - The context in which to run the callbacks.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-06-13 16:41:50 +00:00
|
|
|
* @return {boolean} True if any overlapping Game Objects were separated, otherwise false.
|
|
|
|
*
|
|
|
|
* @see Phaser.Physics.Arcade.World#collide
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
collide: function (object1, object2, collideCallback, processCallback, callbackContext)
|
|
|
|
{
|
|
|
|
if (collideCallback === undefined) { collideCallback = null; }
|
|
|
|
if (processCallback === undefined) { processCallback = null; }
|
|
|
|
if (callbackContext === undefined) { callbackContext = collideCallback; }
|
|
|
|
|
|
|
|
return this.world.collideObjects(object1, object2, collideCallback, processCallback, callbackContext, false);
|
|
|
|
},
|
|
|
|
|
2019-03-07 13:36:15 +00:00
|
|
|
/**
|
|
|
|
* This advanced method is specifically for testing for collision between a single Sprite and an array of Tile objects.
|
|
|
|
*
|
|
|
|
* You should generally use the `collide` method instead, with a Sprite vs. a Tilemap Layer, as that will perform
|
|
|
|
* tile filtering and culling for you, as well as handle the interesting face collision automatically.
|
|
|
|
*
|
|
|
|
* This method is offered for those who would like to check for collision with specific Tiles in a layer, without
|
|
|
|
* having to set any collision attributes on the tiles in question. This allows you to perform quick dynamic collisions
|
|
|
|
* on small sets of Tiles. As such, no culling or checks are made to the array of Tiles given to this method,
|
|
|
|
* you should filter them before passing them to this method.
|
|
|
|
*
|
|
|
|
* Important: Use of this method skips the `interesting faces` system that Tilemap Layers use. This means if you have
|
|
|
|
* say a row or column of tiles, and you jump into, or walk over them, it's possible to get stuck on the edges of the
|
|
|
|
* tiles as the interesting face calculations are skipped. However, for quick-fire small collision set tests on
|
|
|
|
* dynamic maps, this method can prove very useful.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#collideTiles
|
|
|
|
* @fires Phaser.Physics.Arcade.Events#TILE_COLLIDE
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} sprite - The first object to check for collision.
|
|
|
|
* @param {Phaser.Tilemaps.Tile[]} tiles - An array of Tiles to check for collision against.
|
|
|
|
* @param {ArcadePhysicsCallback} [collideCallback] - An optional callback function that is called if the objects collide.
|
|
|
|
* @param {ArcadePhysicsCallback} [processCallback] - An optional callback function that lets you perform additional checks against the two objects if they collide. If this is set then `collideCallback` will only be called if this callback returns `true`.
|
|
|
|
* @param {any} [callbackContext] - The context in which to run the callbacks.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if any objects overlap (with `overlapOnly`); or true if any overlapping objects were separated.
|
|
|
|
*/
|
|
|
|
collideTiles: function (sprite, tiles, collideCallback, processCallback, callbackContext)
|
|
|
|
{
|
|
|
|
return this.world.collideTiles(sprite, tiles, collideCallback, processCallback, callbackContext);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This advanced method is specifically for testing for overlaps between a single Sprite and an array of Tile objects.
|
|
|
|
*
|
|
|
|
* You should generally use the `overlap` method instead, with a Sprite vs. a Tilemap Layer, as that will perform
|
|
|
|
* tile filtering and culling for you, as well as handle the interesting face collision automatically.
|
|
|
|
*
|
|
|
|
* This method is offered for those who would like to check for overlaps with specific Tiles in a layer, without
|
|
|
|
* having to set any collision attributes on the tiles in question. This allows you to perform quick dynamic overlap
|
|
|
|
* tests on small sets of Tiles. As such, no culling or checks are made to the array of Tiles given to this method,
|
|
|
|
* you should filter them before passing them to this method.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#overlapTiles
|
|
|
|
* @fires Phaser.Physics.Arcade.Events#TILE_OVERLAP
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} sprite - The first object to check for collision.
|
|
|
|
* @param {Phaser.Tilemaps.Tile[]} tiles - An array of Tiles to check for collision against.
|
|
|
|
* @param {ArcadePhysicsCallback} [collideCallback] - An optional callback function that is called if the objects overlap.
|
|
|
|
* @param {ArcadePhysicsCallback} [processCallback] - An optional callback function that lets you perform additional checks against the two objects if they collide. If this is set then `collideCallback` will only be called if this callback returns `true`.
|
|
|
|
* @param {any} [callbackContext] - The context in which to run the callbacks.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if any objects overlap (with `overlapOnly`); or true if any overlapping objects were separated.
|
|
|
|
*/
|
|
|
|
overlapTiles: function (sprite, tiles, collideCallback, processCallback, callbackContext)
|
|
|
|
{
|
|
|
|
return this.world.overlapTiles(sprite, tiles, collideCallback, processCallback, callbackContext);
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Pauses the simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#pause
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-13 16:41:50 +00:00
|
|
|
* @return {Phaser.Physics.Arcade.World} The simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
|
|
|
pause: function ()
|
|
|
|
{
|
|
|
|
return this.world.pause();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Resumes the simulation (if paused).
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#resume
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-13 16:41:50 +00:00
|
|
|
* @return {Phaser.Physics.Arcade.World} The simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
|
|
|
resume: function ()
|
|
|
|
{
|
|
|
|
return this.world.resume();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-04-24 00:48:15 +00:00
|
|
|
* Sets the acceleration.x/y property on the game object so it will move towards the x/y coordinates at the given rate (in pixels per second squared)
|
2018-03-18 23:29:46 +00:00
|
|
|
*
|
2018-02-09 03:44:23 +00:00
|
|
|
* You must give a maximum speed value, beyond which the game object won't go any faster.
|
2018-03-18 23:29:46 +00:00
|
|
|
*
|
2018-02-09 03:44:23 +00:00
|
|
|
* Note: The game object does not continuously track the target. If the target changes location during transit the game object will not modify its course.
|
|
|
|
* Note: The game object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#accelerateTo
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - Any Game Object with an Arcade Physics body.
|
|
|
|
* @param {number} x - The x coordinate to accelerate towards.
|
|
|
|
* @param {number} y - The y coordinate to accelerate towards.
|
2018-04-24 00:48:15 +00:00
|
|
|
* @param {number} [speed=60] - The acceleration (change in speed) in pixels per second squared.
|
2018-02-09 03:44:23 +00:00
|
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the game object can reach.
|
|
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the game object can reach.
|
|
|
|
*
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
|
|
|
*/
|
|
|
|
accelerateTo: function (gameObject, x, y, speed, xSpeedMax, ySpeedMax)
|
|
|
|
{
|
|
|
|
if (speed === undefined) { speed = 60; }
|
|
|
|
|
|
|
|
var angle = Math.atan2(y - gameObject.y, x - gameObject.x);
|
|
|
|
|
|
|
|
gameObject.body.acceleration.setToPolar(angle, speed);
|
|
|
|
|
|
|
|
if (xSpeedMax !== undefined && ySpeedMax !== undefined)
|
|
|
|
{
|
|
|
|
gameObject.body.maxVelocity.set(xSpeedMax, ySpeedMax);
|
|
|
|
}
|
|
|
|
|
|
|
|
return angle;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-04-24 00:48:15 +00:00
|
|
|
* Sets the acceleration.x/y property on the game object so it will move towards the x/y coordinates at the given rate (in pixels per second squared)
|
2018-03-18 23:29:46 +00:00
|
|
|
*
|
2018-02-09 03:44:23 +00:00
|
|
|
* You must give a maximum speed value, beyond which the game object won't go any faster.
|
2018-03-18 23:29:46 +00:00
|
|
|
*
|
2018-02-09 03:44:23 +00:00
|
|
|
* Note: The game object does not continuously track the target. If the target changes location during transit the game object will not modify its course.
|
|
|
|
* Note: The game object doesn't stop moving once it reaches the destination coordinates.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#accelerateToObject
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - Any Game Object with an Arcade Physics body.
|
|
|
|
* @param {Phaser.GameObjects.GameObject} destination - The Game Object to move towards. Can be any object but must have visible x/y properties.
|
2018-04-24 00:48:15 +00:00
|
|
|
* @param {number} [speed=60] - The acceleration (change in speed) in pixels per second squared.
|
2018-02-09 03:44:23 +00:00
|
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the game object can reach.
|
|
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the game object can reach.
|
|
|
|
*
|
|
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
|
|
|
*/
|
|
|
|
accelerateToObject: function (gameObject, destination, speed, xSpeedMax, ySpeedMax)
|
|
|
|
{
|
|
|
|
return this.accelerateTo(gameObject, destination.x, destination.y, speed, xSpeedMax, ySpeedMax);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-03-07 14:13:45 +00:00
|
|
|
* Finds the Dynamic Body closest to a source point or object.
|
|
|
|
*
|
|
|
|
* If two or more bodies are the exact same distance from the source point, only the first body
|
|
|
|
* is returned.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#closest
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-03-07 14:13:45 +00:00
|
|
|
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2019-03-07 14:13:45 +00:00
|
|
|
* @return {Phaser.Physics.Arcade.Body} The closest Dynamic Body to the given source point.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
|
|
|
closest: function (source)
|
|
|
|
{
|
2019-03-07 14:13:45 +00:00
|
|
|
var bodies = this.world.bodies;
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
var min = Number.MAX_VALUE;
|
|
|
|
var closest = null;
|
|
|
|
var x = source.x;
|
|
|
|
var y = source.y;
|
|
|
|
|
2019-03-07 14:13:45 +00:00
|
|
|
bodies.iterate(function (target)
|
2018-02-09 03:44:23 +00:00
|
|
|
{
|
2018-08-13 19:59:58 +00:00
|
|
|
var distance = DistanceSquared(x, y, target.x, target.y);
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
if (distance < min)
|
|
|
|
{
|
|
|
|
closest = target;
|
|
|
|
min = distance;
|
|
|
|
}
|
2019-03-07 14:13:45 +00:00
|
|
|
|
|
|
|
});
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
return closest;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-03-07 14:13:45 +00:00
|
|
|
* Finds the Dynamic Body farthest from a source point or object.
|
|
|
|
*
|
|
|
|
* If two or more bodies are the exact same distance from the source point, only the first body
|
|
|
|
* is returned.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#furthest
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-03-07 14:13:45 +00:00
|
|
|
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2019-03-07 14:13:45 +00:00
|
|
|
* @return {Phaser.Physics.Arcade.Body} The Dynamic Body furthest away from the given source point.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
|
|
|
furthest: function (source)
|
|
|
|
{
|
2019-03-07 14:13:45 +00:00
|
|
|
var bodies = this.world.bodies;
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
var max = -1;
|
|
|
|
var farthest = null;
|
|
|
|
var x = source.x;
|
|
|
|
var y = source.y;
|
|
|
|
|
2019-03-07 14:13:45 +00:00
|
|
|
bodies.iterate(function (target)
|
2018-02-09 03:44:23 +00:00
|
|
|
{
|
2018-08-13 19:59:58 +00:00
|
|
|
var distance = DistanceSquared(x, y, target.x, target.y);
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
if (distance > max)
|
|
|
|
{
|
|
|
|
farthest = target;
|
|
|
|
max = distance;
|
|
|
|
}
|
2019-03-07 14:13:45 +00:00
|
|
|
|
|
|
|
});
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
return farthest;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#moveTo
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - Any Game Object with an Arcade Physics body.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
moveTo: function (gameObject, x, y, speed, maxTime)
|
|
|
|
{
|
|
|
|
if (speed === undefined) { speed = 60; }
|
|
|
|
if (maxTime === undefined) { maxTime = 0; }
|
|
|
|
|
|
|
|
var angle = Math.atan2(y - gameObject.y, x - gameObject.x);
|
|
|
|
|
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
// We know how many pixels we need to move, but how fast?
|
|
|
|
speed = DistanceBetween(gameObject.x, gameObject.y, x, y) / (maxTime / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
gameObject.body.velocity.setToPolar(angle, speed);
|
|
|
|
|
|
|
|
return angle;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the given display object towards the destination object at a steady velocity.
|
|
|
|
* If you specify a maxTime then it will adjust the speed (overwriting 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.
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#moveToObject
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - Any Game Object with an Arcade Physics body.
|
|
|
|
* @param {object} destination - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
moveToObject: function (gameObject, destination, speed, maxTime)
|
|
|
|
{
|
|
|
|
return this.moveTo(gameObject, destination.x, destination.y, speed, maxTime);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-04-24 00:48:15 +00:00
|
|
|
* Given the angle (in degrees) and speed calculate the velocity and return it as a vector, or set it to the given vector object.
|
|
|
|
* One way to use this is: velocityFromAngle(angle, 200, sprite.body.velocity) which will set the values directly to the sprite's velocity and not create a new vector object.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#velocityFromAngle
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} angle - The angle in degrees calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
2018-04-24 00:48:15 +00:00
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second squared.
|
2018-03-28 13:11:46 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [vec2] - The Vector2 in which the x and y properties will be set to the calculated velocity.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2} The Vector2 that stores the velocity.
|
|
|
|
*/
|
|
|
|
velocityFromAngle: function (angle, speed, vec2)
|
|
|
|
{
|
|
|
|
if (speed === undefined) { speed = 60; }
|
2018-03-28 13:11:46 +00:00
|
|
|
if (vec2 === undefined) { vec2 = new Vector2(); }
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
return vec2.setToPolar(DegToRad(angle), speed);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-04-24 00:48:15 +00:00
|
|
|
* Given the rotation (in radians) and speed calculate the velocity and return it as a vector, or set it to the given vector object.
|
|
|
|
* One way to use this is: velocityFromRotation(rotation, 200, sprite.body.velocity) which will set the values directly to the sprite's velocity and not create a new vector object.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#velocityFromRotation
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} rotation - The angle in radians.
|
2018-04-24 00:48:15 +00:00
|
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second squared
|
2018-03-28 13:11:46 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [vec2] - The Vector2 in which the x and y properties will be set to the calculated velocity.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2} The Vector2 that stores the velocity.
|
|
|
|
*/
|
|
|
|
velocityFromRotation: function (rotation, speed, vec2)
|
|
|
|
{
|
|
|
|
if (speed === undefined) { speed = 60; }
|
2018-03-28 13:11:46 +00:00
|
|
|
if (vec2 === undefined) { vec2 = new Vector2(); }
|
2018-02-09 03:44:23 +00:00
|
|
|
|
|
|
|
return vec2.setToPolar(rotation, speed);
|
|
|
|
},
|
2018-01-17 03:41:58 +00:00
|
|
|
|
2019-03-07 15:16:31 +00:00
|
|
|
/**
|
|
|
|
* This method will search the given rectangular area and return an array of all physics bodies that
|
|
|
|
* overlap with it. It can return either Dynamic or Static bodies. If Arcade Physics is set to use
|
|
|
|
* the RTree (which it is by default) then the search is extremely fast, otherwise the search is O(N).
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#overlapRect
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The top-left x coordinate of the area to search within.
|
|
|
|
* @param {number} y - The top-left y coordinate of the area to search within.
|
|
|
|
* @param {number} width - The width of the area to search within.
|
|
|
|
* @param {number} height - The height of the area to search within.
|
|
|
|
* @param {boolean} [dynamic=true] - Search for Dynamic Bodies (true) or Static Bodies (false)
|
|
|
|
*
|
|
|
|
* @return {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[])} An array of bodies that overlap with the given area.
|
|
|
|
*/
|
|
|
|
overlapRect: function (x, y, width, height, dynamic)
|
|
|
|
{
|
|
|
|
if (dynamic === undefined) { dynamic = true; }
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
var world = this.world;
|
|
|
|
|
|
|
|
var minMax = world.treeMinMax;
|
|
|
|
|
|
|
|
minMax.minX = x;
|
|
|
|
minMax.minY = y;
|
|
|
|
minMax.maxX = x + width;
|
|
|
|
minMax.maxY = y + height;
|
|
|
|
|
|
|
|
if (!dynamic)
|
|
|
|
{
|
|
|
|
output = world.staticTree.search(minMax);
|
|
|
|
}
|
|
|
|
else if (world.useTree)
|
|
|
|
{
|
|
|
|
output = world.tree.search(minMax);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var bodies = world.bodies;
|
|
|
|
|
|
|
|
var fakeBody =
|
|
|
|
{
|
|
|
|
position: {
|
|
|
|
x: x,
|
|
|
|
y: y
|
|
|
|
},
|
|
|
|
left: x,
|
|
|
|
top: y,
|
|
|
|
right: x + width,
|
|
|
|
bottom: y + height,
|
|
|
|
isCircle: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var intersects = world.intersects;
|
|
|
|
|
|
|
|
bodies.iterate(function (target)
|
|
|
|
{
|
|
|
|
if (intersects(target, fakeBody))
|
|
|
|
{
|
|
|
|
output.push(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* The Scene that owns this plugin is shutting down.
|
|
|
|
* We need to kill and reset all internal properties as well as stop listening to Scene events.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#shutdown
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
shutdown: function ()
|
|
|
|
{
|
2018-09-29 10:21:41 +00:00
|
|
|
if (!this.world)
|
|
|
|
{
|
|
|
|
// Already destroyed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 16:12:17 +00:00
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
eventEmitter.off(SceneEvents.UPDATE, this.world.update, this.world);
|
|
|
|
eventEmitter.off(SceneEvents.POST_UPDATE, this.world.postUpdate, this.world);
|
|
|
|
eventEmitter.off(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2018-04-17 01:34:07 +00:00
|
|
|
this.add.destroy();
|
|
|
|
this.world.destroy();
|
|
|
|
|
|
|
|
this.add = null;
|
|
|
|
this.world = null;
|
2018-01-17 03:41:58 +00:00
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* The Scene that owns this plugin is being destroyed.
|
|
|
|
* We need to shutdown and then kill off all external references.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.ArcadePhysics#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-17 03:41:58 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2018-04-13 16:12:17 +00:00
|
|
|
this.shutdown();
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
this.scene.sys.events.off(SceneEvents.START, this.start, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
|
|
|
|
this.scene = null;
|
|
|
|
this.systems = null;
|
2018-01-17 03:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-05-15 11:51:50 +00:00
|
|
|
PluginCache.register('ArcadePhysics', ArcadePhysics, 'arcadePhysics');
|
2018-01-17 03:41:58 +00:00
|
|
|
|
|
|
|
module.exports = ArcadePhysics;
|