Skip to content

Commit 37cc8ea

Browse files
Add axes_2d gizmo. (#12334)
# Objective This PR addresses #12222 (Fixes #12222). Simple addition to add a 2D axes gizmo. ## Solution - Add a new method axes_2d which takes a transform and a case length and then draws two arrows in the XY plane. The only thing I'm not sure about here is taking a 3D transform as an argument. It says in the transform comments that for 2D the z-axis is used for ordering, so I figured I'd keep it that way? --- ## Changelog - Add method axes_2d. - Update arrow_2d to also calculate the tip length depending on arrow length as in arrow. - Add axes_2d to examples 2d_gizmos. --------- Co-authored-by: Ben Lambert <[email protected]>
1 parent 1b6bc2c commit 37cc8ea

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

crates/bevy_gizmos/src/arrows.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy_color::{
88
palettes::basic::{BLUE, GREEN, RED},
99
Color,
1010
};
11-
use bevy_math::{Quat, Vec2, Vec3};
11+
use bevy_math::{Quat, Vec2, Vec3, Vec3Swizzles};
1212
use bevy_transform::TransformPoint;
1313

1414
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
@@ -201,4 +201,35 @@ where
201201
self.arrow(start, end_y, GREEN);
202202
self.arrow(start, end_z, BLUE);
203203
}
204+
205+
/// Draw a set of axes local to the given transform (`transform`), with length scaled by a factor
206+
/// of `base_length`.
207+
///
208+
/// This should be called for each frame the axes need to be rendered.
209+
///
210+
/// # Example
211+
/// ```
212+
/// # use bevy_gizmos::prelude::*;
213+
/// # use bevy_ecs::prelude::*;
214+
/// # use bevy_transform::components::Transform;
215+
/// # #[derive(Component)]
216+
/// # struct AxesComponent;
217+
/// fn draw_axes_2d(
218+
/// mut gizmos: Gizmos,
219+
/// query: Query<&Transform, With<AxesComponent>>,
220+
/// ) {
221+
/// for &transform in &query {
222+
/// gizmos.axes_2d(transform, 1.);
223+
/// }
224+
/// }
225+
/// # bevy_ecs::system::assert_is_system(draw_axes_2d);
226+
/// ```
227+
pub fn axes_2d(&mut self, transform: impl TransformPoint, base_length: f32) {
228+
let start = transform.transform_point(Vec3::ZERO);
229+
let end_x = transform.transform_point(base_length * Vec3::X);
230+
let end_y = transform.transform_point(base_length * Vec3::Y);
231+
232+
self.arrow_2d(start.xy(), end_x.xy(), RED);
233+
self.arrow_2d(start.xy(), end_y.xy(), GREEN);
234+
}
204235
}

0 commit comments

Comments
 (0)