How does Love handle box2d?

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
Magicked
Prole
Posts: 4
Joined: Sun Aug 26, 2012 2:18 pm

How does Love handle box2d?

Post by Magicked »

Please correct me if I'm wrong about anything here. I'm new to Love! From what I understand, the underlying physics engine is Box2d, which uses an x, y location coordinate in the center of the object. Does Love provide any automatic translation of coordinates when drawing its own objects (like images)? For example, if I have an image that is a circle, I obviously want to draw it in the same location as the physics body. Since the image is drawn from the top left, do I need to adjust for that like so:

Code: Select all

love.graphics.draw(self.image, self.body:getX() - self.image:getWidth() / 2, self.body:getY() - self.image.getHeight() / 2)
or can I do this:

Code: Select all

love.graphics.draw(self.image, self.body:getX(), self.body:getY())
Thanks in advance!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How does Love handle box2d?

Post by bartbes »

Graphics and physics are completely decoupled, so indeed, it does not take the origin into account. That said, it's trivial to define your shapes as if they have a top-left origin.
Magicked wrote:

Code: Select all

love.graphics.draw(self.image, self.body:getX() - self.image:getWidth() / 2, self.body:getY() - self.image.getHeight() / 2)
I'd strongly suggest you use the offset parameters to love.graphics.draw when you do things like this, since that handles things like rotation and scaling properly.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How does Love handle box2d?

Post by Nixola »

I think you have to subtract half the width and height to x and y coordinates
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Magicked
Prole
Posts: 4
Joined: Sun Aug 26, 2012 2:18 pm

Re: How does Love handle box2d?

Post by Magicked »

I'd strongly suggest you use the offset parameters to love.graphics.draw when you do things like this, since that handles things like rotation and scaling properly.
Oh nice, I did not know this. That makes sense!

I have my circle image colliding and rotating properly so far. Thanks for the help! :awesome:
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests