Archived

This topic is now archived and is closed to further replies.

Need help with some linear algebra and transforms

This topic is 5111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
I notice that you're passing the same variables by reference...

so that you do something like this:


void func(int & a, const int & b);

int main(int argc, char ** argv)
{
int a=1;
func (a, a);
return 0;
}

void func(int & a, const int & b)
{
a=b*2;
printf("%d\n", b);
}


Note that on my box, it produces this output:

scout@b0rked:~/bob$ make asdf
g++ asdf.C -o asdf
scout@b0rked:~/bob$ ./asdf
2

Also, remember that in matrix multiplication, order matters.
However, I think that the code thing may be your issue. This would result in something pretty strange, because the matrix on the right would be updating while you were still multiplying!

I hope this helps.

Mark (Scout)





All polynomials are funny - some to a higher degree.
Furthermore, polynomials of degree zero are constantly funny.

[edited by - MrScout on December 14, 2003 12:55:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i think you''re gay name might also have something to do with why it might not be working correctly.

Share this post


Link to post
Share on other sites