-
-
Save belzecue/b6ad49bbc7747461bad71d96d7575992 to your computer and use it in GitHub Desktop.
Catmull-Rom Spline in GDScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func catmull_rom_spline( | |
_points: Array, resolution: int = 10, extrapolate_end_points = true | |
) -> PackedVector2Array: | |
var points = _points.duplicate() | |
if extrapolate_end_points: | |
points.insert(0, points[0] - (points[1] - points[0])) | |
points.append(points[-1] + (points[-1] - points[-2])) | |
var smooth_points := PackedVector2Array() | |
if points.size() < 4: | |
return points | |
for i in range(1, points.size() - 2): | |
var p0 = points[i - 1] | |
var p1 = points[i] | |
var p2 = points[i + 1] | |
var p3 = points[i + 2] | |
for t in range(0, resolution): | |
var tt = t / float(resolution) | |
var tt2 = tt * tt | |
var tt3 = tt2 * tt | |
var q = ( | |
0.5 | |
* ( | |
(2.0 * p1) | |
+ (-p0 + p2) * tt | |
+ (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * tt2 | |
+ (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * tt3 | |
) | |
) | |
smooth_points.append(q) | |
return smooth_points |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://twitter.com/JoelBesada/status/1804272313619103844