Quaternion class mult(Quaternion q, Quaternion res) is not safe for res == thi

see http://code.google.com/p/jmonkeyengine/issues/detail?id=559&can=6&colspec=ID%20Type%20Status%20Component%20Priority%20Difficulty%20Product%20Milestone%20Owner%20Summary

From the function definition we can see that, while the documentation says “It IS safe for this and res to be the same object” this is NOT the case!

/**
* mult multiplies this quaternion by a parameter quaternion.
* The result is returned as a new quaternion. It should be noted that
* quaternion multiplication is not commutative so q * p != p * q.
*
* It IS safe for q and res to be the same object.
* It IS safe for this and res to be the same object.
*
* @param q
* the quaternion to multiply this quaternion by.
* @param res
* the quaternion to store the result in.
* @return the new quaternion.
*/
public Quaternion mult(Quaternion q, Quaternion res) {
if (res == null) {
res = new Quaternion();
}
float qw = q.w, qx = q.x, qy = q.y, qz = q.z;
res.x = x * qw + y * qz - z * qy + w * qx;
res.y = -x * qz + y * qw + z * qx + w * qy;
res.z = x * qy - y * qx + z * qw + w * qz;
res.w = -x * qx - y * qy - z * qz + w * qw;
return res;
}

2 Likes

Right, thats correct, the second line should be “IS NOT”. I guess that was the intention of the writer given the capital letters… Thanks for mentioning :slight_smile:

Oh man I think I actually fell for that one. I couldn’t get my rotations working, rewrote the code later on but I remember thinking “Oh, handy, like a multLocal” :smiley: Trust the source Luke.