Main Menu

Quaternion?

Started by Hmm, May 18, 2007, 05:42:37 PM

Previous topic - Next topic

Hmm

Hi,

The physics library which I am using returns rotations as Quaternions.  I have very little experience in 3D math, and cannot see any obvious way to move that over to jPCT.  Given a Quaternion as a javax.vecmath.Quat4f object, how can I use that to set an Object3D's rotation?

Thanks for any help!  :D

EgonOlsen

Cyberkilla did some stuff for quaternions in his skeletal animation API. Maybe you want to download it and see if it helps...

Hmm

Thanks! :D

After reading a Math paper I ended up with the following code, which works quite nicely and solved my larger problem interfacing with OdeJava:

protected Matrix Quat4fToMatrix(javax.vecmath.Quat4f input)
{
float[] MatrixDump = new float[16];
float xx = input.x * input.x;
float xy = input.x * input.y;
float xz = input.x * input.z;
float xw = input.x * input.w;
float yy = input.y * input.y;
float yz = input.y * input.z;
float yw = input.y * input.w;
float zz = input.z * input.z;
float zw = input.z * input.w;

MatrixDump[0] = 1 - 2 * ( yy + zz );
MatrixDump[4] = 2 * ( xy - zw );
MatrixDump[8] = 2 * ( xz + yw );
MatrixDump[1] = 2 * ( xy + zw );
MatrixDump[5] = 1 - 2 * ( xx + zz );
MatrixDump[9] = 2 * ( yz - xw );
MatrixDump[2] = 2 * ( xz - yw );
MatrixDump[6] = 2 * ( yz + xw );
MatrixDump[10] = 1 - 2 * ( xx + yy );
MatrixDump[15] = 1;

Matrix buffer = new Matrix();
buffer.setDump(MatrixDump);
return buffer;
}