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?
Hardon collider and image centers
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- slime
- Solid Snayke
- Posts: 3163
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Hardon collider and image centers
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:
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
Ah, there we go Thanks, I figured there had to be something I was missing!
- OnACoffeeBreak
- Prole
- Posts: 28
- Joined: Tue Apr 11, 2017 11:19 am
Re: Hardon collider and image centers
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:
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?
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)
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?
- OnACoffeeBreak
- Prole
- Posts: 28
- Joined: Tue Apr 11, 2017 11:19 am
[solved] Re: Hardon collider and image centers
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.
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.
Who is online
Users browsing this forum: Google [Bot] and 3 guests