From 9bb474d2ea818fdf74a7080ef958dd50fd285bec Mon Sep 17 00:00:00 2001 From: Connor King Date: Mon, 13 Nov 2023 21:59:34 -0500 Subject: [PATCH 01/17] Add gizmo arrows! --- crates/bevy_gizmos/src/arrows.rs | 57 ++++++++++++++++++++++++++++++++ crates/bevy_gizmos/src/lib.rs | 1 + 2 files changed, 58 insertions(+) create mode 100644 crates/bevy_gizmos/src/arrows.rs diff --git a/crates/bevy_gizmos/src/arrows.rs b/crates/bevy_gizmos/src/arrows.rs new file mode 100644 index 0000000000000..3019f4cc98371 --- /dev/null +++ b/crates/bevy_gizmos/src/arrows.rs @@ -0,0 +1,57 @@ +//! 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, +} + +impl Drop for ArrowBuilder<'_, '_> { + fn drop(&mut self) { + // draw the body of the arrow + 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 up and ends at the origin + 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); + for v in tips { + self.gizmos.line(self.end, v, self.color); + } + } +} + +impl<'s> Gizmos<'s> { + /// draw an arrow. + pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's> { + // self.line_2d(start, end, color); + let length = (end - start).length(); + ArrowBuilder { + gizmos: self, + start, + end, + color, + tip_length: length / 10., + } + } + + /// draw an arrow in 2d space, on the x-y plane. + pub fn arrow_2d(&mut self, start: Vec2, end: Vec2, color: Color) -> ArrowBuilder<'_, 's> { + self.arrow(start.extend(0.), end.extend(0.), color) + } +} diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 333d8e2e9bbc7..5ad9d8934db06 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -16,6 +16,7 @@ //! //! See the documentation on [`Gizmos`] for more examples. +mod arrows; pub mod gizmos; #[cfg(feature = "bevy_sprite")] From 839e3bfcf24956540e504db4b673ca068cd84cbb Mon Sep 17 00:00:00 2001 From: Connor King Date: Mon, 13 Nov 2023 22:00:34 -0500 Subject: [PATCH 02/17] add an arrow to to 3d_gizmos example --- examples/3d/3d_gizmos.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/3d/3d_gizmos.rs b/examples/3d/3d_gizmos.rs index 8f6e1cb2d1373..14f4b3946450f 100644 --- a/examples/3d/3d_gizmos.rs +++ b/examples/3d/3d_gizmos.rs @@ -96,6 +96,8 @@ fn system(mut gizmos: Gizmos, time: Res