fix: glm::angle() discards the sign of result for angles in range (2*pi-1, 2*pi) #1038
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.
While #946 fixes precision loss on small angles in various functions, the way patch is implemented for
glm::angle()
introduces a mathematical error.https://github.com/g-truc/glm/pull/946/files#diff-e2a0bd93372fb1306bb3caaf8fb8ed34089fca0ebd5356d4e6306eac0244fa31R10
For angles in range
(-1, 1)
(in radians),glm::angle()
useasin()
on non-scale components to prevent precision loss. However, the way it extractssin(theta/2)
from the non-scale components -sqrt(x*x+y*y+z*z)
- effectively takes the absloute value, thus the sign of result is lost.This error affects angles in range
(2*pi-1, 2*pi)
. Proof of concept (included in this PR):A simple fix will be take the sign ofw
into consideration, i.e.sign(w) * sqrt(x*x+y*y+z*z)
, but it will break continuity in angle calculations by introducing negative angles (glm::angleAxis(0.3f, my_axis) * glm::angleAxis(5.28f, my_axis) == glm::angleAxis(-0.973186f, my_axis)
, albeit equivalent in terms of rotations). Working on a better fix.