Skip to content

Commit ab300d0

Browse files
Gizmo Arrows (#10550)
## Objective - Add an arrow gizmo as suggested by #9400 ## Solution (excuse my Protomen music) https://github.com/bevyengine/bevy/assets/14184826/192adf24-079f-4a4b-a17b-091e892974ec Wasn't horribly hard when i remembered i can change coordinate systems whenever I want. Gave them four tips (as suggested by @alice-i-cecile in discord) instead of trying to decide what direction the tips should point. Made the tip length default to 1/10 of the arrow's length, which looked good enough to me. Hard-coded the angle from the body to the tips to 45 degrees. ## Still TODO - [x] actual doc comments - [x] doctests - [x] `ArrowBuilder.with_tip_length()` --- ## Changelog - Added `gizmos.arrow()` and `gizmos.arrow_2d()` - Added arrows to `2d_gizmos` and `3d_gizmos` examples ## Migration Guide N/A --------- Co-authored-by: Nicola Papale <[email protected]>
1 parent 8c15713 commit ab300d0

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

crates/bevy_gizmos/src/arrows.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

crates/bevy_gizmos/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//!
1717
//! See the documentation on [`Gizmos`] for more examples.
1818
19+
mod arrows;
1920
pub mod gizmos;
2021

2122
#[cfg(feature = "bevy_sprite")]

examples/2d/2d_gizmos.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ fn system(mut gizmos: Gizmos, time: Res<Time>) {
5353
// Arcs default amount of segments is linearly interpolated between
5454
// 1 and 32, using the arc length as scalar.
5555
gizmos.arc_2d(Vec2::ZERO, sin / 10., PI / 2., 350., Color::ORANGE_RED);
56+
57+
gizmos.arrow_2d(
58+
Vec2::ZERO,
59+
Vec2::from_angle(sin / -10. + PI / 2.) * 50.,
60+
Color::YELLOW,
61+
);
5662
}
5763

5864
fn update_config(mut config: ResMut<GizmoConfig>, keyboard: Res<Input<KeyCode>>, time: Res<Time>) {

examples/3d/3d_gizmos.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ fn system(mut gizmos: Gizmos, time: Res<Time>) {
9696
gizmos
9797
.sphere(Vec3::ZERO, Quat::IDENTITY, 3.2, Color::BLACK)
9898
.circle_segments(64);
99+
100+
gizmos.arrow(Vec3::ZERO, Vec3::ONE * 1.5, Color::YELLOW);
99101
}
100102

101103
fn rotate_camera(mut query: Query<&mut Transform, With<Camera>>, time: Res<Time>) {

0 commit comments

Comments
 (0)