drawable shapes

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
tiotags
Prole
Posts: 5
Joined: Tue Dec 29, 2009 5:36 pm

drawable shapes

Post by tiotags »

Hello,

An idea for the next version of love, make the shapes (polygon, circle) drawables, so you can draw them with love.graphics.draw

an example:

Code: Select all

specialtriangle = love.graphics.newWhatever(x1,y1,x2,y2,x3,y3)
love.graphics.draw(specialtriangle,whatever,whatever)
Any opinions ?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: drawable shapes

Post by Robin »

That might be an idea, but I don't think it should be in the LÖVE core: Shapes are for physics simulation, not for drawing (you have Images for that ;)). However, you (or someone else) could write a function that draws Shapes in the right position with primitives, maybe to be used together with CAMERA?
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: drawable shapes

Post by kikito »

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.
When I write def I mean function.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: drawable shapes

Post by osuf oboys »

Seems reasonable since this de facto is what we're doing during debug anyhow.

kikito: You can also use shape:getType() to get rid of the function testing. This could also be used to make a seamless extension of the love.graphics.draw function, checking if what we send is a shape or not.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: drawable shapes

Post by kikito »

Heh, I didn't see that one! Indeed, the getType() function is the way to go.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests