Question

What is the difference between rotating a geometry in JMonkeyEngine using the rotate method:

float r = FastMath.DEG_TO_RAD * 45f; // convert degrees to radians
geom.rotate(r, 0.0f, 0.0f); // rotate the geometry around the x-axis by 45 degrees

and rotating a geometry using a quaternion:

Quaternion roll045 = new Quaternion(); // create the quaternion
roll045.fromAngleAxis(45*FastMath.DEG_TO_RAD, Vector3f.UNIT_X); // supply angle and axis as arguments)
geom.setLocalRotation(roll045); // rotate the geometry around the x-axis by 45 degrees

This is confusing for me because the result is the same for both. So I'd like to find out the difference and when to use one over the other.

The book that I'm reading says that the first method is relative, and the second using a quaternion is absolute but I'm still fuzzy on what that means.

Was it helpful?

Solution

Difference between using a quaternion and using Euler angles
To the question in your title there is no functional difference between using quaternions and angle representation, in fact internally the .rotate() function is

public Spatial rotate(float xAngle, float yAngle, float zAngle) {
    TempVars vars = TempVars.get();
    Quaternion q = vars.quat1;
    q.fromAngles(xAngle, yAngle, zAngle);
    rotate(q);
    vars.release();

    return this;
}

In other words, whether you use a quaternion directly or not you are using a quaternion.



Difference between .rotate() and .setLocalRotation()
However the two functions you are using are not equivalent, in fact there are both .rotate(angles) and .rotate(quaternion) (although the .setLocalRotation() is only available for quaternions). So the second part of your question is whats the difference between .rotate(anything) and .setLocalRotation(anything). Again looking at the source code gives us our answer

public Spatial rotate(Quaternion rot) {
    this.localTransform.getRotation().multLocal(rot);
    setTransformRefresh();

    return this;
}


public void setLocalRotation(Quaternion quaternion) {
    localTransform.setRotation(quaternion);
    setTransformRefresh();
}

So, .rotate() rotates the object (in its local frame) by an amount from where it is now whereas .setLocalRotation() changes the rotation irrespective of where it is now.

Conclusion
If your object currently has no rotation the two functions are identical, however if the object has already been rotated then they are equivalent to "as well as the current rotation" and "instead of the current rotation".

Quaternions have many advantages over the standard angle approach; the most obvious of which is avoiding gimbal lock. Where you can use quaternions do use them. The angles methods are all convenience methods to help you if you need them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top