Author Topic: Quaternion?  (Read 3479 times)

Offline Hmm

  • byte
  • *
  • Posts: 6
    • View Profile
Quaternion?
« on: May 18, 2007, 05:42:37 pm »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Quaternion?
« Reply #1 on: May 18, 2007, 07:22:48 pm »
Cyberkilla did some stuff for quaternions in his skeletal animation API. Maybe you want to download it and see if it helps...

Offline Hmm

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Quaternion?
« Reply #2 on: May 20, 2007, 08:37:19 pm »
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:

Code: [Select]
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;
}