Mini contribution to Quaternion

Hi,

I’m learning JME but not Java, so I might have small contributions.

In com.jme3.math.Quaternion

        public Quaternion inverse() {
                float norm = norm();
                if (norm > 0.0) {
                    float invNorm = 1.0f / norm;
                    return new Quaternion(-x * invNorm, -y * invNorm, -z * invNorm, w
                            * invNorm);
                }
                // return an invalid result to flag the error
                return null;
            }

Should throw an IllegalArgumentException instead of returning null, so the stacktrace stays clean.
Interested in that kind of feedback?

Code that relies on this returning null would break.

It might be that code expects to be able to call this method to see if the quaternion can be inverted successfully… and throwing an exception both breaks that kind of code and makes it a lot more complicated. Where as checking for a null is easy.

Furthermore, if your quaternion was valid to begin with you’d never see this in normal code.

Ah yes, code point about existing calling code

Ack! Before this gets any older…
There is something that can be added to an ‘in-house’ Quaternion. (In house being not the usual Java Quat.)
How about adding getForwardVector() that would return q.getRotationColumn(2) and getBackwardVector() that would return q.getRotationColumn(2).negate() and the corresponding get left/right and up/down vectors?
How many people look for that/these and can’t find it/them because they don’t know it’s/the’re there?
We could use a tutorial on 3D (spaceship) navigation, too.

Well, in real life, quaternions can be used for many things and anyway those getRotationColumn() methods are black magic voodoo that one should avoid.

Way easier just to do exactly what you think you want… rotate an axis vector. Then you avoid conversations about “what is forward?”

quat.mult(Vector3f.UNIT_Z) can’t really be any clearer, I think.

1 Like