www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: Hmm on May 13, 2007, 03:24:08 am

Title: Matrix3f
Post by: Hmm on May 13, 2007, 03:24:08 am
Hello,

I have a rotation stored as a Matrix3f (http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dapi/javax/vecmath/Matrix3f.html) object, and need to use that to set the rotation of an Object3D (http://www.jpct.net/doc/com/threed/jpct/Object3D.html).  Unfortunately, I don't quite understand how rotations are represented in this format, so I'm having trouble making the conversion to the Matrix (http://www.jpct.net/doc/com/threed/jpct/Matrix.html) object needed for the setRotationMatrix() (http://www.jpct.net/doc/com/threed/jpct/Object3D.html#setRotationMatrix(com.threed.jpct.Matrix)) method of the Object3D (http://www.jpct.net/doc/com/threed/jpct/Object3D.html).

Is there any simple way to convert from a Matrix3f (http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dapi/javax/vecmath/Matrix3f.html) to a Matrix (http://www.jpct.net/doc/com/threed/jpct/Matrix.html)?

Thank you for any help! :)
Title: Re: Matrix3f
Post by: Hmm on May 13, 2007, 06:18:21 pm
Here's the code I ended up with for copying a Matrix3f into the format for a rotation Matrix:

Code: [Select]
protected Matrix Matrix3fToMatrix(Matrix3f input)
{
//Declare variables
float[] insertDump = new float[16]; //The array holding the values in the new Matrix
float[] rowDump = new float[3];     //The array for temporarily holding values from the rows of the Matrix3f
int targetElement = 0;              //Points to the next location in the insertDump to add data

//Loop through the rows of the Matrix3f
for (int sourceRow = 0; sourceRow < 3; sourceRow++)
{
//Grab the row from the Matrix3f
input.getRow(sourceRow, rowDump);

//Insert the 3 elements from the Matrix3f into the Matrix.  The fourth element has been initialized to 0.0f.
insertDump[targetElement] = rowDump[0];
insertDump[targetElement + 1] = rowDump[1];
insertDump[targetElement + 2] = rowDump[2];

//Move the target ahead by four spaces
targetElement += 4;
}

//The final row consists of { 0.0f, 0.0f, 0.0f, 1.0f }, since this is a rotation matrix
insertDump[15] = 1.0f;

//Create the new Matrix and load in the values
Matrix output = new Matrix();
output.setDump(insertDump);

//Return
return output;
}
Title: Re: Matrix3f
Post by: EgonOlsen on May 13, 2007, 09:26:03 pm
That's exactly how to do it.