4

Okay, so I'm trying to get the angle of two quaternions, and it almost works perfectly, but then it jumps from

evec angle: 237.44999653311922
evec angle: 119.60001380112993

and I can't figure out why for the life of me. (Note: evec was a old variable name that just stayed in the print)

Anyway, here's my code:

FloatBuffer fb = BufferUtils.createFloatBuffer(16);

// get the current modelview matrix
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, fb);
Matrix4f mvm = new Matrix4f();
mvm.load(fb);

Quaternion qmv2 = new Quaternion();
Matrix4f imvm = new Matrix4f();
Matrix4f.invert(mvm, imvm);
qmv2.setFromMatrix(imvm);
qmv2.normalise();

Matrix3f nil = new Matrix3f();
nil.setIdentity();
Quaternion qnil = new Quaternion();
qnil.setFromMatrix(nil);
qnil.normalise();

float radAngle = (float)(2.0 * Math.acos(Quaternion.dot(qmv2, qnil)));

System.out.println("evec angle: " + Math.toDegrees(radAngle));

How do I make it stop jumping from 237 to 119 and keep going up to the full 360?

1
  • I'm not totally fresh on my quaternion math right now, but the fact that you're ever getting an angle greater than 180° smells fishy to me - and suggests that something is wrong in the logic. There is no way for two vectors to have a greater-than-180° angle between them... Commented Feb 6, 2012 at 1:35

1 Answer 1

1

First, what does an angle between two four dimensional vectors (=quaternions) mean to you geometrically? You can calculate it but the result might not make sense. Maybe you are looking for the angle between the axis of the rotations that the two quaternions represent?

Second, you have an error here:

float radAngle = (float)(2.0 * Math.acos(Quaternion.dot(qmv2, qnil)));
                         ^^^^^

The result from acos is the angle. Don't multiply by 2.

Third, the angle between two vectors in a 3D or 4D space can never be greater than 180°. On a plane it can because the plane imposes an orientation. In a 3D space you would have to define an arbitrary direction as "up" to get angles higher than 180°.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.