From 08e5f18257a2c7cfdc1cec1a952390bbaecc4af6 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 19 Feb 2014 03:51:48 +0000 Subject: [PATCH] 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. --- examples/wip/contact1.js | 40 +++-------------- examples/wip/platforms.js | 2 +- src/core/Game.js | 15 ++++++- src/geom/Point.js | 2 +- src/input/Key.js | 2 +- src/physics/Body.js | 6 +++ src/physics/World.js | 91 +++++++++++++++++++++++++++++++-------- 7 files changed, 102 insertions(+), 56 deletions(-) diff --git a/examples/wip/contact1.js b/examples/wip/contact1.js index 37e2095a..e0826be5 100644 --- a/examples/wip/contact1.js +++ b/examples/wip/contact1.js @@ -1,5 +1,5 @@ -var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }, false, false, { gravity: [0, 10] } ); function preload() { @@ -23,25 +23,13 @@ function create() { bg = game.add.tileSprite(0, 0, 800, 600, 'background'); bg.fixedToCamera = true; - game.physics.gravity.y = 20; - // game.physics.setBoundsToWorld(true, true, false, true); + // game.physics.gravity.y = 20; - // game.physics.world.gravity[1] = -20; game.physics.friction = 0.5; - // game.physics.world.solver.stiffness = 1e20; - // game.physics.world.solver.relaxation = 3; - - // Materials - // var groundMaterial = game.physics.createMaterial('ground'); - // var characterMaterial = game.physics.createMaterial('character'); - // var boxMaterial = game.physics.createMaterial('box'); player = game.add.sprite(50, 400, 'dude'); player.physicsEnabled = true; player.body.fixedRotation = true; - // player.body.setMaterial(characterMaterial); - // player.body.mass = 1; - // player.body.damping = 0.5; player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('turn', [4], 20, true); @@ -51,27 +39,15 @@ function create() { for (var i = 0; i < 1; i++) { - var box = boxes.create(game.rnd.integerInRange(200, 700), game.rnd.integerInRange(0, 100), 'box'); + // var box = boxes.create(game.rnd.integerInRange(200, 700), game.rnd.integerInRange(0, 100), 'box'); + var box = boxes.create(game.rnd.integerInRange(200, 700), 550, 'box'); // box.scale.set(0.5); // box.scale.set(game.rnd.realInRange(0.2, 0.7)); box.physicsEnabled = true; - // box.body.mass = 10; - // box.body.setMaterial(boxMaterial); + box.body.static = true; box.body.fixedRotation = true; } - // Set the material along the ground - // game.physics.setWorldMaterial(groundMaterial); - - // var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground - // var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes - // var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground - - // console.log(groundCharacterCM); - // console.log(boxGroundCM); - - // game.camera.follow(player); - cursors = game.input.keyboard.createCursorKeys(); jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); @@ -145,10 +121,6 @@ return result; function render () { - // if (player.debug) - // { - game.debug.renderPhysicsBody(player.body); - // game.debug.renderBodyInfo(player, 16, 24); - // } + game.debug.renderPhysicsBody(player.body); } diff --git a/examples/wip/platforms.js b/examples/wip/platforms.js index b4e792d3..f7cf07b5 100644 --- a/examples/wip/platforms.js +++ b/examples/wip/platforms.js @@ -40,7 +40,7 @@ function create() { // game.physics.setBoundsToWorld(); // game.physics.gravity.y = 9.78; - game.physics.setBoundsToWorld(true, true, false, true); + game.physics.setBoundsToWorld(true, true, false, true, false); game.physics.world.gravity[1] = -20; game.physics.friction = 0.5; diff --git a/src/core/Game.js b/src/core/Game.js index 2873fefc..bad3c62d 100644 --- a/src/core/Game.js +++ b/src/core/Game.js @@ -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); diff --git a/src/geom/Point.js b/src/geom/Point.js index c4c291d9..79739189 100644 --- a/src/geom/Point.js +++ b/src/geom/Point.js @@ -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 */ diff --git a/src/input/Key.js b/src/input/Key.js index 853502bd..11419031 100644 --- a/src/input/Key.js +++ b/src/input/Key.js @@ -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. */ diff --git a/src/physics/Body.js b/src/physics/Body.js index 19043272..2e0727ae 100644 --- a/src/physics/Body.js +++ b/src/physics/Body.js @@ -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); } diff --git a/src/physics/World.js b/src/physics/World.js index bd712490..5b8578e2 100644 --- a/src/physics/World.js +++ b/src/physics/World.js @@ -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} 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;