1 decade ago by newmicro
I have a Box2D entity (polygon shape). If I turn debug on, I can see the shape/outline of the body; however, this isn't what I want in the actual game. I don't want the entity to be a green line with gray fill, I have a particular color for the fill, stroke, etc.
I've managed to hack together this to draw, based on the vertex array of the body:
This works; however, when the body reacts/moves to objects hitting it in the world, while the debug drawing of the object looks right, my custom drawing of the body seems to react strangely (as if it's pivoting oppositely to the debug layer).
Is there a plugin or simple way of drawing Box2D shapes? It would be nice to just duplicate the built-in debug code, but I was hoping there was something easier.
I've managed to hack together this to draw, based on the vertex array of the body:
draw: function() { ig.system.context.lineWidth = 2; ig.system.context.fillStyle = this.fillStyle; ig.system.context.strokeStyle = this.strokeStyle; ig.system.context.beginPath(); var bX = this.body.GetPosition().x / Box2D.SCALE * 2; var bY = this.body.GetPosition().y / Box2D.SCALE * 2; ig.system.context.moveTo( bX + (this.vertices[1].x / Box2D.SCALE) * 2, bY + (this.vertices[1].y / Box2D.SCALE) * 2 ); ig.system.context.lineTo( bX + (this.vertices[2].x / Box2D.SCALE) * 2, bY + (this.vertices[2].y / Box2D.SCALE) * 2 ); ig.system.context.lineTo( bX + (this.vertices[3].x / Box2D.SCALE) * 2, bY + (this.vertices[3].y / Box2D.SCALE) * 2 ); ig.system.context.lineTo( bX + (this.vertices[0].x / Box2D.SCALE) * 2, bY + (this.vertices[0].y / Box2D.SCALE) * 2 ); ig.system.context.stroke(); ig.system.context.closePath(); ig.system.context.fill(); },
This works; however, when the body reacts/moves to objects hitting it in the world, while the debug drawing of the object looks right, my custom drawing of the body seems to react strangely (as if it's pivoting oppositely to the debug layer).
Is there a plugin or simple way of drawing Box2D shapes? It would be nice to just duplicate the built-in debug code, but I was hoping there was something easier.