2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
// Add an extra properties to p2 that we need
|
2014-02-12 19:45:09 +00:00
|
|
|
p2.Body.prototype.parent = null;
|
2014-02-18 15:16:26 +00:00
|
|
|
p2.Spring.prototype.parent = null;
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @class Phaser.Physics.P2
|
2014-02-10 16:01:30 +00:00
|
|
|
* @classdesc Physics World Constructor
|
|
|
|
* @constructor
|
2014-02-19 03:51:48 +00:00
|
|
|
* @param {Phaser.Game} game - Reference to the current game instance.
|
|
|
|
* @param {object} [config] - Physics configuration object passed in from the game constructor.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Phaser.Physics.P2 = function (game, config) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
2014-02-19 03:51:48 +00:00
|
|
|
if (typeof config === 'undefined')
|
|
|
|
{
|
|
|
|
config = { gravity: [0, 0], broadphase: new p2.SAPBroadphase() };
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:37:53 +00:00
|
|
|
/**
|
|
|
|
* @property {p2.World} game - The p2 World in which the simulation is run.
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-02-19 03:51:48 +00:00
|
|
|
this.world = new p2.World(config);
|
2014-02-18 15:16:26 +00:00
|
|
|
|
2014-02-18 16:37:53 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @property {array<Phaser.Physics.P2.Material>} materials - A local array of all created Materials.
|
2014-02-18 16:37:53 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
this.materials = [];
|
|
|
|
|
2014-02-18 04:49:03 +00:00
|
|
|
/**
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {Phaser.InversePointProxy} gravity - The gravity applied to all bodies each step.
|
2014-02-18 04:49:03 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.gravity = new Phaser.Physics.P2.InversePointProxy(game, this.world.gravity);
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {p2.Body} bounds - The bounds body contains the 4 walls that border the World. Define or disable with setBounds.
|
|
|
|
*/
|
|
|
|
this.bounds = null;
|
2014-02-18 04:49:03 +00:00
|
|
|
|
2014-02-18 17:25:54 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _wallShapes - The wall bounds shapes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._wallShapes = [ null, null, null, null ];
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onBodyAdded - Dispatched when a new Body is added to the World.
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
this.onBodyAdded = new Phaser.Signal();
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onBodyRemoved - Dispatched when a Body is removed from the World.
|
|
|
|
*/
|
2014-02-12 19:45:09 +00:00
|
|
|
this.onBodyRemoved = new Phaser.Signal();
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onSpringAdded - Dispatched when a new Spring is added to the World.
|
|
|
|
*/
|
|
|
|
this.onSpringAdded = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onSpringRemoved - Dispatched when a Spring is removed from the World.
|
|
|
|
*/
|
|
|
|
this.onSpringRemoved = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onConstraintAdded - Dispatched when a new Constraint is added to the World.
|
|
|
|
*/
|
|
|
|
this.onConstraintAdded = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onConstraintRemoved - Dispatched when a Constraint is removed from the World.
|
|
|
|
*/
|
|
|
|
this.onConstraintRemoved = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onContactMaterialAdded - Dispatched when a new ContactMaterial is added to the World.
|
|
|
|
*/
|
|
|
|
this.onContactMaterialAdded = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onContactMaterialRemoved - Dispatched when a ContactMaterial is removed from the World.
|
|
|
|
*/
|
|
|
|
this.onContactMaterialRemoved = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onPostStep - Dispatched after the World.step()
|
|
|
|
*/
|
|
|
|
this.onPostStep = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onPostBroadphase - Dispatched after the Broadphase has collected collision pairs in the world.
|
|
|
|
*/
|
|
|
|
this.onPostBroadphase = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onImpact - Dispatched when a first contact is created between two bodies. This event is fired after the step has been done.
|
|
|
|
*/
|
|
|
|
this.onImpact = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onBeginContact - Dispatched when a first contact is created between two bodies. This event is fired before the step has been done.
|
|
|
|
*/
|
|
|
|
this.onBeginContact = new Phaser.Signal();
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onEndContact - Dispatched when final contact occurs between two bodies. This event is fired before the step has been done.
|
|
|
|
*/
|
|
|
|
this.onEndContact = new Phaser.Signal();
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
// Hook into the World events
|
|
|
|
this.world.on("postStep", this.postStepHandler, this);
|
|
|
|
this.world.on("postBroadphase", this.postBroadphaseHandler, this);
|
|
|
|
this.world.on("impact", this.impactHandler, this);
|
|
|
|
this.world.on("beginContact", this.beginContactHandler, this);
|
|
|
|
this.world.on("endContact", this.endContactHandler, this);
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-19 01:51:14 +00:00
|
|
|
/**
|
|
|
|
* @property {array} collisionGroups - Internal var.
|
|
|
|
*/
|
|
|
|
this.collisionGroups = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _collisionGroupID - Internal var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._collisionGroupID = 2;
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
this.nothingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(1);
|
|
|
|
this.boundsCollisionGroup = new Phaser.Physics.P2.CollisionGroup(2);
|
|
|
|
this.everythingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(2147483648);
|
2014-02-20 04:21:14 +00:00
|
|
|
|
2014-02-19 01:51:14 +00:00
|
|
|
this.boundsCollidesWith = [];
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
// Group vs. Group callbacks
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
// By default we want everything colliding with everything
|
|
|
|
this.setBoundsToWorld(true, true, true, true, false);
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
};
|
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Physics.P2.LIME_CORONA_JSON = 0;
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
Phaser.Physics.P2.prototype = {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Handles a p2 postStep event.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#postStepHandler
|
2014-02-18 15:16:26 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
postStepHandler: function (event) {
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Fired after the Broadphase has collected collision pairs in the world.
|
|
|
|
* Inside the event handler, you can modify the pairs array as you like, to prevent collisions between objects that you don't want.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#postBroadphaseHandler
|
2014-02-18 15:16:26 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
postBroadphaseHandler: function (event) {
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
// Body.id 1 is always the World bounds object
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-19 03:51:48 +00:00
|
|
|
for (var i = 0; i < event.pairs.length; i += 2)
|
2014-02-18 15:16:26 +00:00
|
|
|
{
|
2014-02-19 03:51:48 +00:00
|
|
|
var a = event.pairs[i];
|
|
|
|
var b = event.pairs[i+1];
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-19 03:51:48 +00:00
|
|
|
if (a.id !== 1 && b.id !== 1)
|
2014-02-18 15:16:26 +00:00
|
|
|
{
|
2014-02-19 03:51:48 +00:00
|
|
|
// console.log('postBroadphaseHandler', a, b);
|
2014-02-18 15:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Handles a p2 impact event.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#impactHandler
|
2014-02-18 15:16:26 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
impactHandler: function (event) {
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
if (event.bodyA.parent && event.bodyB.parent)
|
2014-02-19 03:51:48 +00:00
|
|
|
{
|
2014-02-19 05:22:37 +00:00
|
|
|
// Body vs. Body callbacks
|
|
|
|
var a = event.bodyA.parent;
|
|
|
|
var b = event.bodyB.parent;
|
|
|
|
|
|
|
|
if (a._bodyCallbacks[event.bodyB.id])
|
|
|
|
{
|
|
|
|
a._bodyCallbacks[event.bodyB.id].call(a._bodyCallbackContext[event.bodyB.id], a, b, event.shapeA, event.shapeB);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b._bodyCallbacks[event.bodyA.id])
|
|
|
|
{
|
|
|
|
b._bodyCallbacks[event.bodyA.id].call(b._bodyCallbackContext[event.bodyA.id], b, a, event.shapeB, event.shapeA);
|
|
|
|
}
|
2014-02-19 03:51:48 +00:00
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
// Body vs. Group callbacks
|
|
|
|
if (a._groupCallbacks[event.shapeB.collisionGroup])
|
2014-02-19 04:05:12 +00:00
|
|
|
{
|
2014-02-19 05:22:37 +00:00
|
|
|
a._groupCallbacks[event.shapeB.collisionGroup].call(a._groupCallbackContext[event.shapeB.collisionGroup], a, b, event.shapeA, event.shapeB);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b._groupCallbacks[event.shapeA.collisionGroup])
|
|
|
|
{
|
|
|
|
b._groupCallbacks[event.shapeA.collisionGroup].call(b._groupCallbackContext[event.shapeA.collisionGroup], b, a, event.shapeB, event.shapeA);
|
2014-02-19 04:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 03:51:48 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Handles a p2 begin contact event.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#beginContactHandler
|
2014-02-18 15:16:26 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
beginContactHandler: function (event) {
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
// console.log('beginContactHandler');
|
|
|
|
// console.log(event);
|
2014-02-19 15:01:59 +00:00
|
|
|
|
2014-02-19 03:51:48 +00:00
|
|
|
if (event.bodyA.id > 1 && event.bodyB.id > 1)
|
|
|
|
{
|
2014-02-19 05:22:37 +00:00
|
|
|
// console.log('beginContactHandler');
|
|
|
|
// console.log(event.bodyA.parent.sprite.key);
|
|
|
|
// console.log(event.bodyB.parent.sprite.key);
|
2014-02-19 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a p2 end contact event.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#endContactHandler
|
2014-02-18 15:16:26 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
endContactHandler: function (event) {
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
// console.log('endContactHandler');
|
|
|
|
// console.log(event);
|
2014-02-19 15:01:59 +00:00
|
|
|
|
|
|
|
|
2014-02-19 03:51:48 +00:00
|
|
|
if (event.bodyA.id > 1 && event.bodyB.id > 1)
|
|
|
|
{
|
2014-02-19 05:22:37 +00:00
|
|
|
// console.log('endContactHandler');
|
|
|
|
// console.log(event);
|
2014-02-19 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the bounds of the Physics world to match the Game.World dimensions.
|
|
|
|
* You can optionally set which 'walls' to create: left, right, top or bottom.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#setBoundsToWorld
|
|
|
|
* @param {boolean} [left=true] - If true will create the left bounds wall.
|
|
|
|
* @param {boolean} [right=true] - If true will create the right bounds wall.
|
|
|
|
* @param {boolean} [top=true] - If true will create the top bounds wall.
|
|
|
|
* @param {boolean} [bottom=true] - If true will create the bottom bounds wall.
|
2014-02-19 03:51:48 +00:00
|
|
|
* @param {boolean} [setCollisionGroup=true] - If true the Bounds will be set to use its own Collision Group.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
2014-02-19 03:51:48 +00:00
|
|
|
setBoundsToWorld: function (left, right, top, bottom, setCollisionGroup) {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
2014-02-19 03:51:48 +00:00
|
|
|
this.setBounds(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, left, right, top, bottom, setCollisionGroup);
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 17:25:54 +00:00
|
|
|
/**
|
|
|
|
* Sets the given material against the 4 bounds of this World.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#setWorldMaterial
|
2014-03-05 02:36:08 +00:00
|
|
|
* @param {Phaser.Physics.P2.Material} material - The material to set.
|
2014-02-18 17:25:54 +00:00
|
|
|
* @param {boolean} [left=true] - If true will set the material on the left bounds wall.
|
|
|
|
* @param {boolean} [right=true] - If true will set the material on the right bounds wall.
|
|
|
|
* @param {boolean} [top=true] - If true will set the material on the top bounds wall.
|
|
|
|
* @param {boolean} [bottom=true] - If true will set the material on the bottom bounds wall.
|
|
|
|
*/
|
|
|
|
setWorldMaterial: function (material, left, right, top, bottom) {
|
|
|
|
|
|
|
|
if (typeof left === 'undefined') { left = true; }
|
|
|
|
if (typeof right === 'undefined') { right = true; }
|
|
|
|
if (typeof top === 'undefined') { top = true; }
|
|
|
|
if (typeof bottom === 'undefined') { bottom = true; }
|
|
|
|
|
|
|
|
if (left && this._wallShapes[0])
|
|
|
|
{
|
|
|
|
this._wallShapes[0].material = material;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (right && this._wallShapes[1])
|
|
|
|
{
|
|
|
|
this._wallShapes[1].material = material;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top && this._wallShapes[2])
|
|
|
|
{
|
|
|
|
this._wallShapes[2].material = material;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bottom && this._wallShapes[3])
|
|
|
|
{
|
|
|
|
this._wallShapes[3].material = material;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Sets the bounds of the Physics world to match the given world pixel dimensions.
|
|
|
|
* You can optionally set which 'walls' to create: left, right, top or bottom.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#setBounds
|
2014-02-18 15:16:26 +00:00
|
|
|
* @param {number} x - The x coordinate of the top-left corner of the bounds.
|
|
|
|
* @param {number} y - The y coordinate of the top-left corner of the bounds.
|
|
|
|
* @param {number} width - The width of the bounds.
|
|
|
|
* @param {number} height - The height of the bounds.
|
|
|
|
* @param {boolean} [left=true] - If true will create the left bounds wall.
|
|
|
|
* @param {boolean} [right=true] - If true will create the right bounds wall.
|
|
|
|
* @param {boolean} [top=true] - If true will create the top bounds wall.
|
|
|
|
* @param {boolean} [bottom=true] - If true will create the bottom bounds wall.
|
2014-02-19 03:51:48 +00:00
|
|
|
* @param {boolean} [setCollisionGroup=true] - If true the Bounds will be set to use its own Collision Group.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
2014-02-19 03:51:48 +00:00
|
|
|
setBounds: function (x, y, width, height, left, right, top, bottom, setCollisionGroup) {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
if (typeof left === 'undefined') { left = true; }
|
|
|
|
if (typeof right === 'undefined') { right = true; }
|
|
|
|
if (typeof top === 'undefined') { top = true; }
|
|
|
|
if (typeof bottom === 'undefined') { bottom = true; }
|
2014-02-19 03:51:48 +00:00
|
|
|
if (typeof setCollisionGroup === 'undefined') { setCollisionGroup = true; }
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
var hw = (width / 2);
|
|
|
|
var hh = (height / 2);
|
|
|
|
var cx = hw + x;
|
|
|
|
var cy = hh + y;
|
|
|
|
|
|
|
|
if (this.bounds !== null)
|
|
|
|
{
|
2014-03-03 05:19:46 +00:00
|
|
|
if (this.bounds.world)
|
|
|
|
{
|
|
|
|
this.world.removeBody(this.bounds);
|
|
|
|
}
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
var i = this.bounds.shapes.length;
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
var shape = this.bounds.shapes[i];
|
|
|
|
this.bounds.removeShape(shape);
|
|
|
|
}
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-19 15:21:03 +00:00
|
|
|
this.bounds.position[0] = this.game.math.px2pi(cx);
|
|
|
|
this.bounds.position[1] = this.game.math.px2pi(cy);
|
2014-02-18 15:16:26 +00:00
|
|
|
}
|
|
|
|
else
|
2014-02-12 19:45:09 +00:00
|
|
|
{
|
2014-02-19 15:21:03 +00:00
|
|
|
this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2pi(cx), this.game.math.px2pi(cy)] });
|
2014-02-12 19:45:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
if (left)
|
|
|
|
{
|
2014-02-18 17:25:54 +00:00
|
|
|
this._wallShapes[0] = new p2.Plane();
|
2014-02-19 03:51:48 +00:00
|
|
|
|
|
|
|
if (setCollisionGroup)
|
|
|
|
{
|
|
|
|
this._wallShapes[0].collisionGroup = this.boundsCollisionGroup.mask;
|
2014-02-20 04:21:14 +00:00
|
|
|
// this._wallShapes[0].collisionGroup = this.everythingCollisionGroup.mask;
|
|
|
|
// this._wallShapes[0].collisionMask = this.everythingCollisionGroup.mask;
|
2014-02-19 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2pi(-hw), 0], 1.5707963267948966 );
|
2014-02-18 15:16:26 +00:00
|
|
|
}
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
if (right)
|
|
|
|
{
|
2014-02-18 17:25:54 +00:00
|
|
|
this._wallShapes[1] = new p2.Plane();
|
2014-02-19 03:51:48 +00:00
|
|
|
|
|
|
|
if (setCollisionGroup)
|
|
|
|
{
|
|
|
|
this._wallShapes[1].collisionGroup = this.boundsCollisionGroup.mask;
|
2014-02-20 04:21:14 +00:00
|
|
|
// this._wallShapes[1].collisionGroup = this.everythingCollisionGroup.mask;
|
|
|
|
// this._wallShapes[1].collisionMask = this.everythingCollisionGroup.mask;
|
2014-02-19 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2pi(hw), 0], -1.5707963267948966 );
|
2014-02-18 15:16:26 +00:00
|
|
|
}
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
if (top)
|
|
|
|
{
|
2014-02-18 17:25:54 +00:00
|
|
|
this._wallShapes[2] = new p2.Plane();
|
2014-02-19 03:51:48 +00:00
|
|
|
|
|
|
|
if (setCollisionGroup)
|
|
|
|
{
|
|
|
|
this._wallShapes[2].collisionGroup = this.boundsCollisionGroup.mask;
|
2014-02-20 04:21:14 +00:00
|
|
|
// this._wallShapes[2].collisionGroup = this.everythingCollisionGroup.mask;
|
|
|
|
// this._wallShapes[2].collisionMask = this.everythingCollisionGroup.mask;
|
2014-02-19 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2pi(-hh)], -3.141592653589793 );
|
2014-02-18 15:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bottom)
|
|
|
|
{
|
2014-02-18 17:25:54 +00:00
|
|
|
this._wallShapes[3] = new p2.Plane();
|
2014-02-19 03:51:48 +00:00
|
|
|
|
|
|
|
if (setCollisionGroup)
|
|
|
|
{
|
|
|
|
this._wallShapes[3].collisionGroup = this.boundsCollisionGroup.mask;
|
2014-02-20 04:21:14 +00:00
|
|
|
// this._wallShapes[3].collisionGroup = this.everythingCollisionGroup.mask;
|
|
|
|
// this._wallShapes[3].collisionMask = this.everythingCollisionGroup.mask;
|
2014-02-19 03:51:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 04:21:14 +00:00
|
|
|
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2pi(hh)] );
|
2014-02-18 15:16:26 +00:00
|
|
|
}
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
this.world.addBody(this.bounds);
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#update
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
update: function () {
|
|
|
|
|
|
|
|
this.world.step(1 / 60);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-02 11:04:04 +00:00
|
|
|
* Clears all bodies from the simulation.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#clear
|
2014-03-02 11:04:04 +00:00
|
|
|
*/
|
|
|
|
clear: function () {
|
|
|
|
|
|
|
|
this.world.clear();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all bodies from the simulation and unlinks World from Game. Should only be called on game shutdown. Call `clear` on a State change.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#destroy
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this.world.clear();
|
|
|
|
|
|
|
|
this.game = null;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a body to the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#addBody
|
|
|
|
* @param {Phaser.Physics.P2.Body} body - The Body to add to the World.
|
2014-02-18 15:16:26 +00:00
|
|
|
* @return {boolean} True if the Body was added successfully, otherwise false.
|
|
|
|
*/
|
|
|
|
addBody: function (body) {
|
|
|
|
|
|
|
|
if (body.data.world)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.world.addBody(body.data);
|
|
|
|
|
|
|
|
this.onBodyAdded.dispatch(body);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a body from the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#removeBody
|
|
|
|
* @param {Phaser.Physics.P2.Body} body - The Body to remove from the World.
|
|
|
|
* @return {Phaser.Physics.P2.Body} The Body that was removed.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
removeBody: function (body) {
|
|
|
|
|
|
|
|
this.world.removeBody(body.data);
|
|
|
|
|
|
|
|
this.onBodyRemoved.dispatch(body);
|
|
|
|
|
|
|
|
return body;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Spring to the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#addSpring
|
|
|
|
* @param {Phaser.Physics.P2.Spring} spring - The Spring to add to the World.
|
|
|
|
* @return {Phaser.Physics.P2.Spring} The Spring that was added.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
addSpring: function (spring) {
|
|
|
|
|
|
|
|
this.world.addSpring(spring);
|
|
|
|
|
|
|
|
this.onSpringAdded.dispatch(spring);
|
|
|
|
|
|
|
|
return spring;
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Removes a Spring from the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#removeSpring
|
|
|
|
* @param {Phaser.Physics.P2.Spring} spring - The Spring to remove from the World.
|
|
|
|
* @return {Phaser.Physics.P2.Spring} The Spring that was removed.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
removeSpring: function (spring) {
|
|
|
|
|
|
|
|
this.world.removeSpring(spring);
|
|
|
|
|
|
|
|
this.onSpringRemoved.dispatch(spring);
|
|
|
|
|
|
|
|
return spring;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Constraint to the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#addConstraint
|
|
|
|
* @param {Phaser.Physics.P2.Constraint} constraint - The Constraint to add to the World.
|
|
|
|
* @return {Phaser.Physics.P2.Constraint} The Constraint that was added.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
addConstraint: function (constraint) {
|
|
|
|
|
|
|
|
this.world.addConstraint(constraint);
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
this.onConstraintAdded.dispatch(constraint);
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
return constraint;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a Constraint from the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#removeConstraint
|
|
|
|
* @param {Phaser.Physics.P2.Constraint} constraint - The Constraint to be removed from the World.
|
|
|
|
* @return {Phaser.Physics.P2.Constraint} The Constraint that was removed.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
removeConstraint: function (constraint) {
|
|
|
|
|
|
|
|
this.world.removeConstraint(constraint);
|
|
|
|
|
|
|
|
this.onConstraintRemoved.dispatch(constraint);
|
|
|
|
|
|
|
|
return constraint;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Contact Material to the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#addContactMaterial
|
|
|
|
* @param {Phaser.Physics.P2.ContactMaterial} material - The Contact Material to be added to the World.
|
|
|
|
* @return {Phaser.Physics.P2.ContactMaterial} The Contact Material that was added.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
addContactMaterial: function (material) {
|
|
|
|
|
|
|
|
this.world.addContactMaterial(material);
|
|
|
|
|
|
|
|
this.onContactMaterialAdded.dispatch(material);
|
|
|
|
|
|
|
|
return material;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a Contact Material from the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#removeContactMaterial
|
|
|
|
* @param {Phaser.Physics.P2.ContactMaterial} material - The Contact Material to be removed from the World.
|
|
|
|
* @return {Phaser.Physics.P2.ContactMaterial} The Contact Material that was removed.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
removeContactMaterial: function (material) {
|
|
|
|
|
|
|
|
this.world.removeContactMaterial(material);
|
|
|
|
|
|
|
|
this.onContactMaterialRemoved.dispatch(material);
|
|
|
|
|
|
|
|
return material;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Contact Material based on the two given Materials.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#getContactMaterial
|
|
|
|
* @param {Phaser.Physics.P2.Material} materialA - The first Material to search for.
|
|
|
|
* @param {Phaser.Physics.P2.Material} materialB - The second Material to search for.
|
|
|
|
* @return {Phaser.Physics.P2.ContactMaterial|boolean} The Contact Material or false if none was found matching the Materials given.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
getContactMaterial: function (materialA, materialB) {
|
|
|
|
|
|
|
|
return this.world.getContactMaterial(materialA, materialB);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 16:37:53 +00:00
|
|
|
/**
|
|
|
|
* Sets the given Material against all Shapes owned by all the Bodies in the given array.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#setMaterial
|
|
|
|
* @param {Phaser.Physics.P2.Material} material - The Material to be applied to the given Bodies.
|
|
|
|
* @param {array<Phaser.Physics.P2.Body>} bodies - An Array of Body objects that the given Material will be set on.
|
2014-02-18 16:37:53 +00:00
|
|
|
*/
|
|
|
|
setMaterial: function (material, bodies) {
|
|
|
|
|
|
|
|
var i = bodies.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
bodies.setMaterial(material);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Material. Materials are applied to Shapes owned by a Body and can be set with Body.setMaterial().
|
|
|
|
* Materials are a way to control what happens when Shapes collide. Combine unique Materials together to create Contact Materials.
|
|
|
|
* Contact Materials have properties such as friction and restitution that allow for fine-grained collision control between different Materials.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#createMaterial
|
2014-02-18 16:37:53 +00:00
|
|
|
* @param {string} [name] - Optional name of the Material. Each Material has a unique ID but string names are handy for debugging.
|
2014-03-05 02:36:08 +00:00
|
|
|
* @param {Phaser.Physics.P2.Body} [body] - Optional Body. If given it will assign the newly created Material to the Body shapes.
|
|
|
|
* @return {Phaser.Physics.P2.Material} The Material that was created. This is also stored in Phaser.Physics.P2.materials.
|
2014-02-18 16:37:53 +00:00
|
|
|
*/
|
|
|
|
createMaterial: function (name, body) {
|
|
|
|
|
|
|
|
name = name || '';
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
var material = new Phaser.Physics.P2.Material(name);
|
2014-02-18 16:37:53 +00:00
|
|
|
|
|
|
|
this.materials.push(material);
|
|
|
|
|
|
|
|
if (typeof body !== 'undefined')
|
|
|
|
{
|
|
|
|
body.setMaterial(material);
|
|
|
|
}
|
|
|
|
|
|
|
|
return material;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Creates a Contact Material from the two given Materials. You can then edit the properties of the Contact Material directly.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#createContactMaterial
|
|
|
|
* @param {Phaser.Physics.P2.Material} [materialA] - The first Material to create the ContactMaterial from. If undefined it will create a new Material object first.
|
|
|
|
* @param {Phaser.Physics.P2.Material} [materialB] - The second Material to create the ContactMaterial from. If undefined it will create a new Material object first.
|
2014-02-18 17:25:54 +00:00
|
|
|
* @param {object} [options] - Material options object.
|
2014-03-05 02:36:08 +00:00
|
|
|
* @return {Phaser.Physics.P2.ContactMaterial} The Contact Material that was created.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
2014-02-18 17:25:54 +00:00
|
|
|
createContactMaterial: function (materialA, materialB, options) {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
2014-02-18 16:37:53 +00:00
|
|
|
if (typeof materialA === 'undefined') { materialA = this.createMaterial(); }
|
|
|
|
if (typeof materialB === 'undefined') { materialB = this.createMaterial(); }
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
var contact = new Phaser.Physics.P2.ContactMaterial(materialA, materialB, options);
|
2014-02-18 16:37:53 +00:00
|
|
|
|
|
|
|
return this.addContactMaterial(contact);
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates and returns an array of all current Bodies in the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#getBodies
|
|
|
|
* @return {array<Phaser.Physics.P2.Body>} An array containing all current Bodies in the world.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
getBodies: function () {
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
var i = this.world.bodies.length;
|
2014-02-14 17:52:59 +00:00
|
|
|
|
|
|
|
while (i--)
|
2014-02-12 19:45:09 +00:00
|
|
|
{
|
2014-02-18 15:16:26 +00:00
|
|
|
output.push(this.world.bodies[i].parent);
|
2014-02-12 19:45:09 +00:00
|
|
|
}
|
2014-02-14 17:52:59 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
return output;
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
|
|
|
* Populates and returns an array of all current Springs in the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#getSprings
|
|
|
|
* @return {array<Phaser.Physics.P2.Spring>} An array containing all current Springs in the world.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
getSprings: function () {
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
var output = [];
|
|
|
|
var i = this.world.springs.length;
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
output.push(this.world.springs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates and returns an array of all current Constraints in the world.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#getConstraints
|
|
|
|
* @return {array<Phaser.Physics.P2.Constraints>} An array containing all current Constraints in the world.
|
2014-02-18 15:16:26 +00:00
|
|
|
*/
|
|
|
|
getConstraints: function () {
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
var i = this.world.constraints.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
output.push(this.world.springs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a world point overlaps bodies.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#hitTest
|
2014-02-18 15:16:26 +00:00
|
|
|
* @param {Phaser.Point} worldPoint - Point to use for intersection tests.
|
|
|
|
* @param {Array} bodies - A list of objects to check for intersection.
|
|
|
|
* @param {number} precision - Used for matching against particles and lines. Adds some margin to these infinitesimal objects.
|
|
|
|
* @return {Array} Array of bodies that overlap the point.
|
|
|
|
*/
|
|
|
|
hitTest: function (worldPoint, bodies, precision) {
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the current world into a JSON object.
|
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2#toJSON
|
2014-02-18 15:16:26 +00:00
|
|
|
* @return {object} A JSON representation of the world.
|
|
|
|
*/
|
|
|
|
toJSON: function () {
|
|
|
|
|
|
|
|
this.world.toJSON();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-19 01:51:14 +00:00
|
|
|
createCollisionGroup: function () {
|
|
|
|
|
|
|
|
var bitmask = Math.pow(2, this._collisionGroupID);
|
|
|
|
|
|
|
|
if (this._wallShapes[0])
|
|
|
|
{
|
|
|
|
this._wallShapes[0].collisionMask = this._wallShapes[0].collisionMask | bitmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._wallShapes[1])
|
|
|
|
{
|
|
|
|
this._wallShapes[1].collisionMask = this._wallShapes[1].collisionMask | bitmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._wallShapes[2])
|
|
|
|
{
|
|
|
|
this._wallShapes[2].collisionMask = this._wallShapes[2].collisionMask | bitmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._wallShapes[3])
|
|
|
|
{
|
|
|
|
this._wallShapes[3].collisionMask = this._wallShapes[3].collisionMask | bitmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._collisionGroupID++;
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
var group = new Phaser.Physics.P2.CollisionGroup(bitmask);
|
2014-02-19 01:51:14 +00:00
|
|
|
|
|
|
|
this.collisionGroups.push(group);
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2.prototype.createBody
|
2014-02-18 15:16:26 +00:00
|
|
|
* @param {number} x - The x coordinate of Body.
|
|
|
|
* @param {number} y - The y coordinate of Body.
|
|
|
|
* @param {number} mass - The mass of the Body. A mass of 0 means a 'static' Body is created.
|
|
|
|
* @param {boolean} [addToWorld=false] - Automatically add this Body to the world? (usually false as it won't have any shapes on construction).
|
|
|
|
* @param {object} options - An object containing the build options:
|
|
|
|
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
|
|
|
|
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
|
|
|
|
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
|
|
|
|
* @param {(number[]|...number)} points - An array of 2d vectors that form the convex or concave polygon.
|
|
|
|
* Either [[0,0], [0,1],...] or a flat array of numbers that will be interpreted as [x,y, x,y, ...],
|
|
|
|
* or the arguments passed can be flat x,y values e.g. `setPolygon(options, x,y, x,y, x,y, ...)` where `x` and `y` are numbers.
|
|
|
|
*/
|
|
|
|
createBody: function (x, y, mass, addToWorld, options, data) {
|
|
|
|
|
|
|
|
if (typeof addToWorld === 'undefined') { addToWorld = false; }
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
var body = new Phaser.Physics.P2.Body(this.game, null, x, y, mass);
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
var result = body.addPolygon(options, data);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addToWorld)
|
|
|
|
{
|
|
|
|
this.world.addBody(body.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @method Phaser.Physics.P2.prototype.createBody
|
2014-02-18 15:16:26 +00:00
|
|
|
* @param {number} x - The x coordinate of Body.
|
|
|
|
* @param {number} y - The y coordinate of Body.
|
|
|
|
* @param {number} mass - The mass of the Body. A mass of 0 means a 'static' Body is created.
|
|
|
|
* @param {boolean} [addToWorld=false] - Automatically add this Body to the world? (usually false as it won't have any shapes on construction).
|
|
|
|
* @param {object} options - An object containing the build options:
|
|
|
|
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
|
|
|
|
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
|
|
|
|
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
|
|
|
|
* @param {(number[]|...number)} points - An array of 2d vectors that form the convex or concave polygon.
|
|
|
|
* Either [[0,0], [0,1],...] or a flat array of numbers that will be interpreted as [x,y, x,y, ...],
|
|
|
|
* or the arguments passed can be flat x,y values e.g. `setPolygon(options, x,y, x,y, x,y, ...)` where `x` and `y` are numbers.
|
|
|
|
*/
|
|
|
|
createParticle: function (x, y, mass, addToWorld, options, data) {
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
if (typeof addToWorld === 'undefined') { addToWorld = false; }
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
var body = new Phaser.Physics.P2.Body(this.game, null, x, y, mass);
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
var result = body.addPolygon(options, data);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addToWorld)
|
|
|
|
{
|
|
|
|
this.world.addBody(body.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
2014-02-10 23:28:32 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#friction
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {number} friction - Friction between colliding bodies. This value is used if no matching ContactMaterial is found for a Material pair.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "friction", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
return this.world.defaultFriction;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.defaultFriction = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-02-15 02:19:37 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#restituion
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {number} restitution - Default coefficient of restitution between colliding bodies. This value is used if no matching ContactMaterial is found for a Material pair.
|
2014-02-15 02:19:37 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "restituion", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
return this.world.defaultRestitution;
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.defaultRestitution = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-02-17 17:54:10 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#applySpringForces
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} applySpringForces - Enable to automatically apply spring forces each step.
|
2014-02-17 17:54:10 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "applySpringForces", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
2014-02-17 17:54:10 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
return this.world.applySpringForces;
|
2014-02-17 17:54:10 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-17 17:54:10 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.applySpringForces = value;
|
2014-02-18 03:01:51 +00:00
|
|
|
|
2014-02-17 17:54:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#applyDamping
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} applyDamping - Enable to automatically apply body damping each step.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "applyDamping", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.world.applyDamping;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.applyDamping = value;
|
|
|
|
|
2014-02-17 17:54:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
});
|
2014-02-17 17:54:10 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#applyGravity
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} applyGravity - Enable to automatically apply gravity each step.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "applyGravity", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.world.applyGravity;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.applyGravity = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-02-18 04:49:03 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#solveConstraints
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} solveConstraints - Enable/disable constraint solving in each step.
|
2014-02-18 04:49:03 +00:00
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "solveConstraints", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
2014-02-18 04:49:03 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
return this.world.solveConstraints;
|
2014-02-18 04:49:03 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
},
|
2014-02-18 04:49:03 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.solveConstraints = value;
|
2014-02-18 04:49:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#time
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} time - The World time.
|
|
|
|
* @readonly
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "time", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.world.time;
|
|
|
|
|
2014-02-18 04:49:03 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
});
|
2014-02-18 04:49:03 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#emitImpactEvent
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} emitImpactEvent - Set to true if you want to the world to emit the "impact" event. Turning this off could improve performance.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "emitImpactEvent", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.world.emitImpactEvent;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.emitImpactEvent = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-03-05 02:36:08 +00:00
|
|
|
* @name Phaser.Physics.P2#enableBodySleeping
|
2014-02-18 15:16:26 +00:00
|
|
|
* @property {boolean} enableBodySleeping - Enable / disable automatic body sleeping.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "enableBodySleeping", {
|
2014-02-18 15:16:26 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.world.enableBodySleeping;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.world.enableBodySleeping = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-03-10 14:47:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.P2#total
|
|
|
|
* @property {number} total - The total number of bodies in the world.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.P2.prototype, "total", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.world.bodies.length;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|