I noticed that sometimes preset rotation of the object (in values Roll Pitch Yaw) and kept in data rotation of object are not necessary the same. I am writing the interface which allows the player rotate object by 90 degrees in any axis. Simple enough. But sometimes I get bizarre Rotator values, like (-97,-90,97)… in adding only 90 degrees in some basic Cartesian axis! (By the way, it is totally equivalent to (-90,-90,90)). I figured out that it probably has to do with this set of equations of directional vector (which, together with Roll, has the same amount of system data as RPY values):
x = cos(yaw)cos(pitch),
y = sin(yaw)cos(pitch),
z = sin(pitch).
I figured out that if pitch is +/- 90, then we can’t guess the value of yaw. Sometimes instead of pure (0,0,1) directional vector engine returns something close-to-zero, like (1e-7,5e-8,1) vector - and there we have the source of weird Yaw and Roll values, since to get Yaw value Engine has to divide y by x and take arcus tangens of this value. To get rid of it I try to use some re-normalization procedure, which takes directional vector, cuts any small values, takes from it again Rotator and compares it with the old rotator to figure out proper Roll value.
(As far as I know the solution is to apply equation: Roll’ = Roll + alpha, Yaw’ = Yaw - alpha, where alpha is any angle. After this substitution we get exactly the same system… for the case when Pitch is -90. If Pitch is 90 we have +alpha in both equations. For more general case I don’t even know.).
My question - how would you propose to get rid of this bug in code? I’ll probably post some my own solution soon (when I find it), but I am interested how others can solve it.