local Sod = {}
Sod.__index = Sod
function Sod:update(dt)
local target_vel = (self.target-self.prev_target)/dt
self.pos += self.vel*dt
self.vel += (
self.target
+self.k3*target_vel
-self.pos
-self.k1*self.vel
)*dt/self.k2
self.prev_target = self.target
end
function Sod:config(frequency_rads,damping,response)
local ifreq = 1/frequency_rads
self.k1,self.k2,self.k3 =
damping*ifreq*2,
ifreq*ifreq,
response*damping*ifreq
end
function sod(pos,frequency_rads,damping,response)
local o = {
pos = pos,
prev_target = pos,
target = pos,
-- In case pos is a vector, or any other arithmetically capable data
-- type, *0 guarantees we get the zero position of whatever type pos is.
vel = pos*0,
}
setmetatable(o,Sod)
o:config(frequency_rads,damping,response)
return o
end