Skip to content

Commit

Permalink
support for debug rendering ninja AABB and circle bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
psalaets committed Jul 6, 2014
1 parent da822ab commit 77b86f4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/physics/ninja/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,56 @@ Object.defineProperty(Phaser.Physics.Ninja.Body.prototype, "angle", {

});

/**
* Render Sprite's Body.
*
* @method Phaser.Physics.Ninja.Body#render
* @param {object} context - The context to render to.
* @param {Phaser.Physics.Ninja.Body} body - The Body to render.
* @param {string} [color='rgba(0,255,0,0.4)'] - color of the debug shape to be rendered. (format is css color string).
* @param {boolean} [filled=true] - Render the shape as a filled (default, true) or a stroked (false)
*/
Phaser.Physics.Ninja.Body.render = function(context, body, color, filled) {
color = color || 'rgba(0,255,0,0.4)';

if (typeof filled === 'undefined')
{
filled = true;
}

if (body.aabb)
{
var left = body.x - (body.width / 2) - body.game.camera.x;
var top = body.y - (body.height / 2) - body.game.camera.y;

if (filled)
{
context.fillStyle = color;
context.fillRect(left, top, body.width, body.height);
}
else
{
context.strokeStyle = color;
context.strokeRect(left, top, body.width, body.height);
}
}
else if (body.circle)
{
var x = body.x - body.game.camera.x;
var y = body.y - body.game.camera.y;

context.beginPath();
context.arc(x, y, body.circle.radius, 0, 2 * Math.PI, false);

if (filled)
{
context.fillStyle = color;
context.fill();
}
else
{
context.strokeStyle = color;
context.stroke();
}
}
};
9 changes: 8 additions & 1 deletion src/utils/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ Phaser.Utils.Debug.prototype = {
},

/**
* Render a Sprites Physics body if it has one set. Note this only works for Arcade Physics.
* Render a Sprites Physics body if it has one set. Note this only works for Arcade and
* Ninja (AABB, circle only) Physics.
* To display a P2 body you should enable debug mode on the body when creating it.
*
* @method Phaser.Utils.Debug#body
Expand All @@ -706,6 +707,12 @@ Phaser.Utils.Debug.prototype = {
Phaser.Physics.Arcade.Body.render(this.context, sprite.body, color, filled);
this.stop();
}
else if (sprite.body.type === Phaser.Physics.NINJA)
{
this.start();
Phaser.Physics.Ninja.Body.render(this.context, sprite.body, color, filled);
this.stop();
}
}

},
Expand Down

0 comments on commit 77b86f4

Please sign in to comment.