Skip to content

Commit 8fcf5ee

Browse files
committed
Debug rendering of physics bodies.
1 parent 1cb40b6 commit 8fcf5ee

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

examples/wip/p27.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,29 @@ function create() {
2323

2424
box.physicsEnabled = true;
2525

26-
// box.body.rotateLeft(10);
26+
box.body.rotateLeft(20);
2727

2828
// 95x95
2929
// box.body.setRectangle(64, 64);
3030

3131
// box.body.setRectangle(64, 64, 95/2,95/2);
3232

3333
// Works
34-
// box.body.setPolygon({}, [ [-1, 1], [-1, 0], [1, 0], [1, 1], [0.5, 0.5] ]);
34+
box.body.clearShapes();
35+
// box.body.addPolygon({}, [ [-1, 1], [-1, 0], [1, 0], [1, 1], [0.5, 0.5] ]);
3536

3637
// Works
3738
// box.body.setPolygon({}, [-1, 1], [-1, 0], [1, 0], [1, 1], [0.5, 0.5]);
3839

3940
// Works
40-
// box.body.setPolygon({}, -100, 100, -100, 0, 100, 0, 100, 100, 50, 50);
41+
box.body.addPolygon({}, -100, 100, -100, 0, 100, 0, 100, 100, 50, 50);
4142

4243
// Works
4344
// box.body.setPolygon({}, -1, 1, -1, 0, 1, 0, 1, 1, 0.5, 0.5);
4445

45-
box.body.setZeroDamping();
46+
// box.body.setZeroDamping();
4647

47-
game.input.onDown.addOnce(startTiming, this);
48+
// game.input.onDown.addOnce(startTiming, this);
4849

4950
}
5051

@@ -71,7 +72,7 @@ function update() {
7172
}
7273
else
7374
{
74-
box.body.setZeroVelocity();
75+
// box.body.setZeroVelocity();
7576
}
7677

7778
}
@@ -80,7 +81,7 @@ function render() {
8081

8182
game.debug.renderShape(box.body, 0);
8283

83-
// game.debug.renderText('x: ' + box.body.velocity.x, 32, 32);
84+
// game.debug.renderText(box.body., 32, 32);
8485
// game.debug.renderText('y: ' + box.body.velocity.y, 32, 64);
8586

8687
}

src/utils/Debug.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,8 @@ Phaser.Utils.Debug.prototype = {
894894
},
895895

896896
/**
897-
* @method Phaser.Utils.Debug#renderVertices
898-
* @param {array} vertices
897+
* @method Phaser.Utils.Debug#renderShape
898+
* @param {array} body
899899
* @param {string} [color='rgb(255,255,255)'] - The color the polygon is stroked in.
900900
*/
901901
renderShape: function (body, id, color, context) {
@@ -912,22 +912,44 @@ Phaser.Utils.Debug.prototype = {
912912
var x = body.sprite.x;
913913
var y = body.sprite.y;
914914

915+
var shape = body.data.shapes[id];
916+
var w = this.game.math.p2px(shape.width);
917+
var h = this.game.math.p2px(shape.height);
915918
var points = body.data.shapes[id].vertices;
916919

917920
var ox = x + this.game.math.p2px(body.data.shapeOffsets[id][0]);
918921
var oy = y + this.game.math.p2px(body.data.shapeOffsets[id][1]);
919922

923+
// function drawbox(){
924+
// ctx.beginPath();
925+
// var x = boxBody.position[0],
926+
// y = boxBody.position[1];
927+
// ctx.save();
928+
// ctx.translate(x, y); // Translate to the center of the box
929+
// ctx.rotate(boxBody.angle); // Rotate to the box body frame
930+
// ctx.rect(-boxShape.width/2, -boxShape.height/2, boxShape.width, boxShape.height);
931+
// ctx.stroke();
932+
// ctx.restore();
933+
// }
934+
920935
this.context.beginPath();
921-
this.context.moveTo(ox + this.game.math.p2px(points[0][0]), oy + this.game.math.p2px(points[0][1]));
936+
this.context.save();
937+
this.context.translate(this.game.math.p2px(body.data.position[0]), this.game.math.p2px(body.data.position[1]));
938+
this.context.rotate(body.data.angle);
939+
940+
// this.context.strokeStyle = color;
941+
// this.context.strokeRect(-w / 2, -h / 2, w, h);
942+
943+
this.context.moveTo(this.game.math.p2px(points[0][0]), this.game.math.p2px(points[0][1]));
922944

923945
for (var i = 1; i < points.length; i++)
924946
{
925-
this.context.lineTo(ox + this.game.math.p2px(points[i][0]), oy + this.game.math.p2px(points[i][1]));
947+
this.context.lineTo(this.game.math.p2px(points[i][0]), this.game.math.p2px(points[i][1]));
926948
}
927949

928950
this.context.closePath();
929-
this.context.strokeStyle = color;
930951
this.context.stroke();
952+
this.context.restore();
931953

932954
this.stop();
933955

0 commit comments

Comments
 (0)