I want to implement custom tween service. But there’s some easing styles, which used some strange formulas. Example of one of that easing styles is Bounce.
Does someone here know exact formula for it?
I want to implement custom tween service. But there’s some easing styles, which used some strange formulas. Example of one of that easing styles is Bounce.
Does someone here know exact formula for it?
I don’t know the exact formula but I found this function:
function easeOutBounce(x: number): number {
const n1 = 7.5625;
const d1 = 2.75;
if (x < 1 / d1) {
return n1 * x * x;
} else if (x < 2 / d1) {
return n1 * (x -= 1.5 / d1) * x + 0.75;
} else if (x < 2.5 / d1) {
return n1 * (x -= 2.25 / d1) * x + 0.9375;
} else {
return n1 * (x -= 2.625 / d1) * x + 0.984375;
}
}
From this helpful website: Easing Functions Cheat Sheet
You can use TweenService:GetValue which takes a linear number between 0-1, and returns a number between 0-1 based on an EasingStyle (bounce in this case) and an EasingDirection.
local TweenService = game:GetService("TweenService")
print(TweenService:GetValue(.5, Enum.EasingStyle.Bounce, Enum.EasingDirection.In))
--prints 0.234375
don’t know either but it can be approximated with a sine function
Edit: cosine works better as f(1)=1 every time a
becomes an even number
I know about this variant but I don’t love it bc I need formula and not value.
TweenService:GetValue
. You could try and fiddle with some functions till you get one that looks like this.
https://easings.net/ this website contains all the easing styles and more
And heres a lua translation from Canim
local function outBounce(t, b, c, d)
t = t / d
if t < 1 / 2.75 then
return c * (7.5625 * t * t) + b
elseif t < 2 / 2.75 then
t = t - (1.5 / 2.75)
return c * (7.5625 * t * t + 0.75) + b
elseif t < 2.5 / 2.75 then
t = t - (2.25 / 2.75)
return c * (7.5625 * t * t + 0.9375) + b
else
t = t - (2.625 / 2.75)
return c * (7.5625 * t * t + 0.984375) + b
end
end
local function inBounce(t, b, c, d)
return c - outBounce(d - t, 0, c, d) + b
end
local function inOutBounce(t, b, c, d)
if t < d / 2 then
return inBounce(t * 2, 0, c, d) * 0.5 + b
else
return outBounce(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b
end
end
local function outInBounce(t, b, c, d)
if t < d / 2 then
return outBounce(t * 2, b, c / 2, d)
else
return inBounce((t * 2) - d, b + c / 2, c / 2, d)
end
end
To run the lua translations you call them like this: Function(alpha, 0, 1, 1)
Here’s that same function in Lua if anybody needs it:
local function easeOutBounce(x: number)
local n1 = 7.5625
local d1 = 2.75
if x < 1 / d1 then
return n1 * x * x
elseif x < 2 / d1 then
x -= 15
return n1 * (x / d1) * x + 0.75
elseif x < 2.5 / d1 then
x -= 2.25
return n1 * (x / d1) * x + 0.9375
else
x -= 2.625
return n1 * (x / d1) * x + 0.984375
end
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.