Hardon collider and image centers

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
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Hardon collider and image centers

Post 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?
User avatar
slime
Solid Snayke
Posts: 3131
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Hardon collider and image centers

Post 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)
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Hardon collider and image centers

Post by Plu »

Ah, there we go :) Thanks, I figured there had to be something I was missing!
User avatar
OnACoffeeBreak
Prole
Posts: 28
Joined: Tue Apr 11, 2017 11:19 am

Re: Hardon collider and image centers

Post 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?
User avatar
OnACoffeeBreak
Prole
Posts: 28
Joined: Tue Apr 11, 2017 11:19 am

[solved] Re: Hardon collider and image centers

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

Who is online

Users browsing this forum: No registered users and 50 guests