Page 1 of 1

Hardon collider and image centers

Posted: Thu Mar 21, 2013 6:53 pm
by Plu
So, I was looking for any topics on this subject but I couldn't find them. Hardon Collider rectangle shapes use a moveTo and Rotate around their center. Love graphics don't have a center, but instead move and rotate around 0,0.

This is causing all sorts of needless math to be needed to properly align an image with its rectangular collision box. I can't be the first person to notice this, right? There must be some sort of easy solution to map the two on top of each other without having to calculate their exact point of overlap?

Re: Hardon collider and image centers

Posted: Thu Mar 21, 2013 7:35 pm
by slime
When you love.graphics.draw an image, set its offset x/y parameters to 1/2 the image's width and height, respectively. This will make the image's draw position and rotation location the center of the image.

Example:

Code: Select all

love.graphics.draw(image, x, y, angle, 1, 1, image:getWidth()/2, image:getWidth()/2)

Re: Hardon collider and image centers

Posted: Thu Mar 21, 2013 8:14 pm
by Plu
Ah, there we go :) Thanks, I figured there had to be something I was missing!

Re: Hardon collider and image centers

Posted: Wed May 24, 2017 2:19 pm
by OnACoffeeBreak
Resurrecting an old thread because I have a similar issue...

The above worked for me until my game objects stopped being placeholder squares and became more complex. This required that HardonCollider shapes change from squares/rectangles to polygons. The issue is that the HardonCollider rotate(a) rotates around the centroid of the polygon while the game object rotates around the center of the image, which are likely not the same.

I guess I can use HardonCollider:shape:center() and rotate the game object image around the same coords like this:

Code: Select all

collisionShape:rotate(angle)
cx, cy = collisionShape:center()
love.graphics.draw(image, x, y, angle, 1, 1, cx, cy)
But this will shift the coords of the game object image away from the center of the image and to the collision polygon centroid.

Is there a better way of doing this? I am thinking of trying love.graphics transformations instead of love.graphics.draw().

Alternatively, I could just make sure that my assets position the polygon centroid in the center of image. Right?

[solved] Re: Hardon collider and image centers

Posted: Thu May 25, 2017 11:34 am
by OnACoffeeBreak
I wound up centering the centroid of the game object shape to the image center. This resolved the rotation origin discrepancy between HardonCollider:shape:rotate() and lg.draw(image, x, y, angle, 1, 1, image:getWidth()/2, image:getHeight()/2).

The downside is that asset creation takes a bit longer. The upside is that I don't have to reconcile rotation origins between collision shapes and images in code.