## Debug draw for box2d physics World

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

### Re: Debug draw for box2d physics World

tio wrote:And opening brackets at the same line as function definition
Good thing that's the default for Lua.

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

### Re: Debug draw for box2d physics World

Updated, now draws only on screen bodies and can draw from coordinates in world.

nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

### Re: Debug draw for box2d physics World

This is really helpful for debugging physics issues, I was thinking about writing something similar for my own use but found this first Thanks a lot.

I do have one observation to make: your math.randomseed(1) call will forever break random number generation in the project that is being debugged. Resetting the seed at the end of the function doesn't help either, because you'd need the system time in milliseconds, due to the frequent love.draw() calls (and os.time() apparently gives a value in seconds). On the other hand, I understand why you hardcoded the seed (in order to get the same "random" colors for the same fixtures for each screen redraw) and I couldn't think of a way to achieve the same effect and still obtain different colours for a larger number of fixtures.

bartbes
Sex machine
Posts: 4942
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Debug draw for box2d physics World

Use love.math's RandomGenerator objects?

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

### Re: Debug draw for box2d physics World

Updated to fix a few issues.

nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

### Re: Debug draw for box2d physics World

Thanks for the update, random generation works properly now.

edbucks
Prole
Posts: 1
Joined: Fri Jun 17, 2016 2:54 pm

### Re: Debug draw for box2d physics World

Thank you so much for this script. For the latest version I changed

Code: Select all

love.graphics.point
to

Code: Select all

love.graphics.points
and all was well.

The_JinJ
Prole
Posts: 8
Joined: Fri Jul 22, 2016 12:43 am

### Re: Debug draw for box2d physics World

Very handy! Thanks

girng
Prole
Posts: 39
Joined: Fri Sep 07, 2018 5:42 am

### Re: Debug draw for box2d physics World

not sure what to do with the "(world,x,y,width,height)" part.

where are u getting x, y, width and height from?

i'm looking for a tool just to draw the shapes on all my bodies in my tilemap. hmm

ok i just put 0,0,0,0

screen turns red

after i create some shapes, i eventually get

Code: Select all

Error

addons/debugWorldDraw.lua:93: attempt to call field 'point' (a nil value)

Traceback

main.lua:253: in function 'draw'
[C]: in function 'xpcall'
gtx 950, latest lua 11.1

ok, nevermind. found this snippet here:
https://love2d.org/wiki/Tutorial:PhysicsDrawing

at the very bottom. works great!

pgimeno
Party member
Posts: 1232
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Debug draw for box2d physics World

I use this library fairly often. I've updated it to work with versions up to 11.1. Since the author doesn't seem to frequent these forums any longer, I'll post my updated version, but all credit goes to Azhukar.

Changes are:

- All setColor calls have been made cross-version compatible.
- point() has been replaced with rectangle(). The rectangles are 3x3 pixels. setPointSize calls have been removed
- The calls deprecated in 11.1 are not called if the new calls are found.
- The "eye" of the circles has been commented out.
- The library no longer alters the global state (colour, line width, point size).

As for girng's question: you pass it the top left corner's position x and y, and the width and height, of the area to draw, in physics units. The idea is to not even consider any object that is outside that area, as they don't need to be painted. If your units match pixels, and you're using the entire screen, you just use 0, 0, love.graphics.getWidth(), love.graphics.getHeight().
Attachments
debugWorldDraw_11.lua