Skip to content

Commit b0e96a3

Browse files
committed
Added Box2D handler support.
1 parent c629a68 commit b0e96a3

3 files changed

Lines changed: 104 additions & 4 deletions

File tree

build/config.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
$p2 = true;
1010
}
1111

12+
if (!isset($box2d))
13+
{
14+
$box2d = false;
15+
}
16+
1217
if (!isset($ninja))
1318
{
14-
$ninja = true;
19+
$ninja = false;
1520
}
1621

1722
if (!isset($arcade))
@@ -24,6 +29,11 @@
2429
echo " <script src=\"$path/src/physics/p2/p2.js\"></script>";
2530
}
2631

32+
if ($box2d)
33+
{
34+
echo " <script src=\"$path/src/physics/box2d/box2d-html5.js\"></script>";
35+
}
36+
2737
echo <<<EOL
2838
2939
<script src="$path/src/pixi/Pixi.js"></script>
@@ -220,6 +230,19 @@
220230
EOL;
221231
}
222232

233+
if ($box2d)
234+
{
235+
echo <<<EOL
236+
237+
<script src="$path/src/physics/box2d/World.js"></script>
238+
<script src="$path/src/physics/box2d/Body.js"></script>
239+
<script src="$path/src/physics/box2d/PointProxy.js"></script>
240+
<script src="$path/src/physics/box2d/DefaultDebugDraw.js"></script>
241+
<script src="$path/src/physics/box2d/DefaultContactListener.js"></script>
242+
<script src="$path/src/physics/box2d/Polygon.js"></script>
243+
EOL;
244+
}
245+
223246
if (isset($custom))
224247
{
225248
for ($i = 0; $i < count($custom); $i++)

src/physics/Physics.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Phaser.Physics.BOX2D = 3;
8888
* @const
8989
* @type {number}
9090
*/
91-
Phaser.Physics.CHIPMUNK = 5;
91+
Phaser.Physics.CHIPMUNK = 4;
9292

9393
Phaser.Physics.prototype = {
9494

@@ -116,6 +116,11 @@ Phaser.Physics.prototype = {
116116
this.p2 = new Phaser.Physics.P2(this.game, this.config);
117117
}
118118

119+
if (this.config.hasOwnProperty('box2d') && this.config['box2d'] === true && Phaser.Physics.hasOwnProperty('BOX2D'))
120+
{
121+
this.box2d = new Phaser.Physics.BOX2D(this.game, this.config);
122+
}
123+
119124
},
120125

121126
/**
@@ -145,7 +150,7 @@ Phaser.Physics.prototype = {
145150
}
146151
else if (system === Phaser.Physics.BOX2D && this.box2d === null)
147152
{
148-
throw new Error('The Box2D physics system has not been implemented yet.');
153+
this.box2d = new Phaser.Physics.Box2D(this.game, this.config);
149154
}
150155
else if (system === Phaser.Physics.CHIPMUNK && this.chipmunk === null)
151156
{
@@ -162,7 +167,8 @@ Phaser.Physics.prototype = {
162167
* Phaser.Physics.Arcade - A light weight AABB based collision system with basic separation.
163168
* Phaser.Physics.P2JS - A full-body advanced physics system supporting multiple object shapes, polygon loading, contact materials, springs and constraints.
164169
* Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system. Advanced AABB and Circle vs. Tile collision.
165-
* Phaser.Physics.BOX2D and Phaser.Physics.CHIPMUNK are still in development.
170+
* Phaser.Physics.BOX2D - A port of https://code.google.com/p/box2d-html5
171+
* Phaser.Physics.CHIPMUNK is still in development.
166172
*
167173
* If you require more control over what type of body is created, for example to create a Ninja Physics Circle instead of the default AABB, then see the
168174
* individual physics systems `enable` methods instead of using this generic one.
@@ -189,6 +195,10 @@ Phaser.Physics.prototype = {
189195
{
190196
this.ninja.enableAABB(object);
191197
}
198+
else if (system === Phaser.Physics.BOX2D && this.box2d)
199+
{
200+
this.box2d.enable(object);
201+
}
192202

193203
},
194204

@@ -207,6 +217,11 @@ Phaser.Physics.prototype = {
207217
this.p2.preUpdate();
208218
}
209219

220+
if (this.box2d)
221+
{
222+
this.box2d.preUpdate();
223+
}
224+
210225
},
211226

212227
/**
@@ -224,6 +239,11 @@ Phaser.Physics.prototype = {
224239
this.p2.update();
225240
}
226241

242+
if (this.box2d)
243+
{
244+
this.box2d.update();
245+
}
246+
227247
},
228248

229249
/**
@@ -249,6 +269,11 @@ Phaser.Physics.prototype = {
249269
this.p2.setBoundsToWorld();
250270
}
251271

272+
if (this.box2d)
273+
{
274+
//this.box2d.setBoundsToWorld(); TODO
275+
}
276+
252277
},
253278

254279
/**
@@ -264,6 +289,11 @@ Phaser.Physics.prototype = {
264289
this.p2.clear();
265290
}
266291

292+
if (this.box2d)
293+
{
294+
this.box2d.clear();
295+
}
296+
267297
},
268298

269299
/**
@@ -278,9 +308,15 @@ Phaser.Physics.prototype = {
278308
this.p2.destroy();
279309
}
280310

311+
if (this.box2d)
312+
{
313+
this.box2d.destroy();
314+
}
315+
281316
this.arcade = null;
282317
this.ninja = null;
283318
this.p2 = null;
319+
this.box2d = null;
284320

285321
}
286322

src/utils/Debug.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,49 @@ Phaser.Utils.Debug.prototype = {
775775
Phaser.Physics.Arcade.Body.renderBodyInfo(this, sprite.body);
776776
this.stop();
777777
}
778+
else if (sprite.body.type === Phaser.Physics.BOX2D)
779+
{
780+
this.start(x, y, color, 210);
781+
this.game.physics.box2d.renderBodyInfo(this, sprite.body);
782+
this.stop();
783+
}
778784
}
779785

786+
},
787+
788+
/**
789+
* Renders 'debug draw' data for the Box2D world if it exists.
790+
* This uses the standard debug drawing feature of Box2D, so colors will be decided by
791+
* the Box2D engine.
792+
*
793+
* @method Phaser.Utils.Debug#box2dWorld
794+
*/
795+
box2dWorld: function () {
796+
797+
this.start();
798+
799+
this.context.translate(-this.game.camera.view.x, -this.game.camera.view.y, 0);
800+
this.game.physics.box2d.renderDebugDraw(this.context);
801+
802+
this.stop();
803+
804+
},
805+
806+
/**
807+
* Renders 'debug draw' data for the given Box2D body.
808+
* This uses the standard debug drawing feature of Box2D, so colors will be decided by
809+
* the Box2D engine.
810+
*
811+
* @method Phaser.Utils.Debug#box2dBody
812+
* @param {Phaser.Sprite} sprite - The sprite whos body will be rendered.
813+
* @param {string} [color='rgb(0,255,0)'] - color of the debug info to be rendered. (format is css color string).
814+
*/
815+
box2dBody: function (body, color) {
816+
817+
this.start();
818+
Phaser.Physics.Box2D.renderBody(this.context, body, color);
819+
this.stop();
820+
780821
}
781822

782823
};

0 commit comments

Comments
 (0)