|
| 1 | +//! Additional Gizmo Functions -- Arrows |
| 2 | +
|
| 3 | +use crate::prelude::Gizmos; |
| 4 | +use bevy_math::{Quat, Vec2, Vec3}; |
| 5 | +use bevy_render::color::Color; |
| 6 | + |
| 7 | +pub struct ArrowBuilder<'a, 's> { |
| 8 | + gizmos: &'a mut Gizmos<'s>, |
| 9 | + start: Vec3, |
| 10 | + end: Vec3, |
| 11 | + color: Color, |
| 12 | + tip_length: f32, |
| 13 | +} |
| 14 | + |
| 15 | +/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`] |
| 16 | +impl ArrowBuilder<'_, '_> { |
| 17 | + /// Change the length of the tips to be `length`. |
| 18 | + /// The default tip length is [length of the arrow]/10. |
| 19 | + /// |
| 20 | + /// # Example |
| 21 | + /// ``` |
| 22 | + /// # use bevy_gizmos::prelude::*; |
| 23 | + /// # use bevy_render::prelude::*; |
| 24 | + /// # use bevy_math::prelude::*; |
| 25 | + /// fn system(mut gizmos: Gizmos) { |
| 26 | + /// gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::GREEN) |
| 27 | + /// .with_tip_length(3.); |
| 28 | + /// } |
| 29 | + /// # bevy_ecs::system::assert_is_system(system); |
| 30 | + /// ``` |
| 31 | + #[doc(alias = "arrow_head_length")] |
| 32 | + pub fn with_tip_length(&mut self, length: f32) { |
| 33 | + self.tip_length = length; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl Drop for ArrowBuilder<'_, '_> { |
| 38 | + /// Draws the arrow, by drawing lines with the stored [`Gizmos`] |
| 39 | + fn drop(&mut self) { |
| 40 | + // first, draw the body of the arrow |
| 41 | + self.gizmos.line(self.start, self.end, self.color); |
| 42 | + // now the hard part is to draw the head in a sensible way |
| 43 | + // put us in a coordinate system where the arrow is pointing towards +x and ends at the origin |
| 44 | + let pointing = (self.end - self.start).normalize(); |
| 45 | + let rotation = Quat::from_rotation_arc(Vec3::X, pointing); |
| 46 | + let tips = [ |
| 47 | + Vec3::new(-1., 1., 0.), |
| 48 | + Vec3::new(-1., 0., 1.), |
| 49 | + Vec3::new(-1., -1., 0.), |
| 50 | + Vec3::new(-1., 0., -1.), |
| 51 | + ]; |
| 52 | + // - extend the vectors so their length is `tip_length` |
| 53 | + // - rotate the world so +x is facing in the same direction as the arrow |
| 54 | + // - translate over to the tip of the arrow |
| 55 | + let tips = tips.map(|v| rotation * (v.normalize() * self.tip_length) + self.end); |
| 56 | + for v in tips { |
| 57 | + // then actually draw the tips |
| 58 | + self.gizmos.line(self.end, v, self.color); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<'s> Gizmos<'s> { |
| 64 | + /// Draw an arrow in 3D, from `start` to `end`. Has four tips for convienent viewing from any direction. |
| 65 | + /// |
| 66 | + /// This should be called for each frame the arrow needs to be rendered. |
| 67 | + /// |
| 68 | + /// # Example |
| 69 | + /// ``` |
| 70 | + /// # use bevy_gizmos::prelude::*; |
| 71 | + /// # use bevy_render::prelude::*; |
| 72 | + /// # use bevy_math::prelude::*; |
| 73 | + /// fn system(mut gizmos: Gizmos) { |
| 74 | + /// gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::GREEN); |
| 75 | + /// } |
| 76 | + /// # bevy_ecs::system::assert_is_system(system); |
| 77 | + /// ``` |
| 78 | + pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's> { |
| 79 | + let length = (end - start).length(); |
| 80 | + ArrowBuilder { |
| 81 | + gizmos: self, |
| 82 | + start, |
| 83 | + end, |
| 84 | + color, |
| 85 | + tip_length: length / 10., |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Draw an arrow in 2D (on the xy plane), from `start` to `end`. |
| 90 | + /// |
| 91 | + /// This should be called for each frame the arrow needs to be rendered. |
| 92 | + /// |
| 93 | + /// # Example |
| 94 | + /// ``` |
| 95 | + /// # use bevy_gizmos::prelude::*; |
| 96 | + /// # use bevy_render::prelude::*; |
| 97 | + /// # use bevy_math::prelude::*; |
| 98 | + /// fn system(mut gizmos: Gizmos) { |
| 99 | + /// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, Color::GREEN); |
| 100 | + /// } |
| 101 | + /// # bevy_ecs::system::assert_is_system(system); |
| 102 | + /// ``` |
| 103 | + pub fn arrow_2d(&mut self, start: Vec2, end: Vec2, color: Color) -> ArrowBuilder<'_, 's> { |
| 104 | + self.arrow(start.extend(0.), end.extend(0.), color) |
| 105 | + } |
| 106 | +} |
0 commit comments