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

return Direction3d from Transform::up and friends #11604

Merged
merged 5 commits into from
Feb 2, 2024

Conversation

rosefromthedead
Copy link
Contributor

Objective

Drawing a Gizmos::circle whose normal is derived from a Transform's local axes now requires converting a Vec3 to a Direction3d and unwrapping the result, and I think we shold move the conversion into Bevy.

Solution

We can make Transform::{left,right,up,down,forward,back,local_x,local_y,local_z} return a Direction3d, because they know that their results will be of finite non-zero length (roughly 1.0).


Changelog

Transform::up() and similar functions now return Direction3d instead of Vec3.

Migration Guide

Callers of Transform::up() and similar functions may have to dereference the returned Direction3d to get to the inner Vec3.

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@rosefromthedead
Copy link
Contributor Author

Maybe this would be better with a new method on Direction3d to convert to Vec3, so we're not making people use the magic star?

@alice-i-cecile alice-i-cecile added C-Usability A simple quality-of-life change that makes Bevy easier to use A-Transform Translations, rotations and scales A-Math Fundamental domain-agnostic mathematical operations labels Jan 29, 2024
@alice-i-cecile
Copy link
Member

I like this, but I'd prefer to use a From impl between Direction3D and Vec3.

self.rotation * Vec3::X
pub fn local_x(&self) -> Direction3d {
// Direction3d::new(x) panics if x is of invalid length, but quat * unit vector is length 1
Direction3d::new(self.rotation * Vec3::X).unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

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

These should probably use new_unchecked to avoid the normalization. It'd be useful to implement Mul<Quat> for the direction types though; with that, this would just be:

self.rotation * Direction3d::X

Of course, it would have to be guaranteed that rotation doesn't change the length. I think it should work, but I haven't tried with degenerate quaternions.

Copy link
Contributor

@Jondolf Jondolf Jan 29, 2024

Choose a reason for hiding this comment

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

This (rotate direction with multiplication) would also only work for 3D directions until we have a Rotation2d

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know enough maths to confidently use new_unchecked (or to say that the unwrap will never be hit), so I'd appreciate if someone smarter than me could help here

Copy link
Member

Choose a reason for hiding this comment

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

I think we should merge this with the checked version for now, and look into accelerating this (with tests) in a follow-up.

@NiseVoid
Copy link
Contributor

NiseVoid commented Jan 29, 2024

@rosefromthedead

Maybe this would be better with a new method on Direction3d to convert to Vec3, so we're not making people use the magic star?

You mean to stop people from having to do *transform.forward() * length? I think a Mul<f32> for Direction3d that returns Vec3 could be nice too 🤔


@alice-i-cecile

I like this, but I'd prefer to use a From impl between Direction3D and Vec3.

What From are we talking about? From<Vec3> for Direction3d?

@alice-i-cecile
Copy link
Member

What From are we talking about? From for Direction3d?

Both directions of this, and then in the code changes use direction.into() or take an impl Into<Vec3> rather than using deferencing.

also impl From<Direction3d> for Vec3 and Mul<f32> for Direction3d to make
Direction3ds a bit easier to use
@rosefromthedead
Copy link
Contributor Author

New rev has Mul<f32> for Direction3d, From<Direction3d> for Vec3, and we already had TryFrom<Vec3> for Direction3d. This doesn't eliminate dereferencing in examples, because e.g. examples/3d/tonemapping.rs has:

    transform.translation += *transform.forward();

and this can't be a .into() call because then type inference fails. I wonder whether alongside the From impl, we should have a .to_vec3()? Or just stick with the dereferencing?

@alice-i-cecile
Copy link
Member

I'm fine with the dereference-ing there for now; no need to bikeshed endlessly.

Copy link
Contributor

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

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

I think we could get rid of all of the derefs shown here with just some simple impls. Mul<f32> already allows us to get rid of several of them.

I'm fine leaving more impls to follow-ups too though

Comment on lines +94 to +100
impl std::ops::Mul<f32> for Direction3d {
type Output = Vec3;
fn mul(self, rhs: f32) -> Self::Output {
self.0 * rhs
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be implemented for Direction2d as well for consistency. It should probably also be implemented the other way around, i.e. impl Mul<Direction3d> for f32.

Comment on lines +74 to +79
impl From<Direction3d> for Vec3 {
fn from(value: Direction3d) -> Self {
value.0
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This should also be implemented for the Direction2d & Vec2 pair.

crates/bevy_audio/src/sinks.rs Outdated Show resolved Hide resolved
Comment on lines +174 to 178
let forward = *transform.forward();
let right = *transform.right();
transform.translation += controller.velocity.x * dt * right
+ controller.velocity.y * dt * Vec3::Y
+ controller.velocity.z * dt * forward;
Copy link
Contributor

Choose a reason for hiding this comment

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

If we impl Mul<Direction3d> for f32 or switch around the operand order, these derefs shouldn't be needed either.

examples/transforms/transform.rs Outdated Show resolved Hide resolved
examples/transforms/translation.rs Outdated Show resolved Hide resolved
@@ -127,7 +127,7 @@ fn rotate_cube(
// Update the rotation of the cube(s).
for (mut transform, cube) in &mut cubes {
// Calculate the rotation of the cube if it would be looking at the sphere in the center.
let look_at_sphere = transform.looking_at(center, transform.local_y());
let look_at_sphere = transform.looking_at(center, *transform.local_y());
Copy link
Contributor

Choose a reason for hiding this comment

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

We could technically make looking_at take an impl Into<Vec3> to remove the need for deref here, but it's probably follow-up material (and also pretty niche probably)

@@ -224,7 +224,7 @@ fn setup_color_gradient_scene(
camera_transform: Res<CameraTransform>,
) {
let mut transform = camera_transform.0;
transform.translation += transform.forward();
transform.translation += *transform.forward();
Copy link
Contributor

Choose a reason for hiding this comment

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

One way to avoid the deref here would be to simply implement Add<Direction3d> for Vec3. That can be done in a follow-up though

Copy link
Contributor

@NthTensor NthTensor Feb 2, 2024

Choose a reason for hiding this comment

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

I would find this a little odd, from an API perspective. If we include direction in the vector algebra we might as well just call it UnitVector. Multiplying a direction by a scalar to get a vector makes sense. Adding a direction to a vector... that's just weird to me.

Copy link
Contributor

@NthTensor NthTensor left a comment

Choose a reason for hiding this comment

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

While I do think the remaining components are worth fixing, I don't think they are worth blocking on.

Let's get this merged.

@Jondolf Jondolf added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 2, 2024
alice-i-cecile and others added 3 commits February 2, 2024 09:50
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
@alice-i-cecile
Copy link
Member

Committed the simple suggestions, resolved merge conflicts. Going to try and merge: CI will validate that I didn't break any of those trivial changes.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 2, 2024
Merged via the queue into bevyengine:main with commit d6f1649 Feb 2, 2024
23 checks passed
@rosefromthedead
Copy link
Contributor Author

Thanks for pushing this forward, I got busy and it's nice to see it land :)

tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
# Objective
Drawing a `Gizmos::circle` whose normal is derived from a Transform's
local axes now requires converting a Vec3 to a Direction3d and
unwrapping the result, and I think we shold move the conversion into
Bevy.

## Solution
We can make
`Transform::{left,right,up,down,forward,back,local_x,local_y,local_z}`
return a Direction3d, because they know that their results will be of
finite non-zero length (roughly 1.0).

---

## Changelog
`Transform::up()` and similar functions now return `Direction3d` instead
of `Vec3`.

## Migration Guide
Callers of `Transform::up()` and similar functions may have to
dereference the returned `Direction3d` to get to the inner `Vec3`.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Transform Translations, rotations and scales C-Usability A simple quality-of-life change that makes Bevy easier to use 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.

None yet

5 participants