Scaling and camera offset

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
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Scaling and camera offset

Post by norubal »

Hi. I have a problem with code that I'm working on nowadays. Please check my .love file:
cam.love
(178.05 KiB) Downloaded 124 times
It's advanced hamster ball, player can move character with left/up/right/down key, but player can also move camera with w/a/s/d and zoom in/out camera with q, e key. It works well but this is where the problem begins.
1.png
1.png (823.41 KiB) Viewed 5445 times
When I zoom in/zoom out, currently camera is fixed at top left corner. But I'd like to change it - if I zoom in / zoom out, I'd like to fix camera at central place of the screen. So I might change position of camera when I zoom in/zoom out. My question is - how much?

Code: Select all

if love.keyboard.isDown("q") then
	-- scale down
	camscale = camscale - 0.2 * dt
	-- camx = camx - ???
	-- camy = camy - ???
	
elseif love.keyboard.isDown("e") then
	--scale up
	camscale = camscale + 0.2 * dt		
	-- camx = camx + ???
	-- camy = camy + ???
		
end
2.png
2.png (926.91 KiB) Viewed 5445 times
I think bit it's common question to ask, but I couldn't find answer at the forum. So if are there any reference to check please share. Thanks for sparing your time at reading this post and have a nice day.
User avatar
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Re: Scaling and camera offset

Post by norubal »

:oops: I found solution according to this post: https://love2d.org/forums/viewtopic.php?f=4&t=77714

Code: Select all

-- scaling
if love.keyboard.isDown("q") or love.keyboard.isDown("e") then
	local zoominc
	if love.keyboard.isDown("q") then
		zoominc = -zoomspeed * dt
	elseif love.keyboard.isDown("e") then
		zoominc = zoomspeed * dt
	end
	
	local width, height, flags = love.window.getMode()
	local mouse_x = width / 2 - camx
	local mouse_y = height / 2 - camy
	local lastzoom = camscale
	camscale = camscale + zoominc
	local newx = mouse_x * (camscale/lastzoom)
	local newy = mouse_y * (camscale/lastzoom)
	camx = camx + (mouse_x-newx)
	camy = camy + (mouse_y-newy)
end
I attach .love file for further reference.
Attachments
cam_center.love
(178.24 KiB) Downloaded 119 times
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Scaling and camera offset

Post by darkfrei »

Why the WASD is reversed?

Why not exponential zoom?

Code: Select all

camscale_factor = (1+dt*zoomspeed) 
-- or camscale_factor  = 1/(1+dt*zoomspeed) 
camscale = camscale * camscale_factor
Attachments
cam_center-01a.love
(178.33 KiB) Downloaded 111 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Scaling and camera offset

Post by pgimeno »

darkfrei wrote: Tue Nov 10, 2020 3:51 pm Why not exponential zoom?

Code: Select all

camscale_factor = (1+dt*zoomspeed) 
-- or camscale_factor  = 1/(1+dt*zoomspeed) 
camscale = camscale * camscale_factor
That formula doesn't do framerate-independent exponential zoom. You need something like this:

Code: Select all

local zoom_magnification_per_second = 2 -- magnify 2x every second
local zoomspeed = math.log(zoom_magnification_per_second)

...

camscale_factor = math.exp(zoomspeed * dt)
-- or camscale_factor = math.exp(-zoomspeed * dt)
User avatar
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Re: Scaling and camera offset

Post by norubal »

I love you all :)
Post Reply

Who is online

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