cheez3d Posted May 27, 2014 Posted May 27, 2014 (edited) -- EASING EQUATIONS BY ROBERT PENNER ([url=http://www.dzone.com/snippets/robert-penner-easing-equations]http://www.dzone.com/snippets/robert-pe ... -equations[/url]) -- -- CONVERTED TO LUA BY CHEEZ -- local Easing = { Linear = function(t,b,c,d) return c*t/d+b; end, Quadratic = { In = function(t,b,c,d) t = t/d; return c*t^2+b; end, Out = function(t,b,c,d) t = t/d; return -c*t*(t-2)+b; end, InOut = function(t,b,c,d) t = t/(d/2); if t<1 then return c/2*t^2+b; end; t = t-1; return -c/2*(t*(t-2)-1)+b; end; }, Cubic = { In = function(t,b,c,d) t = t/d; return c*t^3+b; end, Out = function(t,b,c,d) t = t/d-1; return c*(t^3+1)+b; end, InOut = function(t,b,c,d) t = t/(d/2); if t<1 then return c/2*t^3+b; end; t = t-2; return c/2*(t^3+2)+b; end; }, Quartic = { In = function(t,b,c,d) t = t/d; return c*t^4+b; end, Out = function(t,b,c,d) t = t/d-1; return -c*(t^4-1)+b; end, InOut = function(t,b,c,d) t = t/(d/2); if t<1 then return c/2*t^4+b; end; t = t-2; return -c/2*(t^4-2)+b; end; }, Quintic = { In = function(t,b,c,d) t = t/d; return c*t^5+b; end, Out = function(t,b,c,d) t = t/d-1; return c*(t^5+1)+b; end, InOut = function(t,b,c,d) t = t/(d/2); if t<1 then return c/2*t^5+b; end; t = t-2; return c/2*(t^5+2)+b; end; }, Sinusoidal = { In = function(t,b,c,d) return -c*math.cos(t/d*(math.pi/2))+c+b; end, Out = function(t,b,c,d) return c*math.sin(t/d*(math.pi/2))+b; end, InOut = function(t,b,c,d) return -c/2*(math.cos(math.pi*t/d)-1)+b; end; }, Exponential = { In = function(t,b,c,d) return c*2^(10*(t/d-1))+b; end, Out = function(t,b,c,d) return c*(-2^(-10*t/d)+1)+b; end, InOut = function(t,b,c,d) t = t/(d/2); if t<1 then return c/2*2^(10*(t-1))+b; end; t = t-1; return c/2*(-2^(-10*t)+2)+b; end; }, Circular = { In = function(t,b,c,d) t = t/d; return -c*(math.sqrt(1-t^2)-1)+b; end, Out = function(t,b,c,d) t = t/d-1; return c*math.sqrt(1-t^2)+b; end, InOut = function(t,b,c,d) t = t/(d/2); if t<1 then return -c/2*(math.sqrt(1-t^2)-1)+b; end; t = t-2; return c/2*(math.sqrt(1-t^2)+1)+b; end; }, Elastic = { In = function(t,b,c,d,a,p) t,a,p = t/d-1,a and a or 0,p and p or d*0.3; local s = nil; if a<math.abs(c) then a,s = c,p/4; else s = p/(2*math.pi)*math.asin(c/a); end; return -(a*2^(10*t)*math.sin((t*d-s)*(2*math.pi)/p))+b; end, Out = function(t,b,c,d,a,p) t,a,p = t/d,a and a or 0,p and p or d*0.3; local s = nil; if a<math.abs(c) then a,s = c,p/4; else s = p/(2*math.pi)*math.asin(c/a); end; return a*2^(-10*t)*math.sin((t*d-s)*(2*math.pi)/p)+c+b; end, InOut = function(t,b,c,d,a,p) t,a,p = t/(d/2)-1,a and a or 0,p and p or d*0.3*1.5; local s = nil; if a<math.abs(c) then a,s = c,p/4; else s = p/(2*math.pi)*math.asin(c/a); end; if t+1<1 then return -0.5*(a*2^(10*t)*math.sin((t*d-s)*(2*math.pi)/p))+b; end; return a*2^(-10*t)*math.sin((t*d-s)*(2*math.pi)/p)*0.5+c+b; end; }, Back = { In = function(t,b,c,d,s) t,s = t/d,s and s or 1.7; return c*t^2*((s+1)*t-s)+b; end, Out = function(t,b,c,d,s) t,s = t/d-1,s and s or 1.7; return c*(t^2*((s+1)*t+s)+1)+b; end, InOut = function(t,b,c,d,s) t,s = t/(d/2),s and s*1.525 or 1.7*1.525; if t<1 then return c/2*(t^2*((s+1)*t-s))+b; end; t = t-2; return c/2*(t^2*((s+1)*t+s)+2)+b; end; }, Bounce = { In = function(self,t,b,c,d) return c-self.Out(d-t,0,c,d)+b; end, Out = function(t,b,c,d) t = t/d; if t<1/2.75 then return c*(7.5625*t^2)+b; elseif t<2/2.75 then t = t-1.5/2.75; return c*(7.5625*t^2+0.75)+b; elseif t<2.5/2.75 then t = t-2.25/2.75; return c*(7.5625*t^2+0.9375)+b; else t = t-2.625/2.75; return c*(7.5625*t^2+0.984375)+b; end; end, InOut = function(self,t,b,c,d) if t<d/2 then return f(t*2,0,c,d)*0.5+b; end; return self.Out(t*2-d,0,c,d)*0.5+c*0.5+b; end; }; }; Interpolate = function(a,b,t,f,d,...) if f == "Linear" then return Easing.Linear(t,a,b-a,1); elseif f == "Bounce" and d ~= "Out" then return Easing.Bounce[d](Easing.Bounce,t,a,b-a,1); end; return Easing[f][d](t,a,b-a,1,...); end; Interpolate2D = function(a,x,b,y,t,f,d,...) return Interpolate(a,x,t,f,d,...),Interpolate(b,y,t,f,d,...); end; Interpolate3D = function(a,x,b,y,c,z,t,f,d,...) return Interpolate(a,x,t,f,d,...),Interpolate(b,y,t,f,d,...),Interpolate(c,z,t,f,d,...); end; Interpolate float Interpolate ( float StartingValue , float EndingValue , float Progress , string EasingFunction , string EasingDirection [ , float EasingAmptitude , float EasingPeriod , float EasingOvershoot ] ) Interpolate2D float Interpolate2D ( float StartingX , float EndingX , float StartingY , float EndingY , float Progress , string EasingFunction , string EasingDirection [ , float EasingAmptitude , float EasingPeriod , float EasingOvershoot ] ) Interpolate3D float Interpolate3D ( float StartingX , float EndingX , float StartingY , float EndingY , float StartingZ , float EndingZ , float Progress , string EasingFunction [ , string EasingDirection , float EasingAmptitude , float EasingPeriod , float EasingOvershoot ] ) Example: print(Interpolate(50,1,0.5,"Linear")); -- => 25; Interpolate(100,35,0.75,"Elastic","InOut"); This table includes all the easing functions that exist here. I just converted the equations form ActionScript to Lua. Have fun. Edited May 28, 2014 by Guest
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now