Page 1 of 1

[Custom] Physics Objects

Posted: Sun Jul 31, 2016 4:51 pm
by Semeon
I was wondering if and how to add say a custom image to a physics object for example this code bellow taken from the love2d wiki :

Code: Select all

objects.ground.shape = love.physics.newRectangleShape(650, 50)
How can someone instead of loading a "newRectangleShape" get a custom image.
What I mean is something like this:

Code: Select all

objects.ground.shape = love.physics.newImage(image)
Thank you for your time..

Re: [Custom] Physics Objects

Posted: Sun Jul 31, 2016 6:19 pm
by The_JinJ
In love.draw you'd get the body coordinates and draw the image using those.

A quick search shows this thread which should be useful although for an earlier version of LOVE

viewtopic.php?t=8849

Re: [Custom] Physics Objects

Posted: Sun Jul 31, 2016 7:31 pm
by Skeletonxf
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()

Re: [Custom] Physics Objects

Posted: Wed Aug 03, 2016 2:23 am
by Positive07
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)

Re: [Custom] Physics Objects

Posted: Wed Aug 03, 2016 8:59 am
by Skeletonxf
I modified newRectangleImageShape for that reason xD

Well could you wrap a table with draw methods around the userdata then? Make the userdata a field in a table with extra methods and not mess with the userdata to add functionality.