diff --git a/Cargo.toml b/Cargo.toml index 0305fdec27855..5eae867043186 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -719,6 +719,16 @@ description = "Create and play an animation defined by code that operates on the category = "Animation" wasm = true +[[example]] +name = "cubic_curve" +path = "examples/animation/cubic_curve.rs" + +[package.metadata.example.cubic_curve] +name = "Cubic Curve" +description = "Bezier curve example showing a cube following a cubic curve" +category = "Animation" +wasm = true + [[example]] name = "custom_skinned_mesh" path = "examples/animation/custom_skinned_mesh.rs" diff --git a/examples/README.md b/examples/README.md index e96a5d1b58d6d..2783dde0d9a19 100644 --- a/examples/README.md +++ b/examples/README.md @@ -143,6 +143,7 @@ Example | Description --- | --- [Animated Fox](../examples/animation/animated_fox.rs) | Plays an animation from a skinned glTF [Animated Transform](../examples/animation/animated_transform.rs) | Create and play an animation defined by code that operates on the `Transform` component +[Cubic Curve](../examples/animation/cubic_curve.rs) | Bezier curve example showing a cube following a cubic curve [Custom Skinned Mesh](../examples/animation/custom_skinned_mesh.rs) | Skinned mesh example with mesh and joints data defined in code [glTF Skinned Mesh](../examples/animation/gltf_skinned_mesh.rs) | Skinned mesh example with mesh and joints data loaded from a glTF file diff --git a/examples/animation/cubic_curve.rs b/examples/animation/cubic_curve.rs new file mode 100644 index 0000000000000..d6aff04536faa --- /dev/null +++ b/examples/animation/cubic_curve.rs @@ -0,0 +1,89 @@ +//! Demonstrates how to work with Cubic curves. + +use bevy::{ + math::{cubic_splines::CubicCurve, vec3}, + prelude::*, +}; + +#[derive(Component)] +pub struct Curve(CubicCurve); + +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>, + mut materials: ResMut>, +) { + // 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 + let bezier = Bezier::new(points).to_curve(); + + // 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