Skip to content

Commit

Permalink
Merge pull request #972 from psalaets/debug-rendering-ninja-aabb-and-…
Browse files Browse the repository at this point in the history
…circle

Debug rendering for Ninja aabb and circle
  • Loading branch information
photonstorm committed Jul 7, 2014
2 parents 730a8eb + cbf0a90 commit e9886ad
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/physics/ninja/AABB.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,31 @@ Phaser.Physics.Ninja.AABB.prototype = {
destroy: function() {
this.body = null;
this.system = null;
}
},

/**
* Render this AABB for debugging purposes.
*
* @method Phaser.Physics.Ninja.AABB#render
* @param {object} context - The context to render to.
* @param {number} xOffset - X offset from AABB's position to render at.
* @param {number} yOffset - Y offset from AABB's position to render at.
* @param {string} color - color of the debug shape to be rendered. (format is css color string).
* @param {boolean} filled - Render the shape as solid (true) or hollow (false).
*/
render: function(context, xOffset, yOffset, color, filled) {
var left = this.pos.x - this.xw - xOffset;
var top = this.pos.y - this.yw - yOffset;

if (filled)
{
context.fillStyle = color;
context.fillRect(left, top, this.width, this.height);
}
else
{
context.strokeStyle = color;
context.strokeRect(left, top, this.width, this.height);
}
}
};
22 changes: 22 additions & 0 deletions src/physics/ninja/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,25 @@ 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 || body.circle)
{
body.shape.render(context, body.game.camera.x, body.game.camera.y, color, filled);
}
};
30 changes: 29 additions & 1 deletion src/physics/ninja/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,34 @@ Phaser.Physics.Ninja.Circle.prototype = {
destroy: function() {
this.body = null;
this.system = null;
}
},

/**
* Render this circle for debugging purposes.
*
* @method Phaser.Physics.Ninja.Circle#render
* @param {object} context - The context to render to.
* @param {number} xOffset - X offset from circle's position to render at.
* @param {number} yOffset - Y offset from circle's position to render at.
* @param {string} color - color of the debug shape to be rendered. (format is css color string).
* @param {boolean} filled - Render the shape as solid (true) or hollow (false).
*/
render: function(context, xOffset, yOffset, color, filled) {
var x = this.pos.x - xOffset;
var y = this.pos.y - yOffset;

context.beginPath();
context.arc(x, y, this.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 e9886ad

Please sign in to comment.