Files
Roblox-Elevator-Game/src/shared/Algebra.lua

90 lines
2.0 KiB
Lua

--!optimize 2
--!native
--!strict
--My versions
type EaseFunction = (n: number) -> number
type LinearFunction = (a: number, b: number, t: number) -> number
export type RotationMatrix = {
Ixx: number,
Ixy: number,
Iyx: number,
Iyy: number,
Izx: number,
Izy: number
}
export type Scalar = {
Distance: number,
Center: Vector2,
Rotation: number
}
export type Math = {
Linear: LinearFunction,
InOutBack: EaseFunction,
OutBounce: EaseFunction,
InQuad: EaseFunction,
IsEven: (n: number) -> boolean,
RotationMatrix: (X: number, Y: number, Z: number) -> RotationMatrix,
Scalar: (X1: number, Y1: number, X2: number, Y2: number) -> Scalar,
maxmin: (min: number, n: number, max: number) -> number
}
local Math = {} :: Math
function Math.IsEven(n)
return bit32.band(n, 1) == 0
end
function Math.maxmin(min, n, max)
return math.max(min, math.min(n, max))
end
function Math.RotationMatrix(X: number, Y: number, Z: number): RotationMatrix
local cosX, sinX = math.cos(X), math.sin(X)
local cosY, sinY = math.cos(Y), math.sin(Y)
local cosZ, sinZ = math.cos(Z), math.sin(Z)
return {
Ixx = cosZ*cosX-sinZ*sinX*sinY,
Ixy = cosZ*sinX*sinY+sinZ*cosX,
Iyx = -cosZ*sinX-sinZ*cosX*sinY,
Iyy = cosZ*cosX*sinY-sinZ*sinX,
Izx = -sinZ*cosY,
Izy = cosZ*sinY
}
end
function Math.Scalar(X1: number, Y1: number, X2: number, Y2: number): Scalar
return {
Distance = math.sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
Center = Vector2.new((X1+X2)/2,(Y1+Y2)/2);
Rotation = math.deg(math.atan2(Y1-Y2,X1-X2))
}
end
--My versions
function Math.Linear(a,b,t)
return a-a*t+b*t
end
local c = 2.59491
function Math.InOutBack(n)
return n<.5 and 2*n*n*(-c+2*n+2*c*n) or 3*(-1+n)*(-1+n)*(-2-c+2*n+2*c*n)
end
local n1, d1 = 7.5625, 2.75
function Math.OutBounce(n)
return (n<0.363636 and n*n*n1 or
n<0.727273 and (.75*(1.*d1-2.*n*n1)) or
n<0.909091 and (.9375*(1.*d1-2.4*n*n1)/d1)) or (.984375*(1.*d1-2.66667*n*n1))/d1
end
function Math.InQuad(n)
return n*n
end
return Math