Matrix.CreateFromAxisAngle

Jul 6, 2010 at 4:06 PM
Edited Jul 6, 2010 at 4:08 PM
I'm working on porting some XNA code to create a hex-tiled sphere (truncated icosahedron subdivided several times) but the code requires Matrix.CreateFromAxisAngle which is not implemented in Balder's Matrix class. Here is what I came up with based on the following website:

http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm

Note: the S matrix is flipped around the diagonal in the code as compared to the website, but it gives the same results as the XNA implementation

public static Matrix CreateFromAxisAngle(Vector3 axis, float angle)
{
float c = (float)Math.Cos(angle);
float s = (float)Math.Sin(angle);
float t = 1.0f - c;

float x = axis.X;
float y = axis.Y;
float z = axis.Z;

Matrix C = c * new Matrix(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 0
);
C.M44 = 1;
Matrix T = t * new Matrix(
x * x, x * y, x * z, 0,
x * y, y * y, y * z, 0,
x * z, y * z, z * z, 0,
0, 0, 0, 0
);
Matrix S = s * new Matrix(
0, z, -y, 0,
-z, 0, x, 0,
y, -x, 0, 0,
0, 0, 0, 0
);
return C + T + S;
}

Code for the test app available on request :)
Coordinator
Jul 6, 2010 at 5:03 PM

Most excellent.

If you don't mind, I can include it in the Matrix class.

Jul 6, 2010 at 5:29 PM
Please do :) it'll require a few tweaks to make it build with Balder, but nothing major
Coordinator
Jul 11, 2010 at 6:24 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.