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 more transform relative vectors #1300

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ impl GlobalTransform {
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation)
}

#[inline]
pub fn right(&self) -> Vec3 {
self.rotation * Vec3::unit_x()

Choose a reason for hiding this comment

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

If Bevy is right-handed y-up, and we're calling +z forward, then doesn't that make +x left, not right?

(Sorry about my previous deleted comment; got momentarily confused about the whole "-handed" naming convention.)

Choose a reason for hiding this comment

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

I.e. I expect the direction called "left" to be to my left if I'm looking down the direction called "forward".

Choose a reason for hiding this comment

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

Ah! Unity (referenced above) is left-handed — maybe that's where the oddness came from. I was beginning to worry that there were two different conventions for what's called "left", i.e. because -x is left on screen in Bevy when doing 2D rendering. 😅

Copy link
Member

Choose a reason for hiding this comment

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

haha yeah fortunately left still means left 😄

Choose a reason for hiding this comment

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

I think I communicated that poorly. I was actually trying to say that I think the code in this PR is actually "wrong" as it stands, at least in that by one obvious interpretation it appears internally inconsistent.

Let me try again. 😊

Unity, which appears to have inspired this code, is left-handed. In that left-handed context, all of the function names in this PR would actually match up to their implementations.

But Bevy is right-handed, and so they appear to be inconsistent. If forward means "along the positive z-axis", as it is defined in this PR, then right should be whatever direction you end up facing by turning 90 degrees clockwise from forward. Which in Bevy would mean you are now pointing along the negative x-axis, whereas this PR defines right as pointing down the positive x-axis.

I suspect the confusion exists because of mixing the idea of absolute directions in screen space with relative orientation in 3D space — and in particular the fact that you can actually do that without consequence in Unity, but not in Bevy. Or of course it could be as simple as just having copied the definitions from Unity without correcting for the differing handedness.

Or... there could be a third explanation that I haven't thought of, which makes the code in this PR "as intended". But if that's so, then I feel this whole API as it stands — at least in the context of a right-handed coordinate system like Bevy's — is likely to provide more confusion than utility, because there are so many obvious interpretations of it that make it look wrong.

I hope I'm making sense... 😅

Copy link
Member

@cart cart Jan 25, 2021

Choose a reason for hiding this comment

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

Ahh yeah I see your point now. And I do think you were clear enough in your initial description, I just didn't think enough about it / fixated too much on your last message. In 2d this "works" because +x is "the viewer's right". However from the perspective of the transform, +x is left (which is also true for 3d).

This should definitely be fixed. I think the functionality is still useful, but we need new names for it. Maybe just something like "local_positive_x" and "local_negative_x"?

We could also consider just flipping the signs on the x-axis, but that also makes things a little weird in 2d, as "left" would be the viewer's "right".

If anyone has better ideas, let me know :)

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 needed these functions when making a 2d game, and I tested it in that context, so I didn't choose the +x direction because of Unity but because it seemed to do the right thing.

}

#[inline]
pub fn up(&self) -> Vec3 {
self.rotation * Vec3::unit_y()
}

#[inline]
pub fn forward(&self) -> Vec3 {
self.rotation * Vec3::unit_z()
Expand Down
10 changes: 10 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ impl Transform {
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation)
}

#[inline]
pub fn right(&self) -> Vec3 {
self.rotation * Vec3::unit_x()
}

#[inline]
pub fn up(&self) -> Vec3 {
self.rotation * Vec3::unit_y()
}

#[inline]
pub fn forward(&self) -> Vec3 {
self.rotation * Vec3::unit_z()
Expand Down