Skip to content

Instantly share code, notes, and snippets.

@dvdfu
Last active April 26, 2017 23:03
Show Gist options
  • Save dvdfu/70ea30a46d76da331243df1e63b88a76 to your computer and use it in GitHub Desktop.
Save dvdfu/70ea30a46d76da331243df1e63b88a76 to your computer and use it in GitHub Desktop.
local Oscillator = {}
Oscillator.__index = Oscillator
function Oscillator.new(period)
assert(period > 0)
return setmetatable({
period = period or 1,
time = 0
}, Oscillator)
end
function Oscillator:update(dt)
if self.time + dt < self.period then
self.time = self.time + dt
else
self.time = self.time + dt - self.period
end
end
function Oscillator:reset()
self.time = 0
end
function Oscillator:get()
return self.time
end
function Oscillator:getFraction()
return self.time / self.period
end
function Oscillator:getSin()
return math.sin(self:getFraction() * math.pi * 2)
end
function Oscillator:getCos()
return math.cos(self:getFraction() * math.pi * 2)
end
return setmetatable(Oscillator,
{ __call = function(_, ...) return Oscillator.new(...) end })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment