From 58102168aabd1a2256ced04a8da09637b267f1da Mon Sep 17 00:00:00 2001 From: photonstorm Date: Mon, 10 Feb 2014 23:28:32 +0000 Subject: [PATCH] Added setBoundsToWorld and got it building the bounds body + 4 (optional) Planes around the edges for collision. --- examples/wip/p24.js | 16 --------- src/physics/World.js | 77 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/examples/wip/p24.js b/examples/wip/p24.js index 8c08ff2f..ba9c686b 100644 --- a/examples/wip/p24.js +++ b/examples/wip/p24.js @@ -36,22 +36,6 @@ function create() { game.physics.defaultRestitution = 0.8; - var top = new p2.Body({ mass: 0, position:[0, 0], angle: game.math.degToRad(-180) }); - top.addShape(new p2.Plane()); - game.physics.addBody(top); - - var bottom = new p2.Body({ mass: 0, position:[0, px2p(600)] }); - bottom.addShape(new p2.Plane()); - game.physics.addBody(bottom); - - var left = new p2.Body({ mass: 0, position:[0, 0], angle: game.math.degToRad(90) }); - left.addShape(new p2.Plane()); - game.physics.addBody(left); - - var right = new p2.Body({ mass: 0, position:[px2p(800), 0], angle: game.math.degToRad(-90) }); - right.addShape(new p2.Plane()); - game.physics.addBody(right); - } function update() { diff --git a/src/physics/World.js b/src/physics/World.js index 07cc5c7b..f48d8524 100644 --- a/src/physics/World.js +++ b/src/physics/World.js @@ -22,13 +22,90 @@ Phaser.Physics.World = function (game) { */ this.game = game; + this.bounds = null; + p2.World.call(this, { gravity: [0, 0] }); + this.setBoundsToWorld(true, true, true, true); + }; Phaser.Physics.World.prototype = Object.create(p2.World.prototype); Phaser.Physics.World.prototype.constructor = Phaser.Physics.World; +/** +* 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. +*/ +Phaser.Physics.World.prototype.setBoundsToWorld = function (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); + +} + +/** +* 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. +* +* @method Phaser.Physics.Arcade#setBounds +* @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. +*/ +Phaser.Physics.World.prototype.setBounds = function (x, y, width, height, 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 (this.bounds !== null) + { + this.removeBody(this.bounds); + } + + var hw = (width / 2); + var hh = (height / 2); + var cx = hw + x; + var cy = hh + y; + + this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2p(cx), this.game.math.px2p(cy)] }); + + if (left) + { + this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(-hw), 0], 1.5707963267948966 ); + } + + if (right) + { + this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(hw), 0], -1.5707963267948966 ); + } + + if (top) + { + this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(-hh)], -3.141592653589793 ); + } + + if (bottom) + { + this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(hh)] ); + } + + this.addBody(this.bounds); + +} + /** * @method Phaser.Physics.World.prototype.update */