# Bezier or Spline?

I am having creatures save waypoints at set intervals (bread crumbs)to mark their paths. When it comes time to visualize the path, I want it to curve, following the waypoints (rather than be angular). Which would be better suited to the task: beziers or splines? I don’t know enough about either to understand the differences. And, is there a best available script out there to hand this?

Bezier curves are very convenient when you don’t know the entire set of points in advance because they are very easy to build piece by piece. They are pretty efficient and conceptually straightforward. There is also an easy way to use quadratic Beziers (less “curvy” than the more common cubics) to round off a path. If you imagine the path as a zigzag line, then the curve passes through the centre point of each line segment and rounds off the corners. This thread has a script that implements this technique.

If you want the curve to pass through the waypoints, the most common type of curve to use is the Catmull-Rom spline. This doesn’t lend itself to piecewise construction of the path, as does the Bezier curve, but it is easier to use when you have the full list of points in advance and simply want to plot a smooth curve that passes through them.

Animating an object along a Bezier or Catmull-Rom path gives a physically plausible motion that accelerates convincingly. The main drawback is that the acceleration doesn’t vary smoothly. This is only a problem if, say, you want a vehicle that banks as it turns. If you need this behaviour, you can use B-splines or “natural” splines. The drawback with B-splines is that the curve passes near the waypoints, but doesn’t pass through them in any predictable way. Natural splines interpolate all the waypoints precisely, but they are complicated to implement and there is a preprocessing step that is CPU-heavy when there are lots of waypoints.

It sounds from your description that you might want Catmull-Rom splines, but it depends on how the creatures have to move. There is a spline controller script on the wiki that implements something similar to this, but post again if you need any further explanation or example code and I’ll see if I can fix you up with something.

1 Like

Thanks, Andeeee. Yes, it does sound like Catmull-Rom is what I need. I do have a full set of points. The question is, how much variance is there from the waypoints and where the curve actually passes? Since the creatures will only (generally) walk in significant curves when going around obstacles, I can’t have too much deviation from the waypoints, or I run the risk of a path passing though the obstacles that were being avoided.

I will read up on the Catmull-Rom implementation and get back to you with any more questions I have.

A Catmull-Rom spline passes through each point, but it tends to “anticipate” an oncoming turn (eg, it will turn outward to the right slightly before taking a sharp left turn). Cubic beziers can give you more precise control over the curve, but not with a simple algorithm that lets you throw in a set of points and get a curve back - you need some heuristic information about what the curve is “doing” (eg, avoiding an obstacle, reversing direction) to control it precisely.

Well, the way my system will work, I do need the curve to pass right through the waypoints, and I don’t anticipate the “anticipation” factor will be relevant, and here’s why.

I’m using the waypoints to mark the general path a creature has already traversed. Later, if a player happens upon the creature’s path, the game will then “fill in” the path with tracks. These tracks will be placed at intervals along the full path.

So, starting at the first waypoint, drop tracks every, say, 1.5 meters. The spline is used to fill in the track points between the actual saved waypoints. Because they are laid at intervals, the “anticipation effect” won’t be as noticeable as it is in a contiguous line.

Now, I was looking at the spline controller script. I just need to figure out how I can trim it down to just what I need, which is: given a full set of waypoints, calulate where each track should be placed, given an interval of distance, following the spline.