Skeletonxf wrote:Could you do something like this?
function love.physics.newRectangleImageShape(image)
local shape = love.physics.newRectangleShape(image:getWidth(),image:getHeight())
shape.image = image
shape.drawImage = function(self,x,y)
love.graphics.draw(self.image,x,y)
end
return shape
end
then in love.draw simply add
shape:drawImage()
NO!! BIG NO!!
This is NOT how [wiki]love.physics[/wiki] works, try reading and testing your answer works before commenting.
There are a few ways to do this, I haven't worked with love.physics so I don't know which works, the first one (and possibly erroneous) is to get the shape vertices ([wiki]PolygonShape:getPoints[/wiki])
Code: Select all
love.draw = function ()
love.graphics.polygon ("fill", shape:getPoints())
end
I guess you could use the above method with a mesh to render an image but I think that it is not entirely right since this is not how Box2D is used.
The other one and I think is the correct solution, would be to attach your [wiki]Shape[/wiki] to a [wiki]Body[/wiki] with a [wiki]Fixture[/wiki] and then using [wiki]Body:getX[/wiki] and [wiki]Body:getY[/wiki] or simply [wiki]Body:getPosition[/wiki]
Code: Select all
love.draw = function ()
love.graphics.draw(image, body:getX(), body:getY(), body:getAngle())
end
Notice that in the above example we even have the angle!
PS: Remember that all the above functions are in [wiki]World[/wiki] coordinates, provably in the unit you set as meter, if 1 meter is not 1 pixel then you most likely will need to do some math to turn your world coordinates into something you can use for drawing, that is where camera libraries come in handy, there are some here in the forums like
gamera and
hump.camera, you can surely find a few doing a
search
Why was the example provided by Skeletonxf wrong? Well LÖVE doesn't return tables, it returns userdata, userdata can be accessed like tables, with methods and indexs, but modifying the fields is not a nice thing to do, so in the above case, adding the "image" field would be wrong even though it might not error. Also shapes don't have an "x" and "y" field you can directly access, you have to do it through functions as I pointed above, so shape.x and shape.y will most likely return nil. Modifying the behaviour of LÖVE functions, in the above example "love.physics.newRectangleShape" is not recommended, other parts of your code may be expecting the default behaviour and when you change it you are breaking that (this is the same reason why globals are not recommended)