Skip to content

Commit f60b2b7

Browse files
committed
World.setBounds if called after you had already started P2 Physics would incorrectly create a new collision group for the wall objects. P2.World now remembers the settings you provide for each wall and the collision group, and re-applies these settings should the world dimensions ever change (thanks @nextht phaserjs#1455)
1 parent b692d3b commit f60b2b7

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ Thanks to @pnstickne for vast majority of this update.
152152
* Timer.update was calling the TimerEvent callback even if `TimerEvent.pendingDelete` was already set to `true`, causing timer events to stack-up in cases where a new TimerEvent was generated in the callback (thanks @clowerweb #838)
153153
* Pointer.stop would call `event.preventDefault` if `Pointer._stateReset` was `true`, which is always `true` after a State has changed and before Pointer.start has been called. However this broken interacting with DOM elements in the case where the State changes and you immediately try to use the DOM element without first having clicked on the Phaser game. An additional guard was added so `preventDefault` will now only be called if both `_stateReste` and `Pointer.withinGame` are true (thanks @satan6 #1509)
154154
* Group.forEach (and many other Group methods) now uses the `children.length` value directly instead of caching it, which both helps performance and stops the loop from breaking should you remove a Group child in the invoked callback.
155-
* Phaser.Ellipse.contains is now working again (thanks @spayton)
155+
* Phaser.Ellipse.contains is now working again (thanks @spayton #1524)
156156
* PIXI.WebGLRenderer.destroy has been fixed to decrement the `glContextId` and remove it from the PIXI.instances global. `Game.destroy` now hooks into this. This now means that you can now delete and create your Phaser game over and over without it crashing WebGL after the 4th attempt (#1260)
157+
* World.setBounds if called after you had already started P2 Physics would incorrectly create a new collision group for the wall objects. P2.World now remembers the settings you provide for each wall and the collision group, and re-applies these settings should the world dimensions ever change (thanks @nextht #1455)
157158

158159
For changes in previous releases please see the extensive [Version History](https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md).
159160

src/physics/p2/World.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,36 @@ Phaser.Physics.P2 = function (game, config) {
197197
*/
198198
this._collisionGroupID = 2;
199199

200+
/**
201+
* @property {boolean} _boundsLeft - Internal var that keeps track of world bounds settings.
202+
* @private
203+
*/
204+
this._boundsLeft = true;
205+
206+
/**
207+
* @property {boolean} _boundsRight - Internal var that keeps track of world bounds settings.
208+
* @private
209+
*/
210+
this._boundsRight = true;
211+
212+
/**
213+
* @property {boolean} _boundsTop - Internal var that keeps track of world bounds settings.
214+
* @private
215+
*/
216+
this._boundsTop = true;
217+
218+
/**
219+
* @property {boolean} _boundsBottom - Internal var that keeps track of world bounds settings.
220+
* @private
221+
*/
222+
this._boundsBottom = true;
223+
224+
/**
225+
* @property {boolean} _boundsOwnGroup - Internal var that keeps track of world bounds settings.
226+
* @private
227+
*/
228+
this._boundsOwnGroup = false;
229+
200230
// By default we want everything colliding with everything
201231
this.setBoundsToWorld(true, true, true, true, false);
202232

@@ -560,6 +590,10 @@ Phaser.Physics.P2.prototype = {
560590
/**
561591
* Sets the bounds of the Physics world to match the given world pixel dimensions.
562592
* You can optionally set which 'walls' to create: left, right, top or bottom.
593+
* If none of the walls are given it will default to use the walls settings it had previously.
594+
* I.e. if you previously told it to not have the left or right walls, and you then adjust the world size
595+
* the newly created bounds will also not have the left and right walls.
596+
* Explicitly state them in the parameters to override this.
563597
*
564598
* @method Phaser.Physics.P2#setBounds
565599
* @param {number} x - The x coordinate of the top-left corner of the bounds.
@@ -574,11 +608,11 @@ Phaser.Physics.P2.prototype = {
574608
*/
575609
setBounds: function (x, y, width, height, left, right, top, bottom, setCollisionGroup) {
576610

577-
if (typeof left === 'undefined') { left = true; }
578-
if (typeof right === 'undefined') { right = true; }
579-
if (typeof top === 'undefined') { top = true; }
580-
if (typeof bottom === 'undefined') { bottom = true; }
581-
if (typeof setCollisionGroup === 'undefined') { setCollisionGroup = true; }
611+
if (typeof left === 'undefined') { left = this._boundsLeft; }
612+
if (typeof right === 'undefined') { right = this._boundsRight; }
613+
if (typeof top === 'undefined') { top = this._boundsTop; }
614+
if (typeof bottom === 'undefined') { bottom = this._boundsBottom; }
615+
if (typeof setCollisionGroup === 'undefined') { setCollisionGroup = this._boundsOwnGroup; }
582616

583617
if (this.walls.left)
584618
{
@@ -652,6 +686,13 @@ Phaser.Physics.P2.prototype = {
652686
this.world.addBody(this.walls.bottom);
653687
}
654688

689+
// Remember the bounds settings in case they change later on via World.setBounds
690+
this._boundsLeft = left;
691+
this._boundsRight = right;
692+
this._boundsTop = top;
693+
this._boundsBottom = bottom;
694+
this._boundsOwnGroup = setCollisionGroup;
695+
655696
},
656697

657698
/**

0 commit comments

Comments
 (0)