2014-03-05 02:36:08 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2014-03-05 02:36:08 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-03-06 06:27:16 +00:00
|
|
|
* The Physics Manager is responsible for looking after all of the running physics systems.
|
2014-11-25 12:09:03 +00:00
|
|
|
* Phaser supports 4 physics systems: Arcade Physics, P2, Ninja Physics and Box2D via a commercial plugin.
|
|
|
|
*
|
|
|
|
* Game Objects (such as Sprites) can only belong to 1 physics system, but you can have multiple systems active in a single game.
|
2014-03-05 02:36:08 +00:00
|
|
|
*
|
2014-03-06 06:27:16 +00:00
|
|
|
* For example you could have P2 managing a polygon-built terrain landscape that an vehicle drives over, while it could be firing bullets that use the
|
|
|
|
* faster (due to being much simpler) Arcade Physics system.
|
2014-03-05 02:36:08 +00:00
|
|
|
*
|
2014-03-06 06:27:16 +00:00
|
|
|
* @class Phaser.Physics
|
2014-03-05 02:36:08 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
* @param {object} [physicsConfig=null] - A physics configuration object to pass to the Physics world on creation.
|
|
|
|
*/
|
|
|
|
Phaser.Physics = function (game, config) {
|
|
|
|
|
2014-03-11 01:23:41 +00:00
|
|
|
config = config || {};
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {object} config - The physics configuration object as passed to the game on creation.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.config = config;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Arcade} arcade - The Arcade Physics system.
|
|
|
|
*/
|
2014-03-11 01:23:41 +00:00
|
|
|
this.arcade = null;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.P2} p2 - The P2.JS Physics system.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.p2 = null;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
2014-11-25 12:09:03 +00:00
|
|
|
* @property {Phaser.Physics.Ninja} ninja - The N+ Ninja Physics system.
|
2014-03-06 06:27:16 +00:00
|
|
|
*/
|
|
|
|
this.ninja = null;
|
|
|
|
|
|
|
|
/**
|
2014-11-25 12:09:03 +00:00
|
|
|
* @property {Phaser.Physics.Box2D} box2d - The Box2D Physics system.
|
2014-03-06 06:27:16 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.box2d = null;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Chipmunk} chipmunk - The Chipmunk Physics system (to be done).
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.chipmunk = null;
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Matter} matter - The MatterJS Physics system (coming soon).
|
|
|
|
*/
|
|
|
|
this.matter = null;
|
|
|
|
|
2014-03-11 01:23:41 +00:00
|
|
|
this.parseConfig();
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Physics.ARCADE = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-10 22:14:52 +00:00
|
|
|
Phaser.Physics.P2JS = 1;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-06 06:27:16 +00:00
|
|
|
Phaser.Physics.NINJA = 2;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-06 06:27:16 +00:00
|
|
|
Phaser.Physics.BOX2D = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-09-29 11:26:01 +00:00
|
|
|
Phaser.Physics.CHIPMUNK = 4;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Physics.MATTERJS = 5;
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
Phaser.Physics.prototype = {
|
|
|
|
|
2014-03-11 01:23:41 +00:00
|
|
|
/**
|
|
|
|
* Parses the Physics Configuration object passed to the Game constructor and starts any physics systems specified within.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#parseConfig
|
|
|
|
*/
|
|
|
|
parseConfig: function () {
|
|
|
|
|
|
|
|
if ((!this.config.hasOwnProperty('arcade') || this.config['arcade'] === true) && Phaser.Physics.hasOwnProperty('Arcade'))
|
|
|
|
{
|
|
|
|
// If Arcade isn't specified, we create it automatically if we can
|
|
|
|
this.arcade = new Phaser.Physics.Arcade(this.game);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.hasOwnProperty('ninja') && this.config['ninja'] === true && Phaser.Physics.hasOwnProperty('Ninja'))
|
|
|
|
{
|
|
|
|
this.ninja = new Phaser.Physics.Ninja(this.game);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.hasOwnProperty('p2') && this.config['p2'] === true && Phaser.Physics.hasOwnProperty('P2'))
|
|
|
|
{
|
|
|
|
this.p2 = new Phaser.Physics.P2(this.game, this.config);
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:26:01 +00:00
|
|
|
if (this.config.hasOwnProperty('box2d') && this.config['box2d'] === true && Phaser.Physics.hasOwnProperty('BOX2D'))
|
|
|
|
{
|
|
|
|
this.box2d = new Phaser.Physics.BOX2D(this.game, this.config);
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.config.hasOwnProperty('matter') && this.config['matter'] === true && Phaser.Physics.hasOwnProperty('Matter'))
|
|
|
|
{
|
|
|
|
this.matter = new Phaser.Physics.Matter(this.game, this.config);
|
|
|
|
}
|
|
|
|
|
2014-03-11 01:23:41 +00:00
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* This will create an instance of the requested physics simulation.
|
|
|
|
* Phaser.Physics.Arcade is running by default, but all others need activating directly.
|
2014-11-25 12:09:03 +00:00
|
|
|
*
|
2014-03-06 06:27:16 +00:00
|
|
|
* You can start the following physics systems:
|
2014-11-25 12:09:03 +00:00
|
|
|
*
|
2014-03-10 22:14:52 +00:00
|
|
|
* Phaser.Physics.P2JS - A full-body advanced physics system by Stefan Hedman.
|
2014-03-06 09:48:42 +00:00
|
|
|
* Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system.
|
2014-11-25 12:09:03 +00:00
|
|
|
* Phaser.Physics.BOX2D - A commercial Phaser Plugin (see http://phaser.io)
|
|
|
|
*
|
|
|
|
* Both Ninja Physics and Box2D require their respective plugins to be loaded before you can start them.
|
|
|
|
* They are not bundled into the core Phaser library.
|
|
|
|
*
|
|
|
|
* If the physics world has already been created (i.e. in another state in your game) then
|
|
|
|
* calling startSystem will reset the physics world, not re-create it. If you need to start them again from their constructors
|
|
|
|
* then set Phaser.Physics.p2 (or whichever system you want to recreate) to `null` before calling `startSystem`.
|
2014-03-06 06:27:16 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics#startSystem
|
2014-11-25 12:09:03 +00:00
|
|
|
* @param {number} system - The physics system to start: Phaser.Physics.ARCADE, Phaser.Physics.P2JS, Phaser.Physics.NINJA or Phaser.Physics.BOX2D.
|
2014-03-06 06:27:16 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
startSystem: function (system) {
|
|
|
|
|
2014-03-17 21:16:59 +00:00
|
|
|
if (system === Phaser.Physics.ARCADE)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
|
|
|
this.arcade = new Phaser.Physics.Arcade(this.game);
|
|
|
|
}
|
2014-03-17 21:16:59 +00:00
|
|
|
else if (system === Phaser.Physics.P2JS)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
2014-11-25 12:09:03 +00:00
|
|
|
if (this.p2 === null)
|
|
|
|
{
|
|
|
|
this.p2 = new Phaser.Physics.P2(this.game, this.config);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.p2.reset();
|
|
|
|
}
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
2014-11-25 12:09:03 +00:00
|
|
|
else if (system === Phaser.Physics.NINJA)
|
2014-03-06 06:27:16 +00:00
|
|
|
{
|
|
|
|
this.ninja = new Phaser.Physics.Ninja(this.game);
|
|
|
|
}
|
2014-11-25 12:09:03 +00:00
|
|
|
else if (system === Phaser.Physics.BOX2D)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
2014-11-25 12:09:03 +00:00
|
|
|
if (this.box2d === null)
|
|
|
|
{
|
|
|
|
this.box2d = new Phaser.Physics.Box2D(this.game, this.config);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.box2d.reset();
|
|
|
|
}
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
2015-04-13 15:16:34 +00:00
|
|
|
else if (system === Phaser.Physics.MATTERJS)
|
|
|
|
{
|
|
|
|
if (this.matter === null)
|
|
|
|
{
|
|
|
|
this.matter = new Phaser.Physics.Matter(this.game, this.config);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.matter.reset();
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
2014-03-11 01:11:08 +00:00
|
|
|
* This will create a default physics body on the given game object or array of objects.
|
2014-03-06 06:27:16 +00:00
|
|
|
* A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed.
|
|
|
|
* It can be for any of the physics systems that have been started:
|
|
|
|
*
|
|
|
|
* Phaser.Physics.Arcade - A light weight AABB based collision system with basic separation.
|
2014-03-10 22:14:52 +00:00
|
|
|
* Phaser.Physics.P2JS - A full-body advanced physics system supporting multiple object shapes, polygon loading, contact materials, springs and constraints.
|
2014-03-06 09:48:42 +00:00
|
|
|
* Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system. Advanced AABB and Circle vs. Tile collision.
|
2014-09-29 11:26:01 +00:00
|
|
|
* Phaser.Physics.BOX2D - A port of https://code.google.com/p/box2d-html5
|
2015-04-13 15:16:34 +00:00
|
|
|
* Phaser.Physics.MATTER - A full-body and light-weight advanced physics system (still in development)
|
2014-09-29 11:26:01 +00:00
|
|
|
* Phaser.Physics.CHIPMUNK is still in development.
|
2014-03-06 06:27:16 +00:00
|
|
|
*
|
2014-03-11 01:11:08 +00:00
|
|
|
* If you require more control over what type of body is created, for example to create a Ninja Physics Circle instead of the default AABB, then see the
|
|
|
|
* individual physics systems `enable` methods instead of using this generic one.
|
|
|
|
*
|
2014-03-06 06:27:16 +00:00
|
|
|
* @method Phaser.Physics#enable
|
|
|
|
* @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array.
|
2014-03-07 15:14:53 +00:00
|
|
|
* @param {number} [system=Phaser.Physics.ARCADE] - The physics system that will be used to create the body. Defaults to Arcade Physics.
|
2014-03-10 13:28:44 +00:00
|
|
|
* @param {boolean} [debug=false] - Enable the debug drawing for this body. Defaults to false.
|
2014-03-06 06:27:16 +00:00
|
|
|
*/
|
2014-03-10 13:28:44 +00:00
|
|
|
enable: function (object, system, debug) {
|
2014-03-05 02:36:08 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (system === undefined) { system = Phaser.Physics.ARCADE; }
|
|
|
|
if (debug === undefined) { debug = false; }
|
2014-03-05 02:36:08 +00:00
|
|
|
|
2014-03-11 01:11:08 +00:00
|
|
|
if (system === Phaser.Physics.ARCADE)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
2014-03-11 01:11:08 +00:00
|
|
|
this.arcade.enable(object);
|
|
|
|
}
|
|
|
|
else if (system === Phaser.Physics.P2JS && this.p2)
|
|
|
|
{
|
|
|
|
this.p2.enable(object, debug);
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
2014-03-11 01:11:08 +00:00
|
|
|
else if (system === Phaser.Physics.NINJA && this.ninja)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
2014-03-11 01:11:08 +00:00
|
|
|
this.ninja.enableAABB(object);
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
2014-09-29 11:26:01 +00:00
|
|
|
else if (system === Phaser.Physics.BOX2D && this.box2d)
|
|
|
|
{
|
|
|
|
this.box2d.enable(object);
|
|
|
|
}
|
2015-04-13 15:16:34 +00:00
|
|
|
else if (system === Phaser.Physics.MATTERJS && this.matter)
|
|
|
|
{
|
|
|
|
this.matter.enable(object);
|
|
|
|
}
|
2016-04-05 15:23:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
console.warn(object.key + ' is attempting to enable a physics body using an unknown physics system.');
|
|
|
|
}
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-13 21:14:10 +00:00
|
|
|
/**
|
|
|
|
* preUpdate checks.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#preUpdate
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
preUpdate: function () {
|
|
|
|
|
|
|
|
// ArcadePhysics / Ninja don't have a core to preUpdate
|
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.preUpdate();
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:26:01 +00:00
|
|
|
if (this.box2d)
|
|
|
|
{
|
|
|
|
this.box2d.preUpdate();
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.matter)
|
|
|
|
{
|
|
|
|
this.matter.preUpdate();
|
|
|
|
}
|
|
|
|
|
2014-03-13 21:14:10 +00:00
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* Updates all running physics systems.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#update
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
update: function () {
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
// ArcadePhysics / Ninja don't have a core to update
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.update();
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:26:01 +00:00
|
|
|
if (this.box2d)
|
|
|
|
{
|
|
|
|
this.box2d.update();
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.matter)
|
|
|
|
{
|
|
|
|
this.matter.update();
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* Updates the physics bounds to match the world dimensions.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#setBoundsToWorld
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
setBoundsToWorld: function () {
|
|
|
|
|
2014-03-19 02:28:20 +00:00
|
|
|
if (this.arcade)
|
|
|
|
{
|
|
|
|
this.arcade.setBoundsToWorld();
|
|
|
|
}
|
|
|
|
|
2014-03-06 09:48:42 +00:00
|
|
|
if (this.ninja)
|
|
|
|
{
|
|
|
|
this.ninja.setBoundsToWorld();
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:02:59 +00:00
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.setBoundsToWorld();
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:26:01 +00:00
|
|
|
if (this.box2d)
|
|
|
|
{
|
2014-09-29 11:29:18 +00:00
|
|
|
this.box2d.setBoundsToWorld();
|
2014-09-29 11:26:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.matter)
|
|
|
|
{
|
|
|
|
this.matter.setBoundsToWorld();
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* Clears down all active physics systems. This doesn't destroy them, it just clears them of objects and is called when the State changes.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#clear
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
clear: function () {
|
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.clear();
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:26:01 +00:00
|
|
|
if (this.box2d)
|
|
|
|
{
|
|
|
|
this.box2d.clear();
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.matter)
|
|
|
|
{
|
|
|
|
this.matter.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:02:59 +00:00
|
|
|
},
|
|
|
|
|
2015-03-24 10:21:12 +00:00
|
|
|
/**
|
|
|
|
* Resets the active physics system. Called automatically on a Phaser.State swap.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#reset
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
reset: function () {
|
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.box2d)
|
|
|
|
{
|
|
|
|
this.box2d.reset();
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.matter)
|
|
|
|
{
|
|
|
|
this.matter.reset();
|
|
|
|
}
|
|
|
|
|
2015-03-24 10:21:12 +00:00
|
|
|
},
|
|
|
|
|
2014-03-11 15:02:59 +00:00
|
|
|
/**
|
|
|
|
* Destroys all active physics systems. Usually only called on a Game Shutdown, not on a State swap.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#destroy
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.destroy();
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:26:01 +00:00
|
|
|
if (this.box2d)
|
|
|
|
{
|
|
|
|
this.box2d.destroy();
|
|
|
|
}
|
|
|
|
|
2015-04-13 15:16:34 +00:00
|
|
|
if (this.matter)
|
|
|
|
{
|
|
|
|
this.matter.destroy();
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:02:59 +00:00
|
|
|
this.arcade = null;
|
|
|
|
this.ninja = null;
|
|
|
|
this.p2 = null;
|
2014-09-29 11:26:01 +00:00
|
|
|
this.box2d = null;
|
2015-04-13 15:16:34 +00:00
|
|
|
this.matter = null;
|
2014-03-11 15:02:59 +00:00
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.prototype.constructor = Phaser.Physics;
|