Shaky, Jittery camera when moving diagonally

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
devtastic
Prole
Posts: 3
Joined: Thu Jan 21, 2021 1:58 pm

Shaky, Jittery camera when moving diagonally

Post 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
User avatar
dusoft
Party member
Posts: 791
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Shaky, Jittery camera when moving diagonally

Post by dusoft »

Common issues, usually solved by:
  1. using: love.graphics.setDefaultFilter("nearest", "nearest")
  2. rounding all drawing coordinates to integers
devtastic
Prole
Posts: 3
Joined: Thu Jan 21, 2021 1:58 pm

Re: Shaky, Jittery camera when moving diagonally

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests