From cce340fa77b8968ba0d7758fa33c99b4091d38f2 Mon Sep 17 00:00:00 2001 From: kjolnyr Date: Fri, 24 Mar 2023 17:02:09 +0100 Subject: [PATCH 01/10] Adding a bezier curve example --- Cargo.toml | 10 ++++ examples/animation/bezier_curve.rs | 93 ++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 examples/animation/bezier_curve.rs diff --git a/Cargo.toml b/Cargo.toml index 9bd451a2e855e..0c8f8e6dbd68f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -713,6 +713,16 @@ description = "Create and play an animation defined by code that operates on the category = "Animation" wasm = true +[[example]] +name = "bezier_curve" +path = "examples/animation/bezier_curve.rs" + +[package.metadata.example.bezier_curve] +name = "Bezier Curve" +description = "Bezier curve example showing a cube following a curve" +category = "Animation" +wasm = true + [[example]] name = "custom_skinned_mesh" path = "examples/animation/custom_skinned_mesh.rs" diff --git a/examples/animation/bezier_curve.rs b/examples/animation/bezier_curve.rs new file mode 100644 index 0000000000000..beb8ff35d26f0 --- /dev/null +++ b/examples/animation/bezier_curve.rs @@ -0,0 +1,93 @@ +//! Demonstrates how to work with beizer curves. +//! + +use bevy::{math::cubic_splines::CubicCurve, prelude::*}; + +#[derive(Component)] +pub struct CubeCurve(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 point will build a curve looking like + // + // y + // │ .``. + // │ .-``-. + // │ .-` `-. + // ●─────────────── x + // + // (Do not comment my ascii art graph skills) + let control_point1 = Vec3::new(-6., 2., 0.); + let control_point2 = Vec3::new(12., 8., 0.); + + let control_point3 = Vec3::new(-12., 8., 0.); + let control_point4 = Vec3::new(6., 2., 0.); + + let points = [[ + control_point1, + control_point2, + control_point3, + control_point4, + ]]; + + // 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(control_point1), + ..default() + }, + CubeCurve(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