-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Gizmo Arrows #10550
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
Merged
Merged
Gizmo Arrows #10550
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9bb474d
Add gizmo arrows!
SIGSTACKFAULT 839e3bf
add an arrow to to 3d_gizmos example
SIGSTACKFAULT 05057e1
add an arrow to 2d example
SIGSTACKFAULT 38a9462
add docs and doctests to arrow and arrow_2d
SIGSTACKFAULT b145755
update comment inside ArrowBuilder.drop
SIGSTACKFAULT a1147cb
add ArrowBuilder.with_tip_length and it's docs
SIGSTACKFAULT 2a14edd
add doc comment to ArrowBuilder
SIGSTACKFAULT 26d0d31
finish writing doc comment on ArrowBuilder.drop
SIGSTACKFAULT 75e7752
add arrow_head_length alias to with_tip_length
SIGSTACKFAULT a93dc95
make the comment on ArrowBuilder.drop a bit more professional
SIGSTACKFAULT dba55c8
capitalize "Example" in three places
SIGSTACKFAULT d177593
Remove commented out dev code
SIGSTACKFAULT d1ac848
Construct `tips` in a smarter way
SIGSTACKFAULT e1bde5a
format arrows.rs
SIGSTACKFAULT 9bc4cff
update doc comment on ArrowBuilder.with_tip_length
SIGSTACKFAULT a5e68cc
Adjust comments inside ArrowBuilder.drop
SIGSTACKFAULT 583de5e
normalize the tips before multiplying by `tip_length`
SIGSTACKFAULT 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 hidden or 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,106 @@ | ||
//! Additional Gizmo Functions -- Arrows | ||
|
||
use crate::prelude::Gizmos; | ||
use bevy_math::{Quat, Vec2, Vec3}; | ||
use bevy_render::color::Color; | ||
|
||
pub struct ArrowBuilder<'a, 's> { | ||
gizmos: &'a mut Gizmos<'s>, | ||
start: Vec3, | ||
end: Vec3, | ||
color: Color, | ||
tip_length: f32, | ||
} | ||
|
||
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`] | ||
impl ArrowBuilder<'_, '_> { | ||
/// Change the length of the tips to be `length`. | ||
/// The default tip length is [length of the arrow]/10, which is sufficient for most applications. | ||
/// | ||
/// # example | ||
SIGSTACKFAULT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ``` | ||
/// # use bevy_gizmos::prelude::*; | ||
/// # use bevy_render::prelude::*; | ||
/// # use bevy_math::prelude::*; | ||
/// fn system(mut gizmos: Gizmos) { | ||
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::GREEN) | ||
/// .with_tip_length(3.); | ||
/// } | ||
/// # bevy_ecs::system::assert_is_system(system); | ||
/// ``` | ||
pub fn with_tip_length(&mut self, length: f32) { | ||
SIGSTACKFAULT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.tip_length = length; | ||
} | ||
} | ||
|
||
impl Drop for ArrowBuilder<'_, '_> { | ||
/// actually draws the arrow, by drawing lines with the stored [`Gizmos`] | ||
SIGSTACKFAULT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn drop(&mut self) { | ||
// draw the body of the arrow | ||
alice-i-cecile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.gizmos.line(self.start, self.end, self.color); | ||
// now the hard part is to draw the head in a sensible way | ||
// put us in a coordinate system where the arrow is pointing towards +x and ends at the origin | ||
// then transform it so we're at the end of the arrow facing the right direction | ||
let pointing = (self.end - self.start).normalize(); | ||
let rotation = Quat::from_rotation_arc(Vec3::X, pointing); | ||
let tips = [(1, 0), (0, 1), (-1, 0), (0, -1)] | ||
.into_iter() | ||
.map(|(y, z)| Vec3 { | ||
x: -1., | ||
y: y as f32, | ||
z: z as f32, | ||
}) | ||
.map(|v| v * self.tip_length) | ||
.map(|v| rotation.mul_vec3(v)) | ||
.map(|v| v + self.end); | ||
SIGSTACKFAULT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for v in tips { | ||
self.gizmos.line(self.end, v, self.color); | ||
} | ||
alice-i-cecile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
impl<'s> Gizmos<'s> { | ||
/// Draw an arrow in 3D, from `start` to `end`. Has four tips for convienent viewing from any direction. | ||
/// | ||
/// This should be called for each frame the arrow needs to be rendered. | ||
/// | ||
/// # example | ||
/// ``` | ||
/// # use bevy_gizmos::prelude::*; | ||
/// # use bevy_render::prelude::*; | ||
/// # use bevy_math::prelude::*; | ||
/// fn system(mut gizmos: Gizmos) { | ||
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::GREEN); | ||
/// } | ||
/// # bevy_ecs::system::assert_is_system(system); | ||
/// ``` | ||
pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's> { | ||
// self.line_2d(start, end, color); | ||
SIGSTACKFAULT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let length = (end - start).length(); | ||
ArrowBuilder { | ||
gizmos: self, | ||
start, | ||
end, | ||
color, | ||
tip_length: length / 10., | ||
} | ||
} | ||
|
||
/// Draw an arrow in 2D (on the xy plane), from `start` to `end`. | ||
/// | ||
/// This should be called for each frame the arrow needs to be rendered. | ||
/// | ||
/// # example | ||
/// ``` | ||
/// # use bevy_gizmos::prelude::*; | ||
/// # use bevy_render::prelude::*; | ||
/// # use bevy_math::prelude::*; | ||
/// fn system(mut gizmos: Gizmos) { | ||
/// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, Color::GREEN); | ||
/// } | ||
/// # bevy_ecs::system::assert_is_system(system); | ||
/// ``` | ||
pub fn arrow_2d(&mut self, start: Vec2, end: Vec2, color: Color) -> ArrowBuilder<'_, 's> { | ||
self.arrow(start.extend(0.), end.extend(0.), color) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.