Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug rendering for Ninja aabb and circle #972

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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