Fixed some doc typos.

You can now pass a physicsConfig object with the game constructor that is given to p2.World, allowing you to set the broadphase, etc.
This commit is contained in:
photonstorm
2014-02-19 03:51:48 +00:00
parent d057a9fe11
commit 08e5f18257
7 changed files with 102 additions and 56 deletions
+13 -2
View File
@@ -21,8 +21,9 @@
* @param {object} [state=null] - The default state object. A object consisting of Phaser.State functions (preload, create, update, render) or null.
* @param {boolean} [transparent=false] - Use a transparent canvas background or not.
* @param {boolean} [antialias=true] - Anti-alias graphics.
* @param {object} [physicsConfig=null] - A physics configuration object to pass to the Physics world on creation.
*/
Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias) {
Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias, physicsConfig) {
/**
* @property {number} id - Phaser Game ID (for when Pixi supports multiple instances).
@@ -34,6 +35,11 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
*/
this.config = null;
/**
* @property {object} physicsConfig - The Phaser.Physics.World configuration object.
*/
this.physicsConfig = physicsConfig;
/**
* @property {HTMLElement} parent - The Games DOM parent.
* @default
@@ -339,6 +345,11 @@ Phaser.Game.prototype = {
this.antialias = config['antialias'];
}
if (config['physicsConfig'])
{
this.physicsConfig = config['physicsConfig'];
}
var state = null;
if (config['state'])
@@ -435,7 +446,7 @@ Phaser.Game.prototype = {
this.tweens = new Phaser.TweenManager(this);
this.input = new Phaser.Input(this);
this.sound = new Phaser.SoundManager(this);
this.physics = new Phaser.Physics.World(this);
this.physics = new Phaser.Physics.World(this, this.physicsConfig);
this.particles = new Phaser.Particles(this);
this.plugins = new Phaser.PluginManager(this, this);
this.net = new Phaser.Net(this);
+1 -1
View File
@@ -267,7 +267,7 @@ Phaser.Point.prototype = {
/**
* Alters the length of the vector without changing the direction
* @method Phaser.Point#getMagnitude
* @method Phaser.Point#setMagnitude
* @param {number} magnitude the desired magnitude of the resulting vector
* @return {Phaser.Point} the modified original vector
*/
+1 -1
View File
@@ -156,7 +156,7 @@ Phaser.Key.prototype = {
/**
* Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given (default 250ms)
* @method Phaser.Key#justPressed
* @method Phaser.Key#justReleased
* @param {number} [duration=250] - The duration below which the key is considered as being just released.
* @return {boolean} True if the key is just released otherwise false.
*/
+6
View File
@@ -76,6 +76,8 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
*/
this.collidesWith = [];
// this.onAdded = new Phaser.Signal();
// this.onRemoved = new Phaser.Signal();
@@ -83,6 +85,10 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
if (sprite)
{
this.setRectangleFromSprite(sprite);
// Default collision mask
// this.data.shapes[0].collisionMask = this.game.physics.boundsCollisionGroup.mask;
this.game.physics.addBody(this);
}
+74 -17
View File
@@ -23,20 +23,26 @@ p2.Spring.prototype.parent = null;
* @class Phaser.Physics.World
* @classdesc Physics World Constructor
* @constructor
* @param {Phaser.Game} game reference to the current game instance.
* @param {Phaser.Game} game - Reference to the current game instance.
* @param {object} [config] - Physics configuration object passed in from the game constructor.
*/
Phaser.Physics.World = function (game) {
Phaser.Physics.World = function (game, config) {
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
if (typeof config === 'undefined')
{
config = { gravity: [0, 0], broadphase: new p2.SAPBroadphase() };
}
/**
* @property {p2.World} game - The p2 World in which the simulation is run.
* @protected
*/
this.world = new p2.World( { gravity: [0, 0] });
this.world = new p2.World(config);
/**
* @property {array<Phaser.Physics.Material>} materials - A local array of all created Materials.
@@ -146,7 +152,7 @@ Phaser.Physics.World = function (game) {
this.boundsCollisionGroup = new Phaser.Physics.CollisionGroup(2);
this.boundsCollidesWith = [];
this.setBoundsToWorld(true, true, true, true);
this.setBoundsToWorld(true, true, true, true, false);
};
@@ -161,6 +167,7 @@ Phaser.Physics.World.prototype = {
*/
postStepHandler: function (event) {
// console.log('postStep', event);
},
@@ -176,12 +183,14 @@ Phaser.Physics.World.prototype = {
// Body.id 1 is always the World bounds object
for (var i = 0; i < event.pairs.length; i++)
for (var i = 0; i < event.pairs.length; i += 2)
{
// console.log(i, event.pairs[i]);
var a = event.pairs[i];
var b = event.pairs[i+1];
if (event.pairs[i].parent)
if (a.id !== 1 && b.id !== 1)
{
// console.log('postBroadphaseHandler', a, b);
}
}
@@ -196,6 +205,15 @@ Phaser.Physics.World.prototype = {
*/
impactHandler: function (event) {
if (event.bodyA.id > 1 && event.bodyB.id > 1)
{
console.log('impactHandler');
console.log(event.bodyA.parent.sprite.key);
console.log(event.bodyB.parent.sprite.key);
}
event = {};
},
/**
@@ -207,6 +225,15 @@ Phaser.Physics.World.prototype = {
*/
beginContactHandler: function (event) {
if (event.bodyA.id > 1 && event.bodyB.id > 1)
{
console.log('beginContactHandler');
console.log(event.bodyA.parent.sprite.key);
console.log(event.bodyB.parent.sprite.key);
}
event = {};
},
/**
@@ -218,6 +245,16 @@ Phaser.Physics.World.prototype = {
*/
endContactHandler: function (event) {
console.log('endContactHandler');
console.log(event);
if (event.bodyA.id > 1 && event.bodyB.id > 1)
{
console.log('endContactHandler');
console.log(event);
}
event = {};
},
/**
@@ -229,10 +266,11 @@ Phaser.Physics.World.prototype = {
* @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.
* @param {boolean} [setCollisionGroup=true] - If true the Bounds will be set to use its own Collision Group.
*/
setBoundsToWorld: function (left, right, top, bottom) {
setBoundsToWorld: function (left, right, top, bottom, setCollisionGroup) {
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);
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);
},
@@ -288,13 +326,15 @@ Phaser.Physics.World.prototype = {
* @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.
* @param {boolean} [setCollisionGroup=true] - If true the Bounds will be set to use its own Collision Group.
*/
setBounds: function (x, y, width, height, left, right, top, bottom) {
setBounds: function (x, y, width, height, left, right, top, bottom, setCollisionGroup) {
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 (typeof setCollisionGroup === 'undefined') { setCollisionGroup = true; }
var hw = (width / 2);
var hh = (height / 2);
@@ -324,28 +364,48 @@ Phaser.Physics.World.prototype = {
if (left)
{
this._wallShapes[0] = new p2.Plane();
this._wallShapes[0].collisionGroup = this.boundsCollisionGroup.mask;
if (setCollisionGroup)
{
this._wallShapes[0].collisionGroup = this.boundsCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
}
if (right)
{
this._wallShapes[1] = new p2.Plane();
this._wallShapes[1].collisionGroup = this.boundsCollisionGroup.mask;
if (setCollisionGroup)
{
this._wallShapes[1].collisionGroup = this.boundsCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2p(hw), 0], -1.5707963267948966 );
}
if (top)
{
this._wallShapes[2] = new p2.Plane();
this._wallShapes[2].collisionGroup = this.boundsCollisionGroup.mask;
if (setCollisionGroup)
{
this._wallShapes[2].collisionGroup = this.boundsCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2p(-hh)], -3.141592653589793 );
}
if (bottom)
{
this._wallShapes[3] = new p2.Plane();
this._wallShapes[3].collisionGroup = this.boundsCollisionGroup.mask;
if (setCollisionGroup)
{
this._wallShapes[3].collisionGroup = this.boundsCollisionGroup.mask;
}
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2p(hh)] );
}
@@ -684,9 +744,6 @@ Phaser.Physics.World.prototype = {
var bitmask = Math.pow(2, this._collisionGroupID);
// Add it to the bounds mask automatically (they won't collide with it unless it's added back by the other group)
// this.boundsCollisionGroup.mask = this.boundsCollisionGroup.mask | bitmask;
if (this._wallShapes[0])
{
this._wallShapes[0].collisionMask = this._wallShapes[0].collisionMask | bitmask;