Page 1 of 4

Debug draw for box2d physics World

Posted: Thu Jan 30, 2014 9:13 am
by Azhukar
Pass this function your physics world object and call it in love.draw(). Helps with seeing what's going on.

Fixtures have random color, sensors are always pure blue, joints are green lines/points, contacts are red points.

Code: Select all

debugWorldDraw = require("debugWorldDraw")
function love.draw()
	debugWorldDraw(world,x,y,width,height)
end

--x,y = top left corner of drawn world area
--width,height = width and height of drawn world area
Update:
-fixed a bug causing multi-fixture bodies to be drawn many times
-modularized
-now uses math.newRandomGenerator

Re: Debug draw for box2d physics World

Posted: Thu Jan 30, 2014 7:02 pm
by fxva
cool

Re: Debug draw for box2d physics World

Posted: Thu Mar 06, 2014 4:50 pm
by tio
There's a little problem in this function: the colors and line widths are not restored at the end of function.
Despite that, it's a very great function :D

Re: Debug draw for box2d physics World

Posted: Thu Mar 06, 2014 9:36 pm
by Azhukar
tio wrote:the colors and line widths are not restored at the end of function.
You never restore color or line width after you change it, you always change it before you use it. That way you never run into a problem with colors/etc leaking into other draw calls.

Re: Debug draw for box2d physics World

Posted: Fri Mar 07, 2014 12:22 pm
by tio
I always thought that this was the right way (save current colors, change to draw a thing and change it back), mainly because of bitmap drawing.
Good to know that ^^

Re: Debug draw for box2d physics World

Posted: Fri Mar 07, 2014 12:58 pm
by Robin
tio wrote:I always thought that this was the right way (save current colors, change to draw a thing and change it back), mainly because of bitmap drawing.
Good to know that ^^
Both ways are the right way, really. Libraries should always restore things like colours, so that you can always use it. Code that doesn't restore colours can only be used with the change-colours-every-time way of doing things.

Re: Debug draw for box2d physics World

Posted: Fri Mar 07, 2014 1:30 pm
by Azhukar
Robin wrote:Both ways are the right way, really. Libraries should always restore things like colours, so that you can always use it. Code that doesn't restore colours can only be used with the change-colours-every-time way of doing things.
Working with a state machine and acting upon baseless assumptions of the state it is in is prone to unexpected behavior.

If you call a draw operation and desire it to use a certain color, yet you did not check whether the color you desire is the color you're using then you are in fact doing it the wrong way.

Comically by your own example the code that doesn't presume state will perform as expected, yet code that does won't.

Re: Debug draw for box2d physics World

Posted: Fri Mar 07, 2014 1:46 pm
by tio
Well, since I don't need to take care about color (in normal circumstances) to draw sprites, I often don't need to set color values. When I do, I restore to whatever state it was before (since previous state was working as expected).

Again, if that's not the right way to do, sorry, my bad. I just found the behavior weird and thought reporting was a good idea. :nyu:

Re: Debug draw for box2d physics World

Posted: Fri Mar 07, 2014 1:55 pm
by Azhukar
tio wrote:Well, since I don't need to take care about color (in normal circumstances) to draw sprites
Only you do, if you intend to draw your sprites in the color as you have them in your image file then you have to set the color to full white, i.e. 255,255,255 or you will start wondering why your sprites suddenly flicker when you added X feature.

Re: Debug draw for box2d physics World

Posted: Fri Mar 07, 2014 1:56 pm
by Robin
tio wrote:Again, if that's not the right way to do, sorry, my bad. I just found the behavior weird and thought reporting was a good idea. :nyu:
Nono, you were right, don't listen to Azukar.

You shouldn't need to do love.graphics.setColor(255, 255, 255) before every image you draw.