-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
return Direction3d from Transform::up and friends #11604
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
7291423
to
0b26a5a
Compare
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? |
I like this, but I'd prefer to use a |
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
You mean to stop people from having to do
What From are we talking about? |
Both directions of this, and then in the code changes use |
0b26a5a
to
bb37d15
Compare
also impl From<Direction3d> for Vec3 and Mul<f32> for Direction3d to make Direction3ds a bit easier to use
bb37d15
to
4c9ac33
Compare
New rev has
and this can't be a |
I'm fine with the dereference-ing there for now; no need to bikeshed endlessly. |
There was a problem hiding this 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
impl std::ops::Mul<f32> for Direction3d { | ||
type Output = Vec3; | ||
fn mul(self, rhs: f32) -> Self::Output { | ||
self.0 * rhs | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
.
impl From<Direction3d> for Vec3 { | ||
fn from(value: Direction3d) -> Self { | ||
value.0 | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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.
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; |
There was a problem hiding this comment.
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.
@@ -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()); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
Co-authored-by: Joona Aalto <[email protected]>
Co-authored-by: Joona Aalto <[email protected]>
Co-authored-by: Joona Aalto <[email protected]>
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. |
Thanks for pushing this forward, I got busy and it's nice to see it land :) |
# 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 <[email protected]> Co-authored-by: Joona Aalto <[email protected]>
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 returnDirection3d
instead ofVec3
.Migration Guide
Callers of
Transform::up()
and similar functions may have to dereference the returnedDirection3d
to get to the innerVec3
.