Skip to content

Commit 7aaa63e

Browse files
committed
When you change State the P2 Physics world is no longer fully cleared. All of the bodies, springs, fixtures, materials and constraints are removed - but config settings such as gravity, restitution, the contact solver, etc are all retained. The P2.World object is only created the very first time you call Physics.startSystem. Every subsequent call hits P2.World.reset instead (phaserjs#1292)
1 parent 0d9678e commit 7aaa63e

3 files changed

Lines changed: 107 additions & 17 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ This fixes a bug in FF where it would use the default DOMMouseWheel (thanks @pns
248248
* Camera.totalInView is a new property that contains the total number of Sprites rendered that have `autoCull` set to true and are within the Cameras view.
249249
* Emitter.setScale fixed minX minY order presedence (thanks spayton)
250250
* Group.iterate can now accept undefined/null as the arguments (thanks @pnstickne #1353 @tasos-ch #1352)
251+
* When you change State the P2 Physics world is no longer fully cleared. All of the bodies, springs, fixtures, materials and constraints are removed - but config settings such as gravity, restitution, the contact solver, etc are all retained. The P2.World object is only created the very first time you call Physics.startSystem. Every subsequent call hits P2.World.reset instead (#1292)
251252

252253
### Pixi 2.1.0 New Features
253254

src/physics/Physics.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
/**
88
* The Physics Manager is responsible for looking after all of the running physics systems.
9-
* Phaser supports 3 physics systems: Arcade Physics, P2 and Ninja Physics (with Box2D and Chipmunk in development)
10-
* Game Objects can belong to only 1 physics system, but you can have multiple systems active in a single game.
9+
* Phaser supports 4 physics systems: Arcade Physics, P2, Ninja Physics and Box2D via a commercial plugin.
10+
*
11+
* Game Objects (such as Sprites) can only belong to 1 physics system, but you can have multiple systems active in a single game.
1112
*
1213
* For example you could have P2 managing a polygon-built terrain landscape that an vehicle drives over, while it could be firing bullets that use the
1314
* faster (due to being much simpler) Arcade Physics system.
@@ -42,12 +43,12 @@ Phaser.Physics = function (game, config) {
4243
this.p2 = null;
4344

4445
/**
45-
* @property {Phaser.Physics.Ninja} ninja - The N+ Ninja Physics System.
46+
* @property {Phaser.Physics.Ninja} ninja - The N+ Ninja Physics system.
4647
*/
4748
this.ninja = null;
4849

4950
/**
50-
* @property {Phaser.Physics.Box2D} box2d - The Box2D Physics system (to be done).
51+
* @property {Phaser.Physics.Box2D} box2d - The Box2D Physics system.
5152
*/
5253
this.box2d = null;
5354

@@ -126,13 +127,22 @@ Phaser.Physics.prototype = {
126127
/**
127128
* This will create an instance of the requested physics simulation.
128129
* Phaser.Physics.Arcade is running by default, but all others need activating directly.
130+
*
129131
* You can start the following physics systems:
132+
*
130133
* Phaser.Physics.P2JS - A full-body advanced physics system by Stefan Hedman.
131134
* Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system.
132-
* Phaser.Physics.BOX2D and Phaser.Physics.CHIPMUNK are still in development.
135+
* Phaser.Physics.BOX2D - A commercial Phaser Plugin (see http://phaser.io)
136+
*
137+
* Both Ninja Physics and Box2D require their respective plugins to be loaded before you can start them.
138+
* They are not bundled into the core Phaser library.
139+
*
140+
* If the physics world has already been created (i.e. in another state in your game) then
141+
* calling startSystem will reset the physics world, not re-create it. If you need to start them again from their constructors
142+
* then set Phaser.Physics.p2 (or whichever system you want to recreate) to `null` before calling `startSystem`.
133143
*
134144
* @method Phaser.Physics#startSystem
135-
* @param {number} The physics system to start.
145+
* @param {number} system - The physics system to start: Phaser.Physics.ARCADE, Phaser.Physics.P2JS, Phaser.Physics.NINJA or Phaser.Physics.BOX2D.
136146
*/
137147
startSystem: function (system) {
138148

@@ -142,19 +152,29 @@ Phaser.Physics.prototype = {
142152
}
143153
else if (system === Phaser.Physics.P2JS)
144154
{
145-
this.p2 = new Phaser.Physics.P2(this.game, this.config);
155+
if (this.p2 === null)
156+
{
157+
this.p2 = new Phaser.Physics.P2(this.game, this.config);
158+
}
159+
else
160+
{
161+
this.p2.reset();
162+
}
146163
}
147-
if (system === Phaser.Physics.NINJA)
164+
else if (system === Phaser.Physics.NINJA)
148165
{
149166
this.ninja = new Phaser.Physics.Ninja(this.game);
150167
}
151-
else if (system === Phaser.Physics.BOX2D && this.box2d === null)
152-
{
153-
this.box2d = new Phaser.Physics.Box2D(this.game, this.config);
154-
}
155-
else if (system === Phaser.Physics.CHIPMUNK && this.chipmunk === null)
168+
else if (system === Phaser.Physics.BOX2D)
156169
{
157-
throw new Error('The Chipmunk physics system has not been implemented yet.');
170+
if (this.box2d === null)
171+
{
172+
this.box2d = new Phaser.Physics.Box2D(this.game, this.config);
173+
}
174+
else
175+
{
176+
this.box2d.reset();
177+
}
158178
}
159179

160180
},

src/physics/p2/World.js

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ Phaser.Physics.P2.prototype = {
671671
*/
672672
update: function () {
673673

674-
// Do nothing when the pysics engine was paused before
674+
// Do nothing if the physics engine was paused before
675675
if (this.paused)
676676
{
677677
return;
@@ -688,14 +688,84 @@ Phaser.Physics.P2.prototype = {
688688

689689
},
690690

691+
/**
692+
* Called by Phaser.Physics when a State swap occurs.
693+
* Starts the begin and end Contact listeners again.
694+
*
695+
* @method Phaser.Physics.P2#reset
696+
*/
697+
reset: function () {
698+
699+
this.world.on("beginContact", this.beginContactHandler, this);
700+
this.world.on("endContact", this.endContactHandler, this);
701+
702+
this.nothingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(1);
703+
this.boundsCollisionGroup = new Phaser.Physics.P2.CollisionGroup(2);
704+
this.everythingCollisionGroup = new Phaser.Physics.P2.CollisionGroup(2147483648);
705+
706+
this._collisionGroupID = 2;
707+
708+
this.setBoundsToWorld(true, true, true, true, false);
709+
710+
},
711+
691712
/**
692713
* Clears all bodies from the simulation, resets callbacks and resets the collision bitmask.
714+
*
715+
* The P2 world is also cleared:
716+
*
717+
* * Removes all solver equations
718+
* * Removes all constraints
719+
* * Removes all bodies
720+
* * Removes all springs
721+
* * Removes all contact materials
722+
*
723+
* This is called automatically when you switch state.
693724
*
694725
* @method Phaser.Physics.P2#clear
695726
*/
696727
clear: function () {
697728

698-
this.world.clear();
729+
this.world.time = 0;
730+
this.world.fixedStepTime = 0;
731+
732+
// Remove all solver equations
733+
if (this.world.solver && this.world.solver.equations.length)
734+
{
735+
this.world.solver.removeAllEquations();
736+
}
737+
738+
// Remove all constraints
739+
var cs = this.world.constraints;
740+
741+
for (var i = cs.length - 1; i >= 0; i--)
742+
{
743+
this.world.removeConstraint(cs[i]);
744+
}
745+
746+
// Remove all bodies
747+
var bodies = this.world.bodies;
748+
749+
for (var i = bodies.length - 1; i >= 0; i--)
750+
{
751+
this.world.removeBody(bodies[i]);
752+
}
753+
754+
// Remove all springs
755+
var springs = this.world.springs;
756+
757+
for (var i = springs.length - 1; i >= 0; i--)
758+
{
759+
this.world.removeSpring(springs[i]);
760+
}
761+
762+
// Remove all contact materials
763+
var cms = this.world.contactMaterials;
764+
765+
for (var i = cms.length - 1; i >= 0; i--)
766+
{
767+
this.world.removeContactMaterial(cms[i]);
768+
}
699769

700770
this.world.off("beginContact", this.beginContactHandler, this);
701771
this.world.off("endContact", this.endContactHandler, this);
@@ -706,7 +776,6 @@ Phaser.Physics.P2.prototype = {
706776

707777
this.collisionGroups = [];
708778
this._toRemove = [];
709-
this._collisionGroupID = 2;
710779
this.boundsCollidesWith = [];
711780

712781
},

0 commit comments

Comments
 (0)