Skip to content

Add coordinate axes gizmo #12211

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

Merged
merged 6 commits into from
Mar 1, 2024
Merged

Conversation

mweatherley
Copy link
Contributor

@mweatherley mweatherley commented Feb 29, 2024

Objective

Solution

A new method, Gizmos::axes, takes a Transform* as input and displays the standard coordinate axes, transformed according to it; its signature looks like this:

pub fn axes(&mut self, transform: into TransformPoint, base_length: f32) { //... }

If my carefully placed asterisks hadn't already tipped you off, the argument here is not actually a Transform but instead anything which implements TransformPoint, which allows it to work also with GlobalTransform (and also Mat4 and Affine3A, if the user happens to be hand-rolling transformations in some way).

The base_length parameter is a scaling factor applied to the coordinate vectors before the transformation takes place; in other words, the caller can use this to help size the coordinate axes appropriately for the entity that they are attached to.

An example invocation of this method looks something like this:

fn draw_axes_system(
    mut gizmos: Gizmos,
    query: Query<&Transform, With<MyMarkerComponent>>,
) {
    for &transform in &query {
        gizmos.axes(transform, 2.);
    }
}

The result is the three coordinate axes, X, Y, Z (colored red, green, and blue, respectively), drawn onto the entity:
Screenshot 2024-02-29 at 2 41 45 PM

Note that, if scaling was applied as part of the given transformation, it shows up in scaling on the axes as well:
Screenshot 2024-02-29 at 2 43 53 PM


Changelog

  • Added Gizmos::axes in bevy_gizmos/src/arrows.rs
  • Fixed a minor issue with ArrowBuilder::with_tip_length not correctly implementing builder style (no external impact)

Discussion

Design considerations

I feel pretty strongly that having no default length scale is for the best, at least for the time being, since it's very easy for the length scale to be too small, leading to the axes being hidden inside the body of the object they are associated with. That is, if the API instead looked like this:

gizmos.axes(transform); // or
gizmos.axes(transform).with_length_scale(3.0);

then I think it's a reasonable expectation that the first thing would "just work" for most applications, and it wouldn't, which would be kind of a footgun.

Future steps

There are a few directions that this might expand in the future:

  1. Introduce additional options via the standard builder pattern; i.e. introducing AxesBuilder<T: TransformPoint> so that people can configure the axis colors, normalize all axes to a fixed length independent of scale deformations, etc.

  2. Fold this functionality into a plugin (like AabbGizmoPlugin) so that the functionality becomes more-or-less automatic based on certain fixed marker components. This wouldn't be very hard to implement, and it has the benefit of making the axes more frictionless to use. Furthermore, if we coupled this to the AABB functionality we already have, we could also ensure that the plugin automatically sizes the axes (by coupling their size to the dimensions of the AABB, for example).

  3. Implement something similar for 2d. Honestly, I have no idea if this is desired/useful, but I could probably just implement it in this PR if that's the case.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Editor Graphical tools to make Bevy games A-Gizmos Visual editor and debug gizmos labels Feb 29, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I'd prefer to have an example for this, but I'm fine to punt that to follow-up :) At some point higher-level gizmos may make more sense in bevy_dev_tools, but this is a good addition.

I really like your argument for forcing users to supply base_length, and suggestion to integrate with AABBs later.

@mweatherley
Copy link
Contributor Author

I'd prefer to have an example for this, but I'm fine to punt that to follow-up :)

Cool! I wasn't sure if this merited an example, but I'd be happy to produce one in a follow-up.

At some point higher-level gizmos may make more sense in bevy_dev_tools, but this is a good addition.

I was wondering about this, but I'm pretty out of the loop with the whole bevy_dev_tools discussion, so I'm glad you brought it up!

Copy link
Contributor

@pablo-lua pablo-lua left a comment

Choose a reason for hiding this comment

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

Good changes. I think maybe we can do some kind of builder with this on the future (probably when we move this to bevy_dev_tools)

Comment on lines +40 to +42
pub fn with_tip_length(mut self, length: f32) -> Self {
self.tip_length = length;
self
Copy link
Contributor

Choose a reason for hiding this comment

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

this is better for builders! Good to have this change.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it M-Needs-Release-Note Work that should be called out in the blog due to impact labels Feb 29, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 29, 2024
Merged via the queue into bevyengine:main with commit f8f8bdd Mar 1, 2024
@mweatherley mweatherley deleted the gizmo-axes branch March 1, 2024 00:23
@Jondolf Jondolf mentioned this pull request Mar 1, 2024
@nicopap nicopap mentioned this pull request Mar 4, 2024
57 tasks
github-merge-queue bot pushed a commit that referenced this pull request Mar 4, 2024
# Objective

- Follow-up to #12211 
- Introduces an example project that demonstrates the implementation and
behavior of `Gizmos::axes` for an entity with a `Transform` component.

## Solution

In order to demonstrate how `Gizmo::axes` can be used and behaves in
practice, we introduce an example of a simple scene containing a pair of
cuboids locked in a grotesque, inscrutable dance: the two are repeatedly
given random `Transform`s which they interpolate to, showing how the
axes move with objects as they translate, rotate, and scale.

<img width="1023" alt="Screenshot 2024-03-04 at 1 16 33 PM"
src="https://github.com/bevyengine/bevy/assets/2975848/c1ff4794-6722-491c-8522-f59801645139">



On the implementation side, we demonstrate how to draw axes for
entities, automatically sizing them according to their bounding boxes
(so that the axes will be visible):
````rust
fn draw_axes(mut gizmos: Gizmos, query: Query<(&Transform, &Aabb), With<ShowAxes>>) {
    for (&transform, &aabb) in &query {
        let length = aabb.half_extents.length();
        gizmos.axes(transform, length);
    }
}
````

---

## Changelog

- Created examples/gizmos/axes.rs.
- Added 'axes' example to Cargo.toml.
spectria-limina pushed a commit to spectria-limina/bevy that referenced this pull request Mar 9, 2024
# Objective

- We introduce a gizmo that displays coordinate axes relative to a
Transform*, primarily for debugging purposes.
- See bevyengine#9400 

## Solution

A new method, `Gizmos::axes`, takes a `Transform`* as input and displays
the standard coordinate axes, transformed according to it; its signature
looks like this:
````rust
pub fn axes(&mut self, transform: into TransformPoint, base_length: f32) { //... }
````
If my carefully placed asterisks hadn't already tipped you off, the
argument here is not actually a `Transform` but instead anything which
implements `TransformPoint`, which allows it to work also with
`GlobalTransform` (and also `Mat4` and `Affine3A`, if the user happens
to be hand-rolling transformations in some way).

The `base_length` parameter is a scaling factor applied to the
coordinate vectors before the transformation takes place; in other
words, the caller can use this to help size the coordinate axes
appropriately for the entity that they are attached to.

An example invocation of this method looks something like this:
````rust
fn draw_axes_system(
    mut gizmos: Gizmos,
    query: Query<&Transform, With<MyMarkerComponent>>,
) {
    for &transform in &query {
        gizmos.axes(transform, 2.);
    }
}
````

The result is the three coordinate axes, X, Y, Z (colored red, green,
and blue, respectively), drawn onto the entity:
<img width="206" alt="Screenshot 2024-02-29 at 2 41 45 PM"
src="https://github.com/bevyengine/bevy/assets/2975848/789d1703-29ae-4295-80ab-b87459cf8037">

Note that, if scaling was applied as part of the given transformation,
it shows up in scaling on the axes as well:
<img width="377" alt="Screenshot 2024-02-29 at 2 43 53 PM"
src="https://github.com/bevyengine/bevy/assets/2975848/6dc1caf4-8b3e-47f7-a86a-8906d870fa72">

---

## Changelog

- Added `Gizmos::axes` in bevy_gizmos/src/arrows.rs
- Fixed a minor issue with `ArrowBuilder::with_tip_length` not correctly
implementing builder style (no external impact)

---

## Discussion

### Design considerations
I feel pretty strongly that having no default length scale is for the
best, at least for the time being, since it's very easy for the length
scale to be too small, leading to the axes being hidden inside the body
of the object they are associated with. That is, if the API instead
looked like this:
````rust
gizmos.axes(transform); // or
gizmos.axes(transform).with_length_scale(3.0);
````
then I think it's a reasonable expectation that the first thing would
"just work" for most applications, and it wouldn't, which would be kind
of a footgun.

### Future steps

There are a few directions that this might expand in the future:
1. Introduce additional options via the standard builder pattern; i.e.
introducing `AxesBuilder<T: TransformPoint>` so that people can
configure the axis colors, normalize all axes to a fixed length
independent of scale deformations, etc.

2. Fold this functionality into a plugin (like AabbGizmoPlugin) so that
the functionality becomes more-or-less automatic based on certain fixed
marker components. This wouldn't be very hard to implement, and it has
the benefit of making the axes more frictionless to use. Furthermore, if
we coupled this to the AABB functionality we already have, we could also
ensure that the plugin automatically sizes the axes (by coupling their
size to the dimensions of the AABB, for example).

3. Implement something similar for 2d. Honestly, I have no idea if this
is desired/useful, but I could probably just implement it in this PR if
that's the case.
spectria-limina pushed a commit to spectria-limina/bevy that referenced this pull request Mar 9, 2024
# Objective

- Follow-up to bevyengine#12211 
- Introduces an example project that demonstrates the implementation and
behavior of `Gizmos::axes` for an entity with a `Transform` component.

## Solution

In order to demonstrate how `Gizmo::axes` can be used and behaves in
practice, we introduce an example of a simple scene containing a pair of
cuboids locked in a grotesque, inscrutable dance: the two are repeatedly
given random `Transform`s which they interpolate to, showing how the
axes move with objects as they translate, rotate, and scale.

<img width="1023" alt="Screenshot 2024-03-04 at 1 16 33 PM"
src="https://github.com/bevyengine/bevy/assets/2975848/c1ff4794-6722-491c-8522-f59801645139">



On the implementation side, we demonstrate how to draw axes for
entities, automatically sizing them according to their bounding boxes
(so that the axes will be visible):
````rust
fn draw_axes(mut gizmos: Gizmos, query: Query<(&Transform, &Aabb), With<ShowAxes>>) {
    for (&transform, &aabb) in &query {
        let length = aabb.half_extents.length();
        gizmos.axes(transform, length);
    }
}
````

---

## Changelog

- Created examples/gizmos/axes.rs.
- Added 'axes' example to Cargo.toml.
mtsr pushed a commit to mtsr/bevy that referenced this pull request Mar 15, 2024
# Objective

- Follow-up to bevyengine#12211 
- Introduces an example project that demonstrates the implementation and
behavior of `Gizmos::axes` for an entity with a `Transform` component.

## Solution

In order to demonstrate how `Gizmo::axes` can be used and behaves in
practice, we introduce an example of a simple scene containing a pair of
cuboids locked in a grotesque, inscrutable dance: the two are repeatedly
given random `Transform`s which they interpolate to, showing how the
axes move with objects as they translate, rotate, and scale.

<img width="1023" alt="Screenshot 2024-03-04 at 1 16 33 PM"
src="https://github.com/bevyengine/bevy/assets/2975848/c1ff4794-6722-491c-8522-f59801645139">



On the implementation side, we demonstrate how to draw axes for
entities, automatically sizing them according to their bounding boxes
(so that the axes will be visible):
````rust
fn draw_axes(mut gizmos: Gizmos, query: Query<(&Transform, &Aabb), With<ShowAxes>>) {
    for (&transform, &aabb) in &query {
        let length = aabb.half_extents.length();
        gizmos.axes(transform, length);
    }
}
````

---

## Changelog

- Created examples/gizmos/axes.rs.
- Added 'axes' example to Cargo.toml.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Editor Graphical tools to make Bevy games A-Gizmos Visual editor and debug gizmos C-Feature A new feature, making something new possible M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants