Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
// code to multiply matrices (concatenation)
void multiply_matrices(float a[4][4], float b[4][4])
{
float tmp[4][4];
int i, j;
for (j = 0; j < 4; j++)
for (i = 0; i < 4; i++)
tmp[i][j] = a[i][0] * b[0][j]
+a[i][1] * b[1][j]
+a[i][2] * b[2][j]
+a[i][3] * b[3][j];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
a[i][j] = tmp[i][j];
}
// multiply with:
// x-rotation
// 1 0 0 0
// 0 cos(x) sin(x) 0
// 0 -sin(x) cos(x) 0
// 0 0 0 1
// y-rotation
// cos(y) 0 -sin(y) 0
// 0 1 0 0
// sin(y) 0 cos(y) 0
// 0 0 0 1
// z-rotation
// cos(z) sin(z) 0 0
// -sin(z) cos(z) 0 0
// 0 0 1 0
// 0 0 0 1
// Maths required to transform a vector using a matrix
// x0 etc are the original object space coords for the vector
x = x0 * a[0][0] + y0 * a[1][0] + z0 * a[2][0] + a[3][0];
y = x0 * a[0][1] + y0 * a[1][1] + z0 * a[2][1] + a[3][1];
z = x0 * a[0][2] + y0 * a[1][2] + z0 * a[2][2] + a[3][2];
// You may need to look into homogenous coordinate systems...
void multiply_matrices(float a[4][4], float b[4][4])
{
float tmp[4][4];
int i, j;
for (j = 0; j < 4; j++)
for (i = 0; i < 4; i++)
tmp[i][j] = a[i][0] * b[0][j]
+ a[i][1] * b[1][j]
+ a[i][2] * b[2][j]
+ a[i][3] * b[3][j];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
a[i][j] = tmp[i][j];
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
To rotate a 2D matrix, I assume we're talking about right-angled rotations, like :
0 1 2 3
4 5 6 7
8 9 A B
C D E F
becomes the following after a rotation over 90 degrees clockwise :
C 8 4 0
D 9 5 1
E A 6 2
F B 7 3
By looking at that example, it should be clear which positions move where when rotating, and implementing a matrix rotation shouldn't be too difficult.