I’m a recent arrival to JMonkey, and I’ve just started digging into Quaternions. As I understand it, they are simply rotation around an given axis (even if the math is not so simple). So I started with a simple case: taking a figure facing ‘forward’ and rotating it around the y-axis to make it turn. I’m rather confounded by the results I’ve been getting.
So when I run the following code…
public class RotationTest {
public static void main(String[] args) {
testRotation();
}
private static void testRotation() {
Vector3f initialDirection = Vector3f.UNIT_Z.negate();
// A small rotation quantity to simulate a tpf value
float amountToRotate = FastMath.PI / 16;
// Rotate 'amountToRotate' around the Y axis
Quaternion testRotation = new Quaternion(0f, 1f, 0f, amountToRotate);
// Apply rotation
// Why does this flip my z axis?
Vector3f resultingDirection = testRotation.mult(initialDirection);
System.out.println("currentDirection: " + initialDirection);
System.out.println("resultingDirection: " + resultingDirection);
}
It looks to me like a rotation is happening, but the Z-axis of the result is inverted! For what should be a small rotation it ‘jumps’ from -1.0 to +0.961…
I am new to quaternions and am not confident enough in my math to sure yet, but this doesn’t seem right. Am I interpreting it wrong?
@eadsjr said:
I'm a recent arrival to JMonkey, and I've just started digging into Quaternions. As I understand it, they are simply rotation around an given axis (even if the math is not so simple). So I started with a simple case: taking a figure facing 'forward' and rotating it around the y-axis to make it turn. I'm rather confounded by the results I've been getting.
So when I run the following code…
I get the following output…
It looks to me like a rotation is happening, but the Z-axis of the result is inverted! For what should be a small rotation it ‘jumps’ from -1.0 to +0.961…
I am new to quaternions and am not confident enough in my math to sure yet, but this doesn’t seem right. Am I interpreting it wrong?
You are constructing the quaternion wrong. The ‘w’-component is not rotation angles around the given axis. That’s not how Quaternion works.
// Rotate 'amountToRotate' around the Y axis
Quaternion testRotation = new Quaternion();
testRotation = testRotation.fromAngleAxis(amountToRotate, Vector3f.UNIT_Y);