Debug draw for box2d physics World

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Debug draw for box2d physics World

Post by Azhukar » Thu Jan 30, 2014 9:13 am

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
Attachments
debugWorldDraw.lua
(2.65 KiB) Downloaded 312 times
Last edited by Azhukar on Fri Feb 13, 2015 10:44 pm, edited 8 times in total.

fxva
Prole
Posts: 8
Joined: Sat Jul 13, 2013 4:54 am

Re: Debug draw for box2d physics World

Post by fxva » Thu Jan 30, 2014 7:02 pm

cool

User avatar
tio
Citizen
Posts: 60
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Debug draw for box2d physics World

Post by tio » Thu Mar 06, 2014 4:50 pm

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

User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Debug draw for box2d physics World

Post by Azhukar » Thu Mar 06, 2014 9:36 pm

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.

User avatar
tio
Citizen
Posts: 60
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Debug draw for box2d physics World

Post by tio » Fri Mar 07, 2014 12:22 pm

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 ^^

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Debug draw for box2d physics World

Post by Robin » Fri Mar 07, 2014 12:58 pm

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.
Help us help you: attach a .love.

User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Debug draw for box2d physics World

Post by Azhukar » Fri Mar 07, 2014 1:30 pm

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.

User avatar
tio
Citizen
Posts: 60
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Debug draw for box2d physics World

Post by tio » Fri Mar 07, 2014 1:46 pm

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:

User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Debug draw for box2d physics World

Post by Azhukar » Fri Mar 07, 2014 1:55 pm

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.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Debug draw for box2d physics World

Post by Robin » Fri Mar 07, 2014 1:56 pm

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.
Help us help you: attach a .love.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests