Page 1 of 1
[STI] How to add map scrolling?
Posted: Wed Nov 20, 2019 3:18 am
by ZacBytes
Hi guys, I'm working on a top-down 2d game. I need the camera to follow WASD controls and also zoom in and out, but I'm not sure how to do that. I am using STI to draw the tiled map. Can anyone help me out?
Thanks!
Re: [STI] How to add map scrolling?
Posted: Sat Nov 23, 2019 2:51 am
by nequals30
This
tutorial for STI shows how to make the camera follow a player (see the draw section at the bottom).
The tutorial is a bit outdated (see
this comment by Karai17). The right way to do it is essentially something like this:
Code: Select all
function love.draw()
-- Scale world
local scale = 2
local screen_width = love.graphics.getWidth() / scale
local screen_height = love.graphics.getHeight() / scale
-- Translate world so that player is always centred
local tx = math.floor(player.x - screen_width / 2)
local ty = math.floor(player.y - screen_height / 2)
-- Draw (different from tutorial)
map:draw(-tx, -ty, scale, scale)
end
Would make the camera centered on player.x and player.y. Then you could change the 'scale' variable to zoom in and out.