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

Support rotating Direction3d by Quat #11649

Merged
merged 3 commits into from
Feb 1, 2024

Conversation

Jondolf
Copy link
Contributor

@Jondolf Jondolf commented Feb 1, 2024

Objective

It's often necessary to rotate directions, but it currently has to be done like this:

Direction3d::new_unchecked(quat * *direction)

It'd be nice if you could rotate Direction3d directly:

quat * direction

Solution

Implement Mul<Direction3d> for Quat and the other way around. (Glam doesn't impl Mul<Quat> or MulAssign<Quat> for Vec3)

The quaternion must be a unit quaternion to keep the direction normalized, so there is a debug_assert! to be sure. Almost all Quat constructors produce unit quaternions, so there should only be issues if doing something like quat + quat instead of quat * quat, using Quat::from_xyzw directly, or when you have significant enough drift caused by e.g. physics simulation that doesn't normalize rotation. In general, these would probably cause unexpected results anyway.

I also moved tests around slightly to make dim2 and dim3 more consistent (dim3 had two separate test modules for some reason).

In the future, we'll probably want a Rotation2d type that would support the same for Direction2d. I considered implementing Mul<Mat2> for Direction2d, but that would probably be more questionable since Mat2 isn't as clearly associated with rotations as Quat is.

@Jondolf Jondolf added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Math Fundamental domain-agnostic mathematical operations labels Feb 1, 2024
@tim-blackbird tim-blackbird 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 1, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 1, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 1, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 1, 2024
Merged via the queue into bevyengine:main with commit 6f2eec8 Feb 1, 2024
23 checks passed
@Jondolf Jondolf deleted the direction3d-rotation branch February 1, 2024 22:27
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
# Objective

It's often necessary to rotate directions, but it currently has to be
done like this:

```rust
Direction3d::new_unchecked(quat * *direction)
```

It'd be nice if you could rotate `Direction3d` directly:

```rust
quat * direction
```

## Solution

Implement `Mul<Direction3d>` for `Quat` ~~and the other way around.~~
(Glam doesn't impl `Mul<Quat>` or `MulAssign<Quat>` for `Vec3`)

The quaternion must be a unit quaternion to keep the direction
normalized, so there is a `debug_assert!` to be sure. Almost all `Quat`
constructors produce unit quaternions, so there should only be issues if
doing something like `quat + quat` instead of `quat * quat`, using
`Quat::from_xyzw` directly, or when you have significant enough drift
caused by e.g. physics simulation that doesn't normalize rotation. In
general, these would probably cause unexpected results anyway.

I also moved tests around slightly to make `dim2` and `dim3` more
consistent (`dim3` had *two* separate `test` modules for some reason).

In the future, we'll probably want a `Rotation2d` type that would
support the same for `Direction2d`. I considered implementing
`Mul<Mat2>` for `Direction2d`, but that would probably be more
questionable since `Mat2` isn't as clearly associated with rotations as
`Quat` is.
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 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.

4 participants