-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cce340f
Adding a bezier curve example
Kjolnyr 080f701
fixing ci
Kjolnyr 948f441
Adding a line showing the cuve
Kjolnyr 81a4425
Typo in comment
Kjolnyr adefa61
changed crappy ascii graph to wiki link
Kjolnyr 9918277
Changed name to cubic_curve.rs
Kjolnyr 7e9f6de
points variable collapse
Kjolnyr 15022a9
changing variable step to t
Kjolnyr b8ec131
fix small issue with point variable collapse commit
Kjolnyr 93e79ba
Merge branch 'bevyengine:main' into bezier-example
Kjolnyr d12dd36
Fix CI
Kjolnyr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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 | ||
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<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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this comment to avoid duplicating the docs. I'd also prefer using
t
instead ofstep
, as it is the standard input into a parametric function, and is what is used in the docs: https://docs.rs/bevy/latest/bevy/math/cubic_splines/struct.CubicCurve.html#method.positionAs above, this is a pretty minor nit, and could be ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point for
t
instead ofstep
, however, I'd keep the comment even though it's duplicating the docs to avoid any confusion the user would have.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I very much like keeping the duplicative comment here :) The information is important and relevant here.