Skip to content

Instantly share code, notes, and snippets.

@pablomayobre
Created April 2, 2016 01:30
Show Gist options
  • Save pablomayobre/a058bf4c01304153e35cf2fe0cb3a360 to your computer and use it in GitHub Desktop.
Save pablomayobre/a058bf4c01304153e35cf2fe0cb3a360 to your computer and use it in GitHub Desktop.
Screen shake for Lua
--Based on S0lll0s code https://love2d.org/forums/viewtopic.php?f=4&t=79207#p176746
local shake = {}
shake.__index = shake
local new = function (angle, intensity, duration, speed)
self = {
angle = angle,
intensity = intensity,
duration = duration,
speed = speed,
time = duration,
finished = false,
x = math.cos(angle),
y = math.sin(angle)
}
return setmetatable(self, shake)
end
shake.update = function (self, dt)
if self.time > 0 then
self.time = self.time - dt
else
self.finished = true
end
end
shake.restart = function (self)
self.time = 0
end
shake.get = function (self)
if not self.finished then
local perc = math.sqrt(self.time/self.duration)
local r = perc * math.sin(self.time * self.speed * perc) * self.intensity
local dx = self.x * r
local dy = self.y * r
return dx, dy, self.angle, r
end
return 0, 0, self.angle, 0
end
return new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment