Shaky, Jittery camera when moving diagonally
Posted: Sun Feb 23, 2025 4:57 pm
I'm making a 2D top down action RPG and I implemented the camera myself. For some reason, when I'm walking diagonally, the world map seems to shake or jitter (video - https://youtu.be/8g6Zl_hEq0M). I think the fault is in my camera object but I can't figure out why. This is the code I'm using for camera.lua file.
This is how I'm applying the camera in love.draw()
Code: Select all
local Camera = {
x = 0,
y = 0,
scale = 4
}
function Camera:apply()
love.graphics.push()
love.graphics.scale(self.scale, self.scale)
love.graphics.translate(-self.x, -self.y)
end
function Camera:clear()
love.graphics.pop()
end
function Camera:setPosition(x, y, dt)
-- Store viewport dimensions
local VPW = love.graphics.getWidth() / self.scale
local VPH = love.graphics.getHeight() / self.scale
-- Translate camera
self.x = (x - VPW / 2)
self.y = (y - VPH / 2)
local RS = self.x + VPW -- Right side
local BS = self.y + VPH -- Bottom side
if self.x < 0 then
self.x = 0
elseif RS > mapWidth then
self.x = mapWidth - VPW
end
if self.y < 0 then
self.y = 0
elseif BS > mapHeight then
self.y = mapHeight - VPH
end
end
return Camera
Code: Select all
function love.draw()
-- Draw world
Map.scene:draw(-Camera.x, -Camera.y, Camera.scale, Camera.scale)
Camera:apply()
-- Draw entities and objects
Bush.drawAll()
Player:draw()
Camera:clear()
end