Skip to content

Commit 6b0e3fa

Browse files
pablo-luaafonsolagePablo Reinhardt
authored
Add double end arrow to gizmos (#11890)
# Objective - Implement double ended arrows, suggestion of #9400 ## Solution - Creation of new field and method to the `ArrowBuilder` --- ## Changelog ### Added - New field `ArrowBuilder::double_ended` - New method `ArrowBuilder::with_double_end` to redefine the double_ended field ## Additional I added this in the 3d_gizmos example, that's the result: ![image](https://github.com/bevyengine/bevy/assets/126117294/2f8a93eb-4952-401a-b600-b1454cf898a9) I added this arrow in the 2d_gizmos example too: ![image](https://github.com/bevyengine/bevy/assets/126117294/c46b4871-8acf-4711-9ca8-c2df36c0464b) --------- Co-authored-by: Afonso Lage <[email protected]> Co-authored-by: Pablo Reinhardt <[email protected]>
1 parent 1141e73 commit 6b0e3fa

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

crates/bevy_gizmos/src/arrows.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct ArrowBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
1717
start: Vec3,
1818
end: Vec3,
1919
color: Color,
20+
double_ended: bool,
2021
tip_length: f32,
2122
}
2223

@@ -41,6 +42,13 @@ impl<T: GizmoConfigGroup> ArrowBuilder<'_, '_, '_, T> {
4142
self.tip_length = length;
4243
self
4344
}
45+
46+
/// Adds another tip to the arrow, appended in the start point.
47+
/// the default is only one tip at the end point.
48+
pub fn with_double_end(mut self) -> Self {
49+
self.double_ended = true;
50+
self
51+
}
4452
}
4553

4654
impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> {
@@ -53,8 +61,8 @@ impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> {
5361
self.gizmos.line(self.start, self.end, self.color);
5462
// now the hard part is to draw the head in a sensible way
5563
// put us in a coordinate system where the arrow is pointing towards +x and ends at the origin
56-
let pointing = (self.end - self.start).normalize();
57-
let rotation = Quat::from_rotation_arc(Vec3::X, pointing);
64+
let pointing_end = (self.end - self.start).normalize();
65+
let rotation_end = Quat::from_rotation_arc(Vec3::X, pointing_end);
5866
let tips = [
5967
Vec3::new(-1., 1., 0.),
6068
Vec3::new(-1., 0., 1.),
@@ -64,11 +72,21 @@ impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> {
6472
// - extend the vectors so their length is `tip_length`
6573
// - rotate the world so +x is facing in the same direction as the arrow
6674
// - translate over to the tip of the arrow
67-
let tips = tips.map(|v| rotation * (v.normalize() * self.tip_length) + self.end);
68-
for v in tips {
75+
let tips_end = tips.map(|v| rotation_end * (v.normalize() * self.tip_length) + self.end);
76+
for v in tips_end {
6977
// then actually draw the tips
7078
self.gizmos.line(self.end, v, self.color);
7179
}
80+
if self.double_ended {
81+
let pointing_start = (self.start - self.end).normalize();
82+
let rotation_start = Quat::from_rotation_arc(Vec3::X, pointing_start);
83+
let tips_start =
84+
tips.map(|v| rotation_start * (v.normalize() * self.tip_length) + self.start);
85+
for v in tips_start {
86+
// draw the start points tips
87+
self.gizmos.line(self.start, v, self.color);
88+
}
89+
}
7290
}
7391
}
7492

@@ -100,6 +118,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
100118
start,
101119
end,
102120
color: color.into(),
121+
double_ended: false,
103122
tip_length: length / 10.,
104123
}
105124
}

examples/gizmos/2d_gizmos.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ fn draw_example_collection(
8989
Vec2::from_angle(sin / -10. + PI / 2.) * 50.,
9090
YELLOW,
9191
);
92+
93+
// You can create more complex arrows using the arrow builder.
94+
gizmos
95+
.arrow_2d(Vec2::ZERO, Vec2::from_angle(sin / -10.) * 50., GREEN)
96+
.with_double_end()
97+
.with_tip_length(10.);
9298
}
9399

94100
fn update_config(

examples/gizmos/3d_gizmos.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ fn draw_example_collection(
137137
.circle_segments(64);
138138

139139
gizmos.arrow(Vec3::ZERO, Vec3::ONE * 1.5, YELLOW);
140+
141+
// You can create more complex arrows using the arrow builder.
142+
gizmos
143+
.arrow(Vec3::new(2., 0., 2.), Vec3::new(2., 2., 2.), ORANGE_RED)
144+
.with_double_end()
145+
.with_tip_length(0.5);
140146
}
141147

142148
fn update_config(

0 commit comments

Comments
 (0)