With polygon & rectangle shapes it is very straightforward to do:
Code: Select all
triangle = love.physics.newPolygonShape(body, 100,200, 200,100, 300, 200)
square = love.physics.newRectangleShape(body, 100,400, 100,100)
...
love.graphics.polygon( 'line', triangle:getPoints() )
love.graphics.polygon( 'line', square:getPoints() )
Circle shapes are a bit more difficult. The
CircleShape api includes a (undocummented) method called getRadius, but not a getLocalPosition (I created a
tracker item for this some weeks ago). So you have to cover that by using getBoundingBox:
Code: Select all
circle = love.physics.newCircleChape(body, 100,500, 50)
...
local x1, y1, x2, y2, x3, y3, x4, y4 = circle:getBoundingBox( )
local xMin, yMin, xMax = math.min(x1,x2,x3,x4), math.min(y1,y2,y3,y4), math.max(x1,x2,x3,x4)
local radius = (xMax-xMin)/2
love.graphics.circle( 'line', xMin+radius, yMin+radius, radius, radius*3) --last parameter ensures that the circle looks pretty no matter what
So I suppose you could bind all this together in one nice function. The trick is differentiating between polygonal/rectangular and circled shapes. Fortunately, we know that circled shapes don't have a getPoints function. So we test for that, and invoke one or the other method, like this:
Code: Select all
function drawShape(shape, style)
assert(type(shape.getBody)=='function', "draw shape needs a shape as its first parameter")
style = style or 'line' --style is 'line' by default
if(type(shape.getPoints)=='function') -- it has a getPoints function, so it is a polygon/rectangle shape
love.graphics.polygon( 'line', square:getPoints() )
else -- it must be a circled shape then
local x1, y1, x2, y2, x3, y3, x4, y4 = shape:getBoundingBox( )
local xMin, yMin, xMax = math.min(x1,x2,x3,x4), math.min(y1,y2,y3,y4), math.max(x1,x2,x3,x4)
local radius = (xMax-xMin)/2
love.graphics.circle( 'line', xMin+radius, yMin+radius, radius, radius*3)
end
end
DISCLAIMER: I wrote this function from the top of my head and haven't tested it. Some bug might have slipped through. Please comment if that is the case.