Page 1 of 1

Shaky, Jittery camera when moving diagonally

Posted: Sun Feb 23, 2025 4:57 pm
by devtastic
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.

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
This is how I'm applying the camera in love.draw()

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

Re: Shaky, Jittery camera when moving diagonally

Posted: Sun Feb 23, 2025 7:02 pm
by dusoft
Common issues, usually solved by:
  1. using: love.graphics.setDefaultFilter("nearest", "nearest")
  2. rounding all drawing coordinates to integers

Re: Shaky, Jittery camera when moving diagonally

Posted: Mon Feb 24, 2025 6:26 am
by devtastic
Rounding the drawing coordinates worked like magic! The bushes stopped shaking when moving. But I still notice a little jitteriness of the map when walking diagonally. I guess that can be fixed by smoothening the camera. Thank you for your reply!