Recently I stumbled upon this piece of code:
local Spring = {} do
Spring.__index = Spring
function Spring.new(freq, pos)
local self = setmetatable({}, Spring)
self.f = freq
self.p = pos
self.v = pos*0
return self
end
function Spring:Update(dt, goal)
local f = self.f*2*pi
local p0 = self.p
local v0 = self.v
local offset = goal - p0
local decay = exp(-f*dt)
local p1 = goal + (v0*dt - offset*(f*dt + 1))*decay
local v1 = (f*dt*(offset*f - v0) + v0)*decay
self.p = p1
self.v = v1
return p1
end
function Spring:Reset(pos)
self.p = pos
self.v = pos*0
end
end
I found it in Roblox’ Freecam script and the way do
is used, surprised me. Until now I just used do
for loops. Can someone explain to me why do
was used in this context and what it does?