Curves - Gr8-Tools/game-developer-roadmap-doc GitHub Wiki
"Curves: Bezier, Catmull-Rom"
Total time: 10 hours
Introduction
Curves are widely used in computer graphics and game development for object modeling and animation. The two most common types of curves are Bezier curves and cubic splines. This lesson will explain the key concepts and algorithms behind these curves.
Key definitions
- Curve - A curve is a graphical representation of a set of points connected by continuous lines.
- Tangent - The tangent line at a point on a curve is the straight line that just touches the curve at that point. It represents the instantaneous direction of the curve at that point.
- Control point - Control points determine the shape of the curve and how it bends. Moving control points allows manipulating the curve.
- Linear curve - A curve where each segment between control points is a straight line. Does not allow smooth bending.
- Spline curve - A curve where each segment is calculated to smoothly interpolate between control points using spline interpolation algorithms. Allows smooth curving.
Working with curves in Unity
Unity uses spline curves under the hood. The main steps to work with curves in Unity are:
- Create a curve asset by going to Assets > Create > Curve
- Add control points by clicking in the curve editor
- Manipulate control points by dragging them to change the curve shape
- Set tangent handles to control smoothness of bending
- Sample curve at certain time to get position/rotation values
Spline interpolation algorithms
The main algorithms Unity uses to smoothly interpolate between control points are:
-
Catmull-Rom spline - The default spline used by Unity. Calculated to smoothly interpolate through all control points. Gives nice smooth curves but can overshoot at endpoints.
-
Cubic spline - Similar to Catmull-Rom but does not overshoot at endpoints. Slightly less smooth overall.
-
Linear spline - Straight line interpolation, no smoothing. Used for linear curves.
These splines are calculated using mathematical formulas involving the positions and tangents of neighboring control points. This article has more details on different spline algorithms.
Bezier curves
Definition
A Bezier curve is a parametric curve used in computer graphics and design. It uses Bernstein polynomials to define smooth curves with control points.
Algorithm
A quadratic Bezier curve uses two control points P0, P1 and an endpoint P2 to define the curve. The position of a point P on the curve is calculated as:
P = (1-t)^2 * P0 + 2(1-t)t * P1 + t^2 * P2
where t is a parameter between 0 and 1.
Cubic and higher order Bezier curves use more control points. The algorithm remains the same, using the appropriate degree Bernstein polynomial to calculate positions along the curve.
Catmull-Rom splines
Definition
A Catmull-Rom spline is a cubic spline interpolation technique used to generate smooth curves through a series of points. Unlike Bezier curves, the control points are not separate from the curve - they define the curve itself.
Articles
Videos
3 hours.
Tasks
MathLibs
- Implement a Bezier curve class to draw simple shapes on any number of knots.
Time: 2 hours.
- Implement a Catmull-Rom splines curve class to draw simple shapes on any number of knots.
Time: 2 hours.
- Use your implementations of Bezier curve and Catmull-Rom splines to draw curves on points, placed by user. Write proggram, where user can place any number of ordered points and can switch between two algorithms, which use to draw curve. On selecting algorithm the curve should be redrawn. Provide a specific "Reset" command to clear all created knots. The knots should be created by clicking mouse button on window. Drawing curve can be realised with help of "LineRenderer". Or you can replace drawing curve with moving object on a path, that have built on this points (it would be more interesting experience).
Time: 3 hours.