Skip to content

Commit cfa750a

Browse files
Kjolnyrtim-blackbirdaevyrie
authored
Adding a bezier curve example (#8194)
# Objective Examples on how to use the freshly merged `Bezier` struct ( #7653 ) are missing. ## Solution - Added a `bezier_curve.rs` example in the `animation/` folder. --------- Co-authored-by: ira <[email protected]> Co-authored-by: Aevyrie <[email protected]>
1 parent e243175 commit cfa750a

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,16 @@ description = "Create and play an animation defined by code that operates on the
722722
category = "Animation"
723723
wasm = true
724724

725+
[[example]]
726+
name = "cubic_curve"
727+
path = "examples/animation/cubic_curve.rs"
728+
729+
[package.metadata.example.cubic_curve]
730+
name = "Cubic Curve"
731+
description = "Bezier curve example showing a cube following a cubic curve"
732+
category = "Animation"
733+
wasm = true
734+
725735
[[example]]
726736
name = "custom_skinned_mesh"
727737
path = "examples/animation/custom_skinned_mesh.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Example | Description
144144
--- | ---
145145
[Animated Fox](../examples/animation/animated_fox.rs) | Plays an animation from a skinned glTF
146146
[Animated Transform](../examples/animation/animated_transform.rs) | Create and play an animation defined by code that operates on the `Transform` component
147+
[Cubic Curve](../examples/animation/cubic_curve.rs) | Bezier curve example showing a cube following a cubic curve
147148
[Custom Skinned Mesh](../examples/animation/custom_skinned_mesh.rs) | Skinned mesh example with mesh and joints data defined in code
148149
[glTF Skinned Mesh](../examples/animation/gltf_skinned_mesh.rs) | Skinned mesh example with mesh and joints data loaded from a glTF file
149150

examples/animation/cubic_curve.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//! Demonstrates how to work with Cubic curves.
2+
3+
use bevy::{
4+
math::{cubic_splines::CubicCurve, vec3},
5+
prelude::*,
6+
};
7+
8+
#[derive(Component)]
9+
pub struct Curve(CubicCurve<Vec3>);
10+
11+
fn main() {
12+
App::new()
13+
.add_plugins(DefaultPlugins)
14+
.add_systems(Startup, setup)
15+
.add_systems(Update, animate_cube)
16+
.run();
17+
}
18+
19+
fn setup(
20+
mut commands: Commands,
21+
mut meshes: ResMut<Assets<Mesh>>,
22+
mut materials: ResMut<Assets<StandardMaterial>>,
23+
) {
24+
// Define your control points
25+
// These points will define the curve
26+
// You can learn more about bezier curves here
27+
// https://en.wikipedia.org/wiki/B%C3%A9zier_curve
28+
let points = [[
29+
vec3(-6., 2., 0.),
30+
vec3(12., 8., 0.),
31+
vec3(-12., 8., 0.),
32+
vec3(6., 2., 0.),
33+
]];
34+
35+
// Make a CubicCurve
36+
let bezier = Bezier::new(points).to_curve();
37+
38+
// Spawning a cube to experiment on
39+
commands.spawn((
40+
PbrBundle {
41+
mesh: meshes.add(shape::Cube::default().into()),
42+
material: materials.add(Color::ORANGE.into()),
43+
transform: Transform::from_translation(points[0][0]),
44+
..default()
45+
},
46+
Curve(bezier),
47+
));
48+
49+
// Some light to see something
50+
commands.spawn(PointLightBundle {
51+
point_light: PointLight {
52+
intensity: 9000.,
53+
range: 100.,
54+
shadows_enabled: true,
55+
..default()
56+
},
57+
transform: Transform::from_xyz(8., 16., 8.),
58+
..default()
59+
});
60+
61+
// ground plane
62+
commands.spawn(PbrBundle {
63+
mesh: meshes.add(shape::Plane::from_size(50.).into()),
64+
material: materials.add(Color::SILVER.into()),
65+
..default()
66+
});
67+
68+
// The camera
69+
commands.spawn(Camera3dBundle {
70+
transform: Transform::from_xyz(0., 6., 12.).looking_at(Vec3::new(0., 3., 0.), Vec3::Y),
71+
..default()
72+
});
73+
}
74+
75+
pub fn animate_cube(
76+
time: Res<Time>,
77+
mut query: Query<(&mut Transform, &Curve)>,
78+
mut gizmos: Gizmos,
79+
) {
80+
let t = (time.elapsed_seconds().sin() + 1.) / 2.;
81+
82+
for (mut transform, cubic_curve) in &mut query {
83+
// Draw the curve
84+
gizmos.linestrip(cubic_curve.0.iter_positions(50), Color::WHITE);
85+
// position takes a point from the curve where 0 is the initial point
86+
// and 1 is the last point
87+
transform.translation = cubic_curve.0.position(t);
88+
}
89+
}

0 commit comments

Comments
 (0)