Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a bezier curve example #8194

Merged
merged 11 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
[Bezier Curve](../examples/animation/bezier_curve.rs) | Bezier curve example showing a cube following a 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

Expand Down
93 changes: 93 additions & 0 deletions examples/animation/bezier_curve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//! Demonstrates how to work with beizer curves.
Kjolnyr marked this conversation as resolved.
Show resolved Hide resolved
//!

use bevy::{math::cubic_splines::CubicCurve, prelude::*};

#[derive(Component)]
pub struct CubeCurve(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 point will build a curve looking like
//
// y
// │ .``.
// │ .-``-.
// │ .-` `-.
// ●─────────────── x
//
// (Do not comment my ascii art graph skills)
Kjolnyr marked this conversation as resolved.
Show resolved Hide resolved
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<Time>, mut query: Query<(&mut Transform, &CubeCurve)>) {
let step = (time.elapsed_seconds().sin() + 1.) / 2.;

for (mut transform, cube_curve) in &mut query {
Kjolnyr marked this conversation as resolved.
Show resolved Hide resolved
// position takes a point from the curve where 0 is the initial point
// and 1 is the last point
transform.translation = cube_curve.0.position(step);
}
}