www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: Hmm on May 18, 2007, 05:42:37 PM

Title: Quaternion?
Post by: Hmm 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 (http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dapi/javax/vecmath/Quat4f.html) object, how can I use that to set an Object3D's rotation?

Thanks for any help!  :D
Title: Re: Quaternion?
Post by: EgonOlsen 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...
Title: Re: Quaternion?
Post by: Hmm on May 20, 2007, 08:37:19 PM
Thanks! :D

After reading a Math paper (http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm) I ended up with the following code, which works quite nicely and solved my larger problem interfacing with OdeJava (http://www.javagaming.org/forums/index.php?topic=16705.0):

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;
}