### Matrix

Posted:

**Sun Mar 01, 2009 4:58 pm**Basic Matrix library for everybody that doesn't want to wait for BeRo's physics implementation ( I assume these functions are in there somewhere as well ). Uses ZGE's build-in Euler rotation space ( a Quaternion based Lib is coming up ).

The function takes the local space coordinates of a vector relative to the rotation pivot ( usually the mesh center unless you're colliding with something .. or you could be doing something like weight transferring ), and the euler rotation.

*Since there's no way to return Arrays you'll have to fetch the results from the Matrix Array yourself.

Useful for calculating rotated 3D Bounding Boxes / Triangles / Vertices coordinates and setting up your own Raycast behavior

Code: Select all

```
void RotationMatrixX(int K, float Angle)
{
float C, S;
C = cos(Angle);
S = sin(Angle);
Matrix[K,0,0] = 1;
Matrix[K,0,1] = 0;
Matrix[K,0,2] = 0;
Matrix[K,1,0] = 0;
Matrix[K,1,1] = C;
Matrix[K,1,2] = S*-1;
Matrix[K,2,0] = 0;
Matrix[K,2,1] = S;
Matrix[K,2,2] = C;
}
void RotationMatrixY(int K, float Angle)
{
float C, S;
C = cos(Angle);
S = sin(Angle);
Matrix[K,0,0] = C;
Matrix[K,0,1] = 0;
Matrix[K,0,2] = S;
Matrix[K,1,0] = 0;
Matrix[K,1,1] = 1;
Matrix[K,1,2] = 0;
Matrix[K,2,0] = S*-1;
Matrix[K,2,1] = 0;
Matrix[K,2,2] = C;
}
void RotationMatrixZ(int K, float Angle)
{
float C, S;
C = cos(Angle);
S = sin(Angle);
Matrix[K,0,0] = C;
Matrix[K,0,1] = S*-1;
Matrix[K,0,2] = 0;
Matrix[K,1,0] = S;
Matrix[K,1,1] = C;
Matrix[K,1,2] = 0;
Matrix[K,2,0] = 0;
Matrix[K,2,1] = 0;
Matrix[K,2,2] = 0;
}
//
void MultiplyMatrix(int K, int A, int B)
{
Matrix[K,0,0] = Matrix[A,0,0]*Matrix[B,0,0]+Matrix[A,0,1]*Matrix[B,1,0]+Matrix[A,0,2]*Matrix[B,2,0];
Matrix[K,0,1] = Matrix[A,0,0]*Matrix[B,0,1]+Matrix[A,0,1]*Matrix[B,1,1]+Matrix[A,0,2]*Matrix[B,2,1];
Matrix[K,0,2] = Matrix[A,0,0]*Matrix[B,0,2]+Matrix[A,0,1]*Matrix[B,1,2]+Matrix[A,0,2]*Matrix[B,2,2];
Matrix[K,1,0] = Matrix[A,1,0]*Matrix[B,0,0]+Matrix[A,1,1]*Matrix[B,1,0]+Matrix[A,1,2]*Matrix[B,2,0];
Matrix[K,1,1] = Matrix[A,1,0]*Matrix[B,0,1]+Matrix[A,1,1]*Matrix[B,1,1]+Matrix[A,1,2]*Matrix[B,2,1];
Matrix[K,1,2] = Matrix[A,1,0]*Matrix[B,0,2]+Matrix[A,1,1]*Matrix[B,1,2]+Matrix[A,1,2]*Matrix[B,2,2];
Matrix[K,2,0] = Matrix[A,2,0]*Matrix[B,0,0]+Matrix[A,2,1]*Matrix[B,1,0]+Matrix[A,2,2]*Matrix[B,2,0];
Matrix[K,2,1] = Matrix[A,2,0]*Matrix[B,0,1]+Matrix[A,2,1]*Matrix[B,1,1]+Matrix[A,2,2]*Matrix[B,2,1];
Matrix[K,2,2] = Matrix[A,2,0]*Matrix[B,0,2]+Matrix[A,2,1]*Matrix[B,1,2]+Matrix[A,2,2]*Matrix[B,2,2];
}
//
void Rotate(float VX, float VY, float VZ, float AX, float AY, float AZ)
{
RotationMatrixX(1,AX);
RotationMatrixY(2,AY);
RotationMatrixZ(3,AZ);
MultiplyMatrix(4,1,2);
MultiplyMatrix(5,4,3);
Matrix[0,0,0] = VX*Matrix[5,0,0]+VY*Matrix[5,0,1]+VZ*Matrix[5,0,2];
Matrix[0,1,0] = VX*Matrix[5,1,0]+VY*Matrix[5,1,1]+VZ*Matrix[5,1,2];
Matrix[0,2,0] = VX*Matrix[5,2,0]+VY*Matrix[5,2,1]+VZ*Matrix[5,2,2];
}
```

K