Catmull-Rom Spline Paths

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Catmull-Rom Spline Paths

#1 Post by Remix »

Catmull-Rom is basically an algorithm that creates a smooth curving path that passes through all the defined points.
catmull.png
(image borrowed from https://package.elm-lang.org/packages/f ... test/Curve)

Unlike other curved paths such as Bezier, we do not have to pre-calculate control points to get the curves to hit our known points. We just tell it the points we want to go through and the clever math does all the rest.
script.rpy
(16.54 KiB) Downloaded 108 times
The script is a drop-in example of the code with a little aeroplane flying around and some dots to show the computed path. Just drop it in a new project and have a see what it can do.

There are bars to adjust:
Steps: number of calculated points between nodes
Alpha: A value between 0.0 and 1.0 makes most sense (0.5 is suitable for most use cases)
note: adjusting these re-calculates the spline so may tickle your CPU a teeny bit

The start of the script is the class and subclass, followed by a python transform, a Ren'Py transform, a screen and a couple of calls to show the screen. Hopefully it is simple enough to pull apart and find the bits you need.

The class actually support N-dimension nodes (for if you were working in 3d or 4d or weird stuff beyond that) and defaults to re-evaluating the calculated points to make them equidistant along the full path. As the math is not trivial, computed splines are added to a rolling spline_cache - so referencing a spline with the same args and kwargs will return a cached version rather than recompute.

Feel free to let me know what you think (or if you find a bug or snaffu type critter)
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: No registered users