Problems getting the angle of two quaternions

Ok, so I'm trying to get the angle of two quaternions, and it almost works fine, but then it jumps with

evec angle: 237.44999653311922 evec angle: 119.60001380112993 

and I can’t understand why life is for me. (Note: evec is the old variable name that remains in print)

Anyway, here is 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 can I make him stop jumping from 237 to 119 and keep going to full 360?

+4
source share
1 answer

First, what does the angle between two four-dimensional vectors (= quaternions) mean geometrically for you? You can calculate it, but the result may not make sense. Maybe you are looking for the angle between the axis of rotation, which is represented by two quaternions?

Secondly, there is an error:

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

The result from acos is the angle. Do not multiply by 2.

Thirdly, the angle between two vectors in three-dimensional or 4D-space can never exceed 180 Β°. On the plane, this can be because the plane imposes an orientation. In three-dimensional space, you will need to define an arbitrary direction as β€œup” to get angles above 180 Β°.

+1
source

Source: https://habr.com/ru/post/1389248/


All Articles