[Custom] Physics Objects

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
User avatar
Semeon
Prole
Posts: 36
Joined: Tue Jul 12, 2016 1:35 pm

[Custom] Physics Objects

Post 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..
The_JinJ
Prole
Posts: 8
Joined: Fri Jul 22, 2016 12:43 am

Re: [Custom] Physics Objects

Post 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
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Re: [Custom] Physics Objects

Post 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()
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: [Custom] Physics Objects

Post 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)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Re: [Custom] Physics Objects

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 148 guests