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

Use Dir3 in Transform APIs #12530

Merged
merged 3 commits into from
Mar 17, 2024
Merged

Conversation

yyogo
Copy link
Contributor

@yyogo yyogo commented Mar 17, 2024

Objective

Make Transform APIs more ergonomic by allowing users to pass Dir3 as an argument where a direction is needed. Fixes #12481.

Solution

Accept impl TryInto<Dir3> instead of Vec3 for direction/axis arguments in Transform APIs


Changelog

The following Transform methods now accept an impl TryInto<Dir3> argument where they previously accepted directions as Vec3:

  • Transform::{look_to,looking_to}
  • Transform::{look_at,looking_at}
  • Transform::{align,aligned_by}

Migration Guide

This is not a breaking change since the arguments were previously Vec3 which already implements TryInto<Dir3>, and behavior is unchanged.

Copy link
Contributor

@afonsolage afonsolage left a comment

Choose a reason for hiding this comment

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

I wonder if shouldn't we use Dir3 directly, instead of impl TryInto<Dir3>, since Dir3 makes it explicit what is going on and what to expect from those parameters.

For the sake of avoiding breaking changes, it could make sense to use impl TryInto<Dir3>, but if we use Dir3 we don't have to worry about fallback values, since Dir3 will always be normalized and always represents an unit direction.

@yyogo
Copy link
Contributor Author

yyogo commented Mar 17, 2024

@afonsolage This is mentioned in the linked issue:

On the level of API design, just using Dir3 was considered for Transform::align because it is more "natural", but providing a Dir3 is often not ergonomic (especially since the Vec3 conversion is fallible) and because Transform::look_to used Vec3 already.

I'm fairly neutral on this tbh, I implemented the issue as specified but both options make sense to me.

@djeedai djeedai added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Transform Translations, rotations and scales labels Mar 17, 2024
Copy link
Contributor

@afonsolage afonsolage left a comment

Choose a reason for hiding this comment

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

Ok, in this case we are going for impl TryInto<Dir3> instead of Dir3 due to ergonomics and DX.

crates/bevy_transform/src/components/transform.rs Outdated Show resolved Hide resolved
Comment on lines 425 to 443
/// Example
/// ```
/// # use bevy_math::{Vec3, Quat};
/// # use bevy_math::{Dir3, Vec3, Quat};
/// # use bevy_transform::components::Transform;
/// # let mut t1 = Transform::IDENTITY;
/// # let mut t2 = Transform::IDENTITY;
/// t1.align(Vec3::X, Vec3::Y, Vec3::new(1., 1., 0.), Vec3::Z);
/// let main_axis_image = t1.rotation * Vec3::X;
/// t1.align(Dir3::X, Dir3::Y, Vec3::new(1., 1., 0.), Dir3::Z);
/// let main_axis_image = t1.rotation * Dir3::X;
/// let secondary_axis_image = t1.rotation * Vec3::new(1., 1., 0.);
/// assert!(main_axis_image.abs_diff_eq(Vec3::Y, 1e-5));
/// assert!(secondary_axis_image.abs_diff_eq(Vec3::new(0., 1., 1.), 1e-5));
///
/// t1.align(Vec3::ZERO, Vec3::Z, Vec3::ZERO, Vec3::X);
/// t2.align(Vec3::X, Vec3::Z, Vec3::Y, Vec3::X);
/// t1.align(Vec3::ZERO, Dir3::Z, Vec3::ZERO, Dir3::X);
/// t2.align(Dir3::X, Dir3::Z, Dir3::Y, Dir3::X);
/// assert_eq!(t1.rotation, t2.rotation);
///
/// t1.align(Vec3::X, Vec3::Z, Vec3::X, Vec3::Y);
/// t1.align(Dir3::X, Dir3::Z, Dir3::X, Dir3::Y);
/// assert_eq!(t1.rotation, Quat::from_rotation_arc(Vec3::X, Vec3::Z));
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice if the other methods also gotten an example like this, showcasing how you can mix&match vectors and directions to improve discoverability of the fact that both Dir3 and Vec3 are valid arguments, for new users.

Not a necessary change though.

@NthTensor NthTensor 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 Mar 17, 2024
@NthTensor
Copy link
Contributor

TryInto seems like the right choice to me, but I haven't looked this over super closely.

Nice work!

Co-authored-by: IQuick 143 <IQuick143cz@gmail.com>
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 17, 2024
Merged via the queue into bevyengine:main with commit ec3e7af Mar 17, 2024
25 checks passed
@yyogo yyogo deleted the transform-apis-dir3 branch March 17, 2024 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Transform Translations, rotations and scales C-Usability A targeted 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.

Make more Transform APIs interoperate with Dir3
6 participants