Fix broken negative scaling when using Jolt Physics #103440
Open
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.
Fixes #103408.
Supersedes #103426.
This PR effectively just brings back some code (derived in part from Jolt's own
JPH::Mat44::Decompose
) that I had left behind when porting the Jolt integration from the Godot Jolt extension in #99895, due to wanting to keep things as minimal as possible.What this code is meant to do is take a potentially scaled
Basis
(orTransform3D
) and decompose it into a rotation part and a scale part, which must be done before passing any transform into Jolt, as Jolt takes scale separately from the rest of the transform in its API, and sometimes also takes rotation separately, in the form of a quaternion.I had mistaken the following code:
... as being a valid substitute for this code:
... which it is not, because while
Basis::orthonormalize
does also use the Gram-Schmidt process to perform the orthonormalization, much like this "new"decompose
function, it does not handle reflection (i.e. negative scaling) in any way, meaning we can end up with aBasis
that is orthonormal, but is not in fact a valid (right-handed) rotation, which causes problems with things likeBasis::get_quaternion
, as it assumes a valid (right-handed) rotation.This "new"
decompose
function solves this by essentially just flipping all the axes of theBasis
when we find ourselves with a negative determinant (i.e. negative scaling) which seems to line up with the convention used by otherBasis
methods, likeBasis::get_scale
andBasis::get_rotation_quaternion
.It also serves as a minor optimization, since we can avoid some of the redundant vector normalizations that are shared between
Basis::get_scale
andBasis::orthonormalize
.