Skip to content

Instantly share code, notes, and snippets.

@Fraktality
Fraktality / NaturalSplines.lua
Created July 3, 2023 01:31
Find the acceleration-minimizing curve between a list of points
-- Given a knot sequence p[1<=i<=n], solve for a sequence of cubic
-- splines s[1<=i<=n-1] such that the square of acceleration is minimized.
local gamma = {
0.50000000000000000, 0.28571428571428571, 0.26923076923076923, 0.26804123711340206,
0.26795580110497238, 0.26794966691339748, 0.26794922649742166, 0.26794919487697295,
0.26794919260672685, 0.26794919244373052, 0.26794919243202791, 0.26794919243118770,
0.26794919243112737, 0.26794919243112304, 0.26794919243112273, 0.26794919243112271,
}
local gammaLimit = gamma[#gamma]
@Fraktality
Fraktality / Interlock.lua
Last active October 28, 2022 07:00
Easy task lock
--[[
local fooInterlock = Interlock.new()
function foo()
local lock = fooInterlock:takeLock()
coroutine.yield()
if not lock:hasLock() then
return
-- Perform a breadth first traversal of an instance's descendant tree.
--
-- > for obj in scanBF(root) do
-- > print(obj)
-- > end
local function scanBF(root)
local queue = {} -- Queue containing stacks of children
local qIdx0 = 1 -- Queue front index
local qIdx1 = 0 -- Queue back index
@Fraktality
Fraktality / Signal.lua
Created August 12, 2020 06:34
Minimal signal datatype impl
local Connection = {} do
Connection.__index = Connection
function Connection.new(hook, signal)
return setmetatable({
_hook = hook,
_signal = signal,
_connected = true,
}, Connection)
end
@Fraktality
Fraktality / Zones.lua
Last active October 31, 2023 20:02
Fast trigger volumes
local RunService = game:GetService("RunService")
-- compile an oriented bounding box into a scaled CFrame
local function compileBBox(cframe: CFrame, size: Vector3)
return CFrame.fromMatrix(
cframe.Position,
cframe.XVector/size.X,
cframe.YVector/size.Y,
cframe.ZVector/size.Z
):Inverse()
local function bbox(part)
local size = part.Size
local inv = part.CFrame:inverse()
local wx = size*inv.XVector
local wy = size*inv.YVector
local wz = size*inv.ZVector
return Vector3.new(
math.abs(wx.x) + math.abs(wx.y) + math.abs(wx.z),
local pi = math.pi
local abs = math.abs
local atan2 = math.atan2
local cos = math.cos
local sqrt = math.sqrt
local sign = math.sign
local EPS = 1e-9
local function cbrt(x)
@Fraktality
Fraktality / WaitForFirst.lua
Created September 8, 2019 21:49
Wait for the first of the passed signals to fire
local function waitForFirst(...)
local shunt = Instance.new("BindableEvent")
local slots = {...}
local function fire(...)
for i = 1, #slots do
slots[i]:Disconnect()
end
return shunt:Fire(...)
end
@Fraktality
Fraktality / ValidateRotation.lua
Last active September 29, 2020 01:35
Verifies that a CFrame is a valid rotation matrix
local validateRotation do
local dot = Vector3.new().Dot
local cross = Vector3.new().Cross
local abs = math.abs
local EPS = 1e-4
local function assertEq(value, expected)
assert(abs(value - expected) < EPS)
end
-- Various ways of testing a screen point against a GuiBase2d
-- Note: Rounded rect corner radius is inferred from SliceCenter.Min.X
local cos = math.cos
local min = math.min
local rad = math.rad
local sin = math.sin
return {
-- Axis-aligned rectangle