Page 1 of 1

Screen Shake effect

Posted: Tue Jan 19, 2016 8:02 pm
by ccde
I am trying to implement a screen shake effect that occurs each time an enemy is killed in my game, however I can't figure out for the life of me how to do so. Can anyone help me out here? Been stuck for a day or two now trying to get this working. Thanks :)

Re: Screen Shake effect

Posted: Tue Jan 19, 2016 8:32 pm
by vrld
There is an example in the documentation of hump. If you don't want to use hump, this should work (untested, and should be adapted for use in your game):

Code: Select all

local t, shakeDuration, shakeMagnitude = 0, -1, 0
function startShake(duration, magnitude)
    t, shakeDuration, shakeMagnitude = 0, duration or 1, magnitude or 5
end

function love.update(dt)
    if t < shakeDuration then
        t = t + dt
    end
end

function love.draw()
    if t < shakeDuration then
        local dx = love.math.random(-shakeMagnitude, shakeMagnitude)
        local dy = love.math.random(-shakeMagnitude, shakeMagnitude)
        love.graphics.translate(dx, dy)
    end
end

Re: Screen Shake effect

Posted: Tue Jan 19, 2016 9:11 pm
by ccde
vrld wrote:There is an example in the documentation of hump. If you don't want to use hump, this should work (untested, and should be adapted for use in your game):

Code: Select all

local t, shakeDuration, shakeMagnitude = 0, -1, 0
function startShake(duration, magnitude)
    t, shakeDuration, shakeMagnitude = 0, duration or 1, magnitude or 5
end

function love.update(dt)
    if t < shakeDuration then
        t = t + dt
    end
end

function love.draw()
    if t < shakeDuration then
        local dx = love.math.random(-shakeMagnitude, shakeMagnitude)
        local dy = love.math.random(-shakeMagnitude, shakeMagnitude)
        love.graphics.translate(dx, dy)
    end
end
Just what I was looking for, thanks!

Re: Screen Shake effect

Posted: Tue Sep 21, 2021 12:45 pm
by Chief Plankton
Does this still work, good sir?

Re: Screen Shake effect

Posted: Wed Sep 22, 2021 5:40 am
by zorg
Chief Plankton wrote: Tue Sep 21, 2021 12:45 pm Does this still work, good sir?
Nothing changed in the 5 years since the last post to make it not work, a quick look at the wiki articles about the used functions would also make it obvious... also please don't necro threads in the future.