Support rotating Direction3d
by Quat
#11649
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
It's often necessary to rotate directions, but it currently has to be done like this:
It'd be nice if you could rotate
Direction3d
directly:quat * direction
Solution
Implement
Mul<Direction3d>
forQuat
and the other way around.(Glam doesn't implMul<Quat>
orMulAssign<Quat>
forVec3
)The quaternion must be a unit quaternion to keep the direction normalized, so there is a
debug_assert!
to be sure. Almost allQuat
constructors produce unit quaternions, so there should only be issues if doing something likequat + quat
instead ofquat * quat
, usingQuat::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
anddim3
more consistent (dim3
had two separatetest
modules for some reason).In the future, we'll probably want a
Rotation2d
type that would support the same forDirection2d
. I considered implementingMul<Mat2>
forDirection2d
, but that would probably be more questionable sinceMat2
isn't as clearly associated with rotations asQuat
is.