-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpolation.lua
More file actions
executable file
·233 lines (195 loc) · 6.19 KB
/
interpolation.lua
File metadata and controls
executable file
·233 lines (195 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
--This is a slightly old library I originally made to make easing bullets and sprites in Create Your Frisk easier, but I ported it over to work with CelLua buttons instead!
local self = {}
local moving = {}
function self.linear(s,e,t)
return s+(e-s)*t
end
function self.easeinout(s,e,t)
return s+(e-s)*(-math.cos(t*math.pi)+1)*0.5
end
function self.easein(s,e,t)
return s+(e-s)*(-math.cos(t*math.pi*0.5)+1)
end
function self.easeout(s,e,t)
return s+(e-s)*(math.sin(t*math.pi*0.5))
end
function self.easeoutin(s,e,t)
if t < 0.5 then
return self.easeout(s,s+(e-s)*0.5,t*2)
else
return self.easein(s+(e-s)*0.5,e,(t-0.5)*2)
end
end
function self.bouncein(s,e,t)
return self.bounceout(e,s,1-t)
end
function self.bounceout(s,e,t)
local scale = 1
local offset = -1/3
local reps = 0
while (t-(offset+2/3*scale) > 0 and reps < 25) do
offset = offset+2/3*scale
scale = scale*0.5
reps = reps + 1
end
t = t-offset
return s+(e-s)*(1-math.sin(t*math.pi*1.5/scale)*scale)
end
function self.bounceinout(s,e,t)
if t < 0.5 then
return self.bouncein(s,s+(e-s)*0.5,t*2)
else
return self.bounceout(s+(e-s)*0.5,e,(t-0.5)*2)
end
end
function self.lightbouncein(s,e,t)
return self.lightbounceout(e,s,1-t)
end
function self.lightbounceout(s,e,t)
local scale = 1
local offset = -1/3
local reps = 0
while (t-(offset+2/3*scale) > 0 and reps < 25) do
offset = offset+2/3*scale
scale = scale*0.5
reps = reps + 1
end
t = t-offset
return s+(e-s)*(1-math.sin(t*math.pi*1.5/scale)*scale^2)
end
function self.lightbounceinout(s,e,t)
if t < 0.5 then
return self.lightbouncein(s,s+(e-s)*0.5,t*2)
else
return self.lightbounceout(s+(e-s)*0.5,e,(t-0.5)*2)
end
end
function self.stronginout(s,e,t)
return s+(e-s)*(-math.cos(self.easeinout(0,1,t)*math.pi)+1)*0.5
end
function self.strongin(s,e,t)
return s+(e-s)*(-math.cos(self.easein(0,1,t)*math.pi*0.5)+1)
end
function self.strongout(s,e,t)
return s+(e-s)*(math.sin(self.easeout(0,1,t)*math.pi*0.5))
end
function self.strongoutin(s,e,t)
if t < 0.5 then
return self.strongout(s,s+(e-s)*0.5,t*2)
else
return self.strongin(s+(e-s)*0.5,e,(t-0.5)*2)
end
end
function self.gentlein(s,e,t)
return s+(e-s)*(math.sin(self.easein(0,1,t)*math.pi*0.5))
end
function self.gentleout(s,e,t)
return s+(e-s)*(-math.cos(self.easeout(0,1,t)*math.pi)+1)*0.5
end
function self.gentleoutin(s,e,t)
if t < 0.5 then
return self.easeout(s,s+(e-s)*0.5,self.easeinout(0,1,t)*2)
else
return self.easein(s+(e-s)*0.5,e,(self.easeinout(0,1,t)-0.5)*2)
end
end
function self.taninout(s,e,t)
return s+(e-s)*(2*t-(math.tan((t-0.5)*math.pi/2)/2+0.5))
end
function self.tanoutin(s,e,t)
return s+(e-s)*(math.tan((t-0.5)*math.pi/2)/2+0.5)
end
function self.sharpinout(s,e,t)
return s+(e-s)*(1-math.sin(math.tan(math.cos(t*math.pi))))*0.5
end
function self.sharpin(s,e,t)
return s+(e-s)*(1-math.sin(math.tan(math.cos(t*math.pi*0.5))))
end
function self.sharpout(s,e,t)
return s+(e-s)*math.sin(math.tan(math.sin(t*math.pi*0.5)))
end
function self.sharpoutin(s,e,t)
if t < 0.5 then
return self.sharpout(s,s+(e-s)*0.5,t*2)
else
return self.sharpin(s+(e-s)*0.5,e,(t-0.5)*2)
end
end
--easing formulas below are courtesy of https://easings.net/
function self.backinout(s,e,t)
local c1 = 1.70158;
local c2 = c1 * 1.525;
if t < 0.5 then
return s+(e-s)*(((2 * t)^2 * ((c2 + 1) * 2 * t - c2)) / 2)
else
return s+(e-s)*(((2 * t - 2)^2 * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2)
end
end
function self.backin(s,e,t)
local c1 = 1.70158;
local c3 = c1 + 1;
return s+(e-s)*(c3 * t * t * t - c1 * t * t)
end
function self.backout(s,e,t)
local c1 = 1.70158;
local c3 = c1 + 1;
return s+(e-s)*(1 + c3 * (t - 1)^3 + c1 * (t - 1)^2)
end
function self.elasticinout(s,e,t)
local c5 = (2 * math.pi) / 4.5
if t == 0 then return s elseif t == 1 then return e end
if t < 0.5 then
return s+(e-s)*(-(2^(20 * t - 10) * math.sin((20 * t - 11.125) * c5)) / 2)
else
return s+(e-s)*((2^(-20 * t + 10) * math.sin((20 * t - 11.125) * c5)) / 2 + 1)
end
end
function self.elasticin(s,e,t)
local c4 = (2 * math.pi) / 3;
return t == 0 and s or t == 1 and e or s+(e-s)*(-(2^(10 * t - 10)) * math.sin((t * 10 - 10.75) * c4))
end
function self.elasticout(s,e,t)
local c4 = (2 * math.pi) / 3;
return t == 0 and s or t == 1 and e or s+(e-s)*(2^(-10 * t) * math.sin((t * 10 - 0.75) * c4) + 1)
end
--moves a button automatically, according to the easing mode you input for mode (assuming you are calling self.Update every frame),
--mode should just be the name of one of the functions above, minus the "self."
--if framebased is true, t is in frames; otherwise, it is in seconds.
--func is a function that will be called when the interpolation ends, useful for staring another interpolation automatically.
function self.MoveObj(obj,x,y,t,mode,func,alwaysfunc,framebased)
table.insert(moving,1,{obj=obj,mode=mode,tx=obj.x+x,ty=obj.y+y,sx=obj.x,sy=obj.y,t=t,ct=0,fb=framebased,endfunc=func,alwaysfunc=alwaysfunc})
end
function self.MoveObjTo(obj,x,y,t,mode,func,alwaysfunc,framebased)
table.insert(moving,1,{obj=obj,mode=mode,tx=x,ty=y,sx=obj.x,sy=obj.y,t=t,ct=0,fb=framebased,endfunc=func,alwaysfunc=alwaysfunc})
end
--call every frame for automatic interpolation
function self.Update(dt)
for i=#moving,1,-1 do
local m = moving[i]
if m.fb then
m.ct = math.min(m.ct + 1,m.t)
local newx,newy = self[m.mode](m.sx,m.tx,m.ct/m.t),self[m.mode](m.sy,m.ty,m.ct/m.t)
m.obj.x,m.obj.y = newx,newy
if m.alwaysfunc then m.alwaysfunc(m.obj) end
if m.ct == m.t then
if m.endfunc then m.endfunc(m.obj) end
table.remove(moving,i)
end
else
m.ct = math.min(m.ct + dt,m.t)
local newx,newy = self[m.mode](m.sx,m.tx,m.ct/m.t),self[m.mode](m.sy,m.ty,m.ct/m.t)
m.obj.x,m.obj.y = newx,newy
if m.alwaysfunc then m.alwaysfunc(m.obj) end
if m.ct == m.t then
if m.endfunc then m.endfunc(m.obj) end
table.remove(moving,i)
end
end
end
end
--THOUGH YOU LOOK AT WHERE IT WAS
--DON'T BE LOSING FAITH BECAUSE
--I WILL GRANT YOU ONE KEY
--CAN YOU PEOPLE FIND ME
--SVRXQVNCRUZPUkVJVFdBU05U
return self