Page 1 of 1

How does Love handle box2d?

Posted: Sun Aug 26, 2012 2:28 pm
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!

Re: How does Love handle box2d?

Posted: Sun Aug 26, 2012 2:56 pm
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.

Re: How does Love handle box2d?

Posted: Sun Aug 26, 2012 2:56 pm
by Nixola
I think you have to subtract half the width and height to x and y coordinates

Re: How does Love handle box2d?

Posted: Sun Aug 26, 2012 3:52 pm
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: