I'm making a 3d software engine and I've come along way so far but I'm getting stuck on concentating matrixes together I think.
Here is the synopsis of what happens.
* If I make a triangle that has z = 1 on all the coordinates and do a RotationZ, it works fine. Rotates around the z axis like a charm
* If I make a triangle that has z = 0 and then do
----------------
Matrix4 matWorld, matTrans, matRot;
MatrixTranslation( matTrans, 0, 0, 1 );
MatrixRotationZ( matRot, rad );
MatrixIdentity( matWorld );
MatrixMultiply( matWorld, matWorld, matRot );
MatrixMultiply( matWorld, matWorld, matTrans );
SetWorldTransform( matWorld );
----------------
This should have the same effect and rotate properly with the triangle 1 on the z axis. Instead it rotates really funky, so I'm guessing that I have a problem with either my Vector * Matrix function or my MatrixMultiply function. I need some help double checking.
Here are all the functions that could help.
This is my code for a 1x4 Vector * 4x4 Matrix and the code for 4x4 Matrix * 4x4 Matrix
void dmath::VectorMatrixMultiply( dmath::Vector4& lhs, const dmath::Vector4& rhs1, const dmath::Matrix4& rhs2 )
{
lhs.x = (rhs1.x * rhs2.M11) + (rhs1.y * rhs2.M21) + (rhs1.z * rhs2.M31) + (rhs1.w * rhs2.M41);
lhs.y = (rhs1.x * rhs2.M12) + (rhs1.y * rhs2.M22) + (rhs1.z * rhs2.M32) + (rhs1.w * rhs2.M42);
lhs.z = (rhs1.x * rhs2.M13) + (rhs1.y * rhs2.M23) + (rhs1.z * rhs2.M33) + (rhs1.w * rhs2.M43);
lhs.w = (rhs1.x * rhs2.M14) + (rhs1.y * rhs2.M24) + (rhs1.z * rhs2.M34) + (rhs1.w * rhs2.M44);
}
// 4x4 Matrix Multiply
void dmath::MatrixMultiply( dmath::Matrix4& lhs, const dmath::Matrix4& rhs1, const dmath::Matrix4& rhs2 )
{
lhs.M11 = (rhs1.M11 * rhs2.M11) + (rhs1.M12 * rhs2.M21) + (rhs1.M13 * rhs2.M31) + (rhs1.M14 * rhs2.M41);
lhs.M12 = (rhs1.M11 * rhs2.M12) + (rhs1.M12 * rhs2.M22) + (rhs1.M13 * rhs2.M32) + (rhs1.M14 * rhs2.M42);
lhs.M13 = (rhs1.M11 * rhs2.M13) + (rhs1.M12 * rhs2.M23) + (rhs1.M13 * rhs2.M33) + (rhs1.M14 * rhs2.M43);
lhs.M14 = (rhs1.M11 * rhs2.M14) + (rhs1.M12 * rhs2.M24) + (rhs1.M13 * rhs2.M34) + (rhs1.M14 * rhs2.M44);
lhs.M21 = (rhs1.M21 * rhs2.M11) + (rhs1.M22 * rhs2.M21) + (rhs1.M23 * rhs2.M31) + (rhs1.M24 * rhs2.M41);
lhs.M22 = (rhs1.M21 * rhs2.M12) + (rhs1.M22 * rhs2.M22) + (rhs1.M23 * rhs2.M32) + (rhs1.M24 * rhs2.M42);
lhs.M23 = (rhs1.M21 * rhs2.M13) + (rhs1.M22 * rhs2.M23) + (rhs1.M23 * rhs2.M33) + (rhs1.M24 * rhs2.M43);
lhs.M24 = (rhs1.M21 * rhs2.M14) + (rhs1.M22 * rhs2.M24) + (rhs1.M23 * rhs2.M34) + (rhs1.M24 * rhs2.M44);
lhs.M31 = (rhs1.M31 * rhs2.M11) + (rhs1.M32 * rhs2.M21) + (rhs1.M33 * rhs2.M31) + (rhs1.M34 * rhs2.M41);
lhs.M32 = (rhs1.M31 * rhs2.M12) + (rhs1.M32 * rhs2.M22) + (rhs1.M33 * rhs2.M32) + (rhs1.M34 * rhs2.M42);
lhs.M33 = (rhs1.M31 * rhs2.M13) + (rhs1.M32 * rhs2.M23) + (rhs1.M33 * rhs2.M33) + (rhs1.M34 * rhs2.M43);
lhs.M34 = (rhs1.M31 * rhs2.M14) + (rhs1.M32 * rhs2.M24) + (rhs1.M33 * rhs2.M34) + (rhs1.M34 * rhs2.M44);
lhs.M41 = (rhs1.M41 * rhs2.M11) + (rhs1.M42 * rhs2.M21) + (rhs1.M43 * rhs2.M31) + (rhs1.M44 * rhs2.M41);
lhs.M42 = (rhs1.M41 * rhs2.M12) + (rhs1.M42 * rhs2.M22) + (rhs1.M43 * rhs2.M32) + (rhs1.M44 * rhs2.M42);
lhs.M43 = (rhs1.M41 * rhs2.M13) + (rhs1.M42 * rhs2.M23) + (rhs1.M43 * rhs2.M33) + (rhs1.M44 * rhs2.M43);
lhs.M44 = (rhs1.M41 * rhs2.M14) + (rhs1.M42 * rhs2.M24) + (rhs1.M43 * rhs2.M34) + (rhs1.M44 * rhs2.M44);
}
This is the code for MatrixRotation, MatrixIdentity, MatrixTranslation
// 4x4 Identity Matrix
void dmath::MatrixIdentity( dmath::Matrix4& m )
{
m.M11 = 1; m.M12 = 0; m.M13 = 0; m.M14 = 0;
m.M21 = 0; m.M22 = 1; m.M23 = 0; m.M24 = 0;
m.M31 = 0; m.M32 = 0; m.M33 = 1; m.M34 = 0;
m.M41 = 0; m.M42 = 0; m.M43 = 0; m.M44 = 1;
}
// -- MATRIX TRANSLATION -- //
// 4x4 Translation Matrix
void dmath::MatrixTranslation( dmath::Matrix4& m, const float x, const float y, const float z )
{
m.M11 = 1; m.M12 = 0; m.M13 = 0; m.M14 = 0;
m.M21 = 0; m.M22 = 1; m.M23 = 0; m.M24 = 0;
m.M31 = 0; m.M32 = 0; m.M33 = 1; m.M34 = 0;
m.M41 = x; m.M42 = y; m.M43 = z; m.M44 = 1;
}
// 4x4 Matrix Rotation Z
void dmath::MatrixRotationZ( dmath::Matrix4& m, const float rad )
{
m.M11 = cosf(rad); m.M12 = sinf(rad); m.M13 = 0; m.M14 = 0;
m.M21 = -sinf(rad); m.M22 = cosf(rad); m.M23 = 0; m.M24 = 0;
m.M31 = 0; m.M32 = 0; m.M33 = 1; m.M34 = 0;
m.M41 = 0; m.M42 = 0; m.M43 = 0; m.M44 = 1;
}
// -- MATRIX PROJECTION -- //
// 4x4 Projection Matrix
void dmath::MatrixProjection( dmath::Matrix4& m )
{
m.M11 = 1; m.M12 = 0; m.M13 = 0; m.M14 = 0;
m.M21 = 0; m.M22 = 1; m.M23 = 0; m.M24 = 0;
m.M31 = 0; m.M32 = 0; m.M33 = 1; m.M34 = 1;
m.M41 = 0; m.M42 = 0; m.M43 = 0; m.M44 = 0;
}
I'm sorry I posted so much code I just don't know exactly where the problems lies. Hope someone will take the time to read this.
-ph33r
[edited by - ph33r on December 14, 2003 11:57:11 AM]