[solved] How do i offset an image so that it centres properly with scale?

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
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

[solved] How do i offset an image so that it centres properly with scale?

Post by unixfreak »

I have an image that scales when the player collides with it, as a simple animation for when the player bounces off.

However, i can't figure out what to put as the offset for love.graphics.draw()
Here's a stripped down bit of code showing what i'm trying to achieve;
scale.love
(5.26 KiB) Downloaded 111 times
The problem: the image should always be centred on the orange box, no matter what the scale is. How can i calculate to draw the image like this using the offset parameters?
Last edited by unixfreak on Tue Jan 30, 2018 2:53 am, edited 1 time in total.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How do i offset an image so that it centres properly with scale?

Post by grump »

Get the center point of the image. Add it to the drawing coords, and set the origin to the coordinates of the center point:

Code: Select all

		local ox, oy = bumper.w * .5, bumper.h * .5 -- get center point
		love.graphics.draw(
			bumper.gfx, 	-- texture
			bumper.x + ox, 		-- x pos
			bumper.y + oy, 		-- y pos
			0, 				-- rotation
			bumper.scale, 	-- x scale
			bumper.scale, 	-- y scale
			ox,
			oy
		)
User avatar
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: How do i offset an image so that it centres properly with scale?

Post by unixfreak »

grump wrote: Mon Jan 29, 2018 10:09 am Get the center point of the image. Add it to the drawing coords, and set the origin to the coordinates of the center point:
For some reason i was trying to use the x/y values within the offset value itself, which obviously wouldn't work. Your method is perfect.
Thank you so much. :ultrahappy:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 208 guests