Screen Shake effect

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
ccde
Prole
Posts: 15
Joined: Sun Dec 29, 2013 10:47 pm

Screen Shake effect

Post 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 :)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Screen Shake effect

Post 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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
ccde
Prole
Posts: 15
Joined: Sun Dec 29, 2013 10:47 pm

Re: Screen Shake effect

Post 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!
Chief Plankton
Prole
Posts: 2
Joined: Fri Sep 17, 2021 6:04 am

Re: Screen Shake effect

Post by Chief Plankton »

Does this still work, good sir?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Screen Shake effect

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests