Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add axes_2d gizmo. #12334

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_gizmos/Cargo.toml
Expand Up @@ -14,8 +14,8 @@ webgpu = []

[dependencies]
# Bevy
bevy_pbr = { path = "../bevy_pbr", version = "0.14.0-dev", optional = true }
bevy_sprite = { path = "../bevy_sprite", version = "0.14.0-dev", optional = true }
bevy_pbr = { path = "../bevy_pbr", version = "0.14.0-dev", optional = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should avoid changing this

bevy_app = { path = "../bevy_app", version = "0.14.0-dev" }
bevy_color = { path = "../bevy_color", version = "0.14.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" }
Expand Down
42 changes: 41 additions & 1 deletion crates/bevy_gizmos/src/arrows.rs
Expand Up @@ -125,7 +125,14 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
end: Vec2,
color: impl Into<Color>,
) -> ArrowBuilder<'_, 'w, 's, T> {
self.arrow(start.extend(0.), end.extend(0.), color)
let length = (end - start).length();
ArrowBuilder {
gizmos: self,
start: start.extend(0.),
end: end.extend(0.),
color: color.into(),
tip_length: length / 10.,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to be consistent with the 3d arrow drawing method, but I realize now that was not a smart change.

}
}

Expand Down Expand Up @@ -163,3 +170,36 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
self.arrow(start, end_z, BLUE);
}
}

impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
Copy link
Contributor

@pablo-lua pablo-lua Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should adress both methods in the same impl if possible and move this to the impl above this.

/// 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.into(), end_x.into(), RED);
self.arrow_2d(start.into(), end_y.into(), GREEN);
Copy link
Member

@mockersf mockersf Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.arrow_2d(start.into(), end_x.into(), RED);
self.arrow_2d(start.into(), end_y.into(), GREEN);
self.arrow_2d(start.xy(), end_x.xy(), RED);
self.arrow_2d(start.xy(), end_y.xy(), GREEN);

and you'll need to import bevy_math::Vec3Swizzles

}
}
2 changes: 2 additions & 0 deletions examples/gizmos/2d_gizmos.rs
Expand Up @@ -87,6 +87,8 @@ fn draw_example_collection(
Vec2::from_angle(sin / -10. + PI / 2.) * 50.,
YELLOW,
);

gizmos.axes_2d(Transform::from_translation(Vec3::new(sin, 0.0, 0.0)), 25.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example is already quite crowded. this will also put it on the grid gizmo already present, making it hard to read

}

fn update_config(
Expand Down