/CatRom

Creates Catmull-Rom splines for Roblox.

Primary LanguageLuaMIT LicenseMIT

CatRom logo CatRom logo

Creates Catmull-Rom splines.

The Catmull-Rom spline (CatRom) is a cousin of the popular Bézier curve, with the key difference that CatRoms are guaranteed to pass through their control points. This allows them to chain together predictably and intuitively.

How to use

The CatRom constructor takes 3 arguments:

  1. points: An array of Vector2s, Vector3s, or CFrames.
  2. alpha [optional]: A number (usually) in [0, 1] that determines the "parameterization" of the spline; defaults to 0.5.
  3. tension [optional]: A number (usually) in [0, 1] that determines how loose the spline is; defaults to 0.

The default alpha of 0.5 is the only way to avoid cusps and loops, as shown in this paper.

API

Note: For each Solve method, there exists a SolveUniform counterpart that spaces the input(s) uniformly along the curve. Be aware that the uniform methods are slower to compute.

CatRom.new(points: array, alpha: number?, tension: number?)
CatRom:SolvePosition(t: number)
CatRom:SolveCFrame(t: number)
CatRom:SolveRotCFrame(t: number)
CatRom:SolveVelocity(t: number)
CatRom:SolveAcceleration(t: number)
CatRom:SolveTangent(t: number)
CatRom:SolveNormal(t: number)
CatRom:SolveBinormal(t: number)
CatRom:SolveCurvature(t: number)
CatRom:SolveLength(a: number?, b: number?)
CatRom:PrecomputeArcLengthParams(numIntervals: number?)

Performance Tips

1. Uniform methods

If you are calling many Uniform methods, you should call PrecomputeArcLengthParams() immediately after construction. This will make your Uniform calls less accurate but cheaper to compute. The accuracy can be further tuned using the numIntervals argument; lower is faster and less accurate, higher is slower and more accurate (defaults to 16).

2. Repeated inputs

If you are calling many methods on the same input like so:

local t -- number in [0, 1]
local catRom -- a CatRom object
catRom:SolvePosition(t)
catRom:SolveVelocity(t)
catRom:SolveTangent(t)

then it is faster to instead do

local t -- number in [0, 1]
local catRom -- a CatRom object
local spline, splineT = catRom:GetSplineFromT(t)
spline:SolvePosition(splineT)
spline:SolveVelocity(splineT)
spline:SolveTangent(splineT)