|
1 | | -<!-- Derivative access patterns for curves --> |
2 | | -<!-- https://github.com/bevyengine/bevy/pull/16503 --> |
| 1 | +`bevy_math` has collected a sizable collection of curves and methods for working with curves, which are useful for everything from animations to color gradients to gameplay logic. |
3 | 2 |
|
4 | | -<!-- TODO --> |
| 3 | +One of the most natural and important things you might want to do with a curve is to inspect its **derivative**: |
| 4 | +the rate at which it's changing. |
| 5 | +You might even be after its **second derivative**: the rate at which the rate of change is changing. |
| 6 | + |
| 7 | +Accessing this information should be easy and error-resistant, |
| 8 | +but we also won't accept any complication or performance penalty in the more common derivative-less use cases. |
| 9 | + |
| 10 | +To satisfy these requirements, our resident mathematicians have built a careful solution involving multiple traits and wrapper types. |
| 11 | +For a full description of the mechanisms and considerations, dive down the rabbit hole into the linked PR description! |
| 12 | +Despite (or because of?) all of the mathematical type wizardry, the actual usage is straightforward: |
| 13 | + |
| 14 | +```rust |
| 15 | +let points = [ |
| 16 | + vec2(-1.0, -20.0), |
| 17 | + vec2(3.0, 2.0), |
| 18 | + vec2(5.0, 3.0), |
| 19 | + vec2(9.0, 8.0), |
| 20 | +]; |
| 21 | + |
| 22 | +// A cubic spline curve that goes through `points`. |
| 23 | +let curve = CubicCardinalSpline::new(0.3, points).to_curve().unwrap(); |
| 24 | + |
| 25 | +// Calling `with_derivative` causes derivative output to be included in the output of the curve API. |
| 26 | +let curve_with_derivative = curve.with_derivative(); |
| 27 | + |
| 28 | +// A `Curve<f32>` that outputs the speed of the original. |
| 29 | +let speed_curve = curve_with_derivative.map(|x| x.derivative.norm()); |
| 30 | +``` |
| 31 | + |
| 32 | +We've implemented the required traits for most of our native curve types: splines, lines, and all manner of compound curves. |
| 33 | +Curves which accept arbitrary functions are not covered (build your own specialized curve types), |
| 34 | +as Rust does not have a first-class notion of a differentiable function! |
0 commit comments