2023-04-17 16:16:56 +00:00
|
|
|
//! Demonstrates how to work with Cubic curves.
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
math::{cubic_splines::CubicCurve, vec3},
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Curve(CubicCurve<Vec3>);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(Update, animate_cube)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
// Define your control points
|
|
|
|
// These points will define the curve
|
|
|
|
// You can learn more about bezier curves here
|
|
|
|
// https://en.wikipedia.org/wiki/B%C3%A9zier_curve
|
|
|
|
let points = [[
|
|
|
|
vec3(-6., 2., 0.),
|
|
|
|
vec3(12., 8., 0.),
|
|
|
|
vec3(-12., 8., 0.),
|
|
|
|
vec3(6., 2., 0.),
|
|
|
|
]];
|
|
|
|
|
|
|
|
// Make a CubicCurve
|
Rename `Bezier` to `CubicBezier` for clarity (#9554)
# Objective
A Bezier curve is a curve defined by two or more control points. In the
simplest form, it's just a line. The (arguably) most common type of
Bezier curve is a cubic Bezier, defined by four control points. These
are often used in animation, etc. Bevy has a Bezier curve struct called
`Bezier`. However, this is technically a misnomer as it only represents
cubic Bezier curves.
## Solution
This PR changes the struct name to `CubicBezier` to more accurately
reflect the struct's usage. Since it's exposed in Bevy's prelude, it can
potentially collide with other `Bezier` implementations. While that
might instead be an argument for removing it from the prelude, there's
also something to be said for adding a more general `Bezier` into Bevy,
in which case we'd likely want to use the name `Bezier`. As a final
motivator, not only is the struct located in `cubic_spines.rs`, there
are also several other spline-related structs which follow the
`CubicXxx` naming convention where applicable. For example,
`CubicSegment` represents a cubic Bezier curve (with coefficients
pre-baked).
---
## Migration Guide
- Change all `Bezier` references to `CubicBezier`
2023-08-28 17:37:42 +00:00
|
|
|
let bezier = CubicBezier::new(points).to_curve();
|
2023-04-17 16:16:56 +00:00
|
|
|
|
|
|
|
// Spawning a cube to experiment on
|
|
|
|
commands.spawn((
|
|
|
|
PbrBundle {
|
|
|
|
mesh: meshes.add(shape::Cube::default().into()),
|
|
|
|
material: materials.add(Color::ORANGE.into()),
|
|
|
|
transform: Transform::from_translation(points[0][0]),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
Curve(bezier),
|
|
|
|
));
|
|
|
|
|
|
|
|
// Some light to see something
|
|
|
|
commands.spawn(PointLightBundle {
|
|
|
|
point_light: PointLight {
|
|
|
|
intensity: 9000.,
|
|
|
|
range: 100.,
|
|
|
|
shadows_enabled: true,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
transform: Transform::from_xyz(8., 16., 8.),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// ground plane
|
|
|
|
commands.spawn(PbrBundle {
|
|
|
|
mesh: meshes.add(shape::Plane::from_size(50.).into()),
|
|
|
|
material: materials.add(Color::SILVER.into()),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// The camera
|
|
|
|
commands.spawn(Camera3dBundle {
|
|
|
|
transform: Transform::from_xyz(0., 6., 12.).looking_at(Vec3::new(0., 3., 0.), Vec3::Y),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn animate_cube(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut query: Query<(&mut Transform, &Curve)>,
|
|
|
|
mut gizmos: Gizmos,
|
|
|
|
) {
|
|
|
|
let t = (time.elapsed_seconds().sin() + 1.) / 2.;
|
|
|
|
|
|
|
|
for (mut transform, cubic_curve) in &mut query {
|
|
|
|
// Draw the curve
|
|
|
|
gizmos.linestrip(cubic_curve.0.iter_positions(50), Color::WHITE);
|
|
|
|
// position takes a point from the curve where 0 is the initial point
|
|
|
|
// and 1 is the last point
|
|
|
|
transform.translation = cubic_curve.0.position(t);
|
|
|
|
}
|
|
|
|
}
|