Skip to main content
gamedev.net
🔒 Locked

Check my code...

Started by acw83 Apr 20, 2000 at 2:08 PM 3 replies 700+ views
Original Post
acw83
acw83
Here''s my code for matrix multiplications and 3D rotation, tell me if you see any areas for improevment or flat out errors. Thanks! ---- void MatMul(MATRIX* first, MATRIX* second, MATRIX* dest) { int c, r; // column and row counters for( c = 0; c < 4; c++ ) { for( r = 0; r < 4; r++ ) { dest->data[c][r] = first->data[0][r] * second->data[c][0] + first->data[1][r] * second->data[c][1] + first->data[2][r] * second->data[c][2] + first->data[3][r] * second->data[c][3]; } } } /////////////// void VecRotate(VECTOR* srcVector, VECTOR* destVector, float x, float y, float z) { float xSin, xCos, ySin, yCos, zSin, zCos; xSin = (float)sin(x * PI / 180); xCos = (float)cos(x * PI / 180); ySin = (float)sin(y * PI / 180); yCos = (float)cos(y * PI / 180); zSin = (float)sin(z * PI / 180); zCos = (float)cos(z * PI / 180); MATRIX xRotMat = { 1, 0, 0, 0, 0, xCos, xSin, 0, 0, -xSin, xCos, 0, 0, 0, 0, 1 }; MATRIX yRotMat = { yCos, 0, -ySin, 0, 0, 1, 0, 0, ySin, 0, yCos, 0, 0, 0, 0, 1 }; MATRIX zRotMat = { zCos, zSin, 0, 0, -zSin, zCos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; MATRIX temp, final; MatMul(&xRotMat, &yRotMat, &temp); MatMul(&temp, &zRotMat, &final); destVector->data[0] = final.data[0][0] * srcVector->data[0] + final.data[1][0] * srcVector->data[1] + final.data[2][0] * srcVector->data[2] + final.data[3][0]; destVector->data[1] = final.data[0][1] * srcVector->data[0] + final.data[1][1] * srcVector->data[1] + final.data[2][1] * srcVector->data[2] + final.data[3][1]; destVector->data[2] = final.data[0][2] * srcVector->data[0] + final.data[1][2] * srcVector->data[1] + final.data[2][2] * srcVector->data[2] + final.data[3][2]; }
acw83
acw83
Thanks for the ideas. Before I even worry about optimizations though, I just need to know if I have this right. Are the matrices set up with the proper values in the right places? Am I missing anything? I''ve tested this code but get some really weird results(Vertices skipping a lot between degrees etc...). Any help appreciated.
goltrpoat
goltrpoat
yup, looks right to me. make sure you''re not doing additive rotations, i.e. keep a copy of the original vertex and increase the rotation angle by a constant increment instead of rotating the same vertex over and over by a constant angle. can you elaborate a bit more on how exactly are the results "weird"?

-goltrpoat


--
Float like a butterfly, bite like a crocodile.

--Float like a butterfly, bite like a crocodile.
acw83
acw83
Actually, I fixed it, thanks for the help!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.