#### Archived

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

# Problem with my math

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

## Recommended Posts

I wrote a few math routines to do matrix math and such for me, but there is a problem some where along the line, because my resulting math is not the same as OpenGL's resulting math. I need my math to match OpenGL's, so I can manual compute some of the information. But when passed in the same order and method to OpenGL and my routines one strange difference arose, my rotation was backwards. When I multiplied my rotation matrix by OpenGL's using glMatrixMult, and then I did the same to mine. When passing points to the final matrices its as if the rotation was exactly opposite. Such as 80 degree rotation in OpenGL becomes -80 degrees in mine At 0 they are identical. Here are my math routines
void Mult4x4Matrix( float*A, float*B, float C[16] )
{

C[0]=A[0]*B[0]+A[1]*B[4]+A[2]*B[8]+A[3]*B[12];
C[1]=A[0]*B[1]+A[1]*B[5]+A[2]*B[9]+A[3]*B[13];
C[2]=A[0]*B[2]+A[1]*B[6]+A[2]*B[10]+A[3]*B[14];
C[3]=A[0]*B[3]+A[1]*B[7]+A[2]*B[11]+A[3]*B[15];

C[4]=A[4]*B[0]+A[5]*B[4]+A[6]*B[8]+A[7]*B[12];
C[5]=A[4]*B[1]+A[5]*B[5]+A[6]*B[9]+A[7]*B[13];
C[6]=A[4]*B[2]+A[5]*B[6]+A[6]*B[10]+A[7]*B[14];
C[7]=A[4]*B[3]+A[5]*B[7]+A[6]*B[11]+A[7]*B[15];

C[8]=A[8]*B[0]+A[9]*B[4]+A[10]*B[8]+A[11]*B[12];
C[9]=A[8]*B[1]+A[9]*B[5]+A[10]*B[9]+A[11]*B[13];
C[10]=A[8]*B[2]+A[9]*B[6]+A[10]*B[10]+A[11]*B[14];
C[11]=A[8]*B[3]+A[9]*B[7]+A[10]*B[11]+A[11]*B[15];

C[12]=A[12]*B[0]+A[13]*B[4]+A[14]*B[8]+A[15]*B[12];
C[13]=A[12]*B[1]+A[13]*B[5]+A[14]*B[9]+A[15]*B[13];
C[14]=A[12]*B[2]+A[13]*B[6]+A[14]*B[10]+A[15]*B[14];
C[15]=A[12]*B[3]+A[13]*B[7]+A[14]*B[11]+A[15]*B[15];
}

V MultVby4x4Matrix(float *A, V B)//

{
V result;

result.x=A[0]*B.x+A[1]*B.y+A[2]*B.z+A[3];
result.y=A[4]*B.x+A[5]*B.y+A[6]*B.z+A[7];
result.z=A[8]*B.x+A[9]*B.y+A[10]*B.z+A[11];

return result;
}

My order is translate then rotate for both cases. Any help would be greatly appreciated. PS: The rotation matrix used in both is IDENTICAL. "I seek knowledge and to help those who also seek it" [edited by - Xero-X2 on January 25, 2004 1:53:30 AM]

##### Share on other sites
Make sure you get the order of multiplication correct. If Mult4x4Matrix(A, B, C) meand C=A*B, and a matrix is indexed like (numbers are index into the corresponding element in the matrix)
[ 0  4  8 12 ][ 1  5  9 13 ][ 2  6 10 14 ][ 3  7 11 15 ]

then I believe you''re calculating B*A instead, which is incorrect.

##### Share on other sites
I used

[ 0  1  2  3  ][ 4  5  6  7  ][ 8  9  10 11 ][ 12 13 14 15 ]

for C=A*B

"I seek knowledge and to help those who also seek it"

[edited by - Xero-X2 on January 25, 2004 2:05:00 PM]

##### Share on other sites
Ok I re did my math to match
Brother Bobs, matrix indices,

void Mult4x4Matrix( float*A, float*B, float C[16] ){     C[0 ]=A[0]*B[0]+A[4]*B[1]+A[8 ]*B[2]+A[12]*B[3];     C[1 ]=A[1]*B[0]+A[5]*B[1]+A[9 ]*B[2]+A[13]*B[3];     C[2 ]=A[2]*B[0]+A[6]*B[1]+A[10]*B[2]+A[14]*B[3];     C[3 ]=A[3]*B[0]+A[7]*B[1]+A[11]*B[2]+A[15]*B[3];     C[4 ]=A[0]*B[4]+A[4]*B[5]+A[8 ]*B[6]+A[12]*B[7];     C[5 ]=A[1]*B[4]+A[5]*B[5]+A[9 ]*B[6]+A[13]*B[7];     C[6 ]=A[2]*B[4]+A[6]*B[5]+A[10]*B[6]+A[14]*B[7];     C[7 ]=A[3]*B[4]+A[7]*B[5]+A[11]*B[6]+A[15]*B[7];     C[8 ]=A[0]*B[8]+A[4]*B[9]+A[8 ]*B[10]+A[12]*B[11];     C[9 ]=A[1]*B[8]+A[5]*B[9]+A[9 ]*B[10]+A[13]*B[11];     C[10]=A[2]*B[8]+A[6]*B[9]+A[10]*B[10]+A[14]*B[11];     C[11]=A[3]*B[8]+A[7]*B[9]+A[11]*B[10]+A[15]*B[11];     C[12]=A[0]*B[12]+A[4]*B[13]+A[8 ]*B[14]+A[12]*B[15];     C[13]=A[1]*B[12]+A[5]*B[13]+A[9 ]*B[14]+A[13]*B[15];     C[14]=A[2]*B[12]+A[6]*B[13]+A[10]*B[14]+A[14]*B[15];     C[15]=A[3]*B[12]+A[7]*B[13]+A[11]*B[14]+A[15]*B[15];}V MultVby4x4Matrix( float*A, V B ){V OutPutVector Out;    Out.x=A[0]*B.x+A[4]*B.y+A[8]*B.z+A[12];    Out.y=A[1]*B.x+A[5]*B.y+A[9]*B.z+A[13];    Out.z=A[2]*B.x+A[6]*B.y+A[10]*B.z+A[14];return OutPutVector;}

Its close, but on the Z axis the points distance from the X/Y plane is to Small .

the other two axis seem to match OpenGL perfectly.

Edit: clearification on problem.
Edit: Sorry OpenGLs results are More distant, then mine.
Edit: Typo i code.

"I seek knowledge and to help those who also seek it"

[edited by - Xero-X2 on January 25, 2004 4:07:06 PM]

##### Share on other sites
Are you shure, your y-value is correct, too? - Look at your output vector. You set its x-component three times and don''t set the other components.

##### Share on other sites
Sorry, that was a typo I changed how I did that before I posted it and didn't double check. I fixed it in the code above

Heres what it looks like:

They should meet rather fluently but they have large gaps.

"I seek knowledge and to help those who also seek it"

[edited by - Xero-X2 on January 25, 2004 5:12:10 PM]

##### Share on other sites
After further testing I have concluded that my math matches OpenGL''s and that the cause of the above error is else where in my code.

Thanks for helping.

"I seek knowledge and to help those who also seek it"

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009487
• ### Who's Online (See full list)

There are no registered users currently online

×