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 <bennett-spencer.lambert@pierer-innovation.com>
This commit is contained in:
Bennett Lambert 2024-06-04 02:02:38 +02:00 committed by GitHub
parent 1b6bc2c240
commit 37cc8ea33b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,7 +8,7 @@ use bevy_color::{
palettes::basic::{BLUE, GREEN, RED},
Color,
};
use bevy_math::{Quat, Vec2, Vec3};
use bevy_math::{Quat, Vec2, Vec3, Vec3Swizzles};
use bevy_transform::TransformPoint;
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
@ -201,4 +201,35 @@ where
self.arrow(start, end_y, GREEN);
self.arrow(start, end_z, BLUE);
}
/// Draw a set of axes local to the given transform (`transform`), with length scaled by a factor
/// of `base_length`.
///
/// This should be called for each frame the axes need to be rendered.
///
/// # Example
/// ```
/// # use bevy_gizmos::prelude::*;
/// # use bevy_ecs::prelude::*;
/// # use bevy_transform::components::Transform;
/// # #[derive(Component)]
/// # struct AxesComponent;
/// fn draw_axes_2d(
/// mut gizmos: Gizmos,
/// query: Query<&Transform, With<AxesComponent>>,
/// ) {
/// for &transform in &query {
/// gizmos.axes_2d(transform, 1.);
/// }
/// }
/// # bevy_ecs::system::assert_is_system(draw_axes_2d);
/// ```
pub fn axes_2d(&mut self, transform: impl TransformPoint, base_length: f32) {
let start = transform.transform_point(Vec3::ZERO);
let end_x = transform.transform_point(base_length * Vec3::X);
let end_y = transform.transform_point(base_length * Vec3::Y);
self.arrow_2d(start.xy(), end_x.xy(), RED);
self.arrow_2d(start.xy(), end_y.xy(), GREEN);
}
}