Upscalefanatic3

(Roblox) Spring Script [Learners]

Apr 3rd, 2020
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. --n is number of elements
  2. --d is damping (0 to inf, 1 is standard)
  3. --s is speed (0 to inf)
  4.  
  5. local spring = {}
  6.  
  7. local tick = tick
  8. local setmt = setmetatable
  9. local cos = math.cos
  10. local sin = math.sin
  11. local e = 2.718281828459045
  12.  
  13. function spring.new(i, d, s)
  14.     local null = 0*i
  15.  
  16.     local t0 = tick()
  17.     local p0 = i
  18.     local v0 = null
  19.     local p1 = i
  20.     local d = d or 1
  21.     local s = s or 1
  22.  
  23.     local self = {}
  24.     local meta = {}
  25.  
  26.     local function getpv(w)
  27.         local t = s*(w - t0)
  28.         local d2 = d*d
  29.  
  30.         local h, si, co
  31.         if d2 < 1 then
  32.             h = (1 - d2)^0.5
  33.             local exp = e^(-d*t)/h
  34.             co, si = exp*cos(h*t), exp*sin(h*t)
  35.         elseif d2 == 1 then
  36.             h = 1
  37.             local exp = e^(-d*t)/h
  38.             co, si = exp, exp*t
  39.         else
  40.             h = (d2 - 1)^0.5
  41.             local u = e^((-d + h)*t)/(2*h)
  42.             local v = e^((-d - h)*t)/(2*h)
  43.             co, si = u + v, u - v
  44.         end
  45.  
  46.         local a0 = h*co + d*si
  47.         local a1 = 1 - (h*co + d*si)
  48.         local a2 = si/s
  49.  
  50.         local b0 = -s*si
  51.         local b1 = s*si
  52.         local b2 = h*co - d*si
  53.  
  54.         return a0*p0 + a1*p1 + a2*v0,
  55.             b0*p0 + b1*p1 + b2*v0
  56.     end
  57.    
  58.     function self.init(p, v, t)
  59.         t0 = tick()
  60.         p0 = p or null
  61.         v0 = v or null
  62.         p1 = t or p or null
  63.     end
  64.  
  65.     function meta:__index(index)
  66.         local w = tick()
  67.         if index=="p" then
  68.             local p, v = getpv(w)
  69.             return p
  70.         elseif index=="v" then
  71.             local p, v = getpv(w)
  72.             return v
  73.         elseif index=="t" then
  74.             return p1
  75.         elseif index=="d" then
  76.             return d
  77.         elseif index=="s" then
  78.             return s
  79.         elseif index=="a" then
  80.             local p, v = getpv(w)
  81.             local a = s*s*(p1 - p) - 2*s*d*v
  82.             return a
  83.         end
  84.     end
  85.  
  86.     function meta:__newindex(index, value)
  87.         local w = tick()
  88.         p0, v0 = getpv(w)
  89.         t0 = w
  90.         if index=="p" then
  91.             p0 = value or null
  92.         elseif index=="v" then
  93.             v0 = value or null
  94.         elseif index=="t" then
  95.             p1 = value or null
  96.         elseif index=="d" then
  97.             d = value or 1
  98.         elseif index=="s" then
  99.             s = value or 1
  100.         end
  101.     end
  102.  
  103.     self.init()
  104.     return setmt(self, meta)
  105. end
  106.  
  107. return spring
Add Comment
Please, Sign In to add comment