## Matrix

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Kjell
Posts: 1721
Joined: Sat Feb 23, 2008 11:15 pm

### Matrix

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];
}``````
Attached a simple implementation.

K
Attachments
Matrix.zgeproj

diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:
kjell, thanks for this nice library! i could not get my head around this topic before.

i've got one question, though: when trying to rotate a z-vector only by its y-axis (like: Rotate( 0, 0, 1, 0, App.CameraRotation.Y * Pi * 2, 0 )), i only get 0 for Matrix[0, 0, 0] and [0, 2, 0]. i worked around this by simply using an x-vector and substituting accordingly, but i wonder where my thinking went wrong; maybe you spot it immediately :)

the code snippet that works for me right now is this (pushing the camera along happily):

Code: Select all

``````Rotate( 10, 0, 0, 0, App.CameraRotation.Y * Pi * 2, 0 );

if( CamKeys.KeyIndex == 0 ){ // W, forward
App.CameraPosition.X = App.CameraPosition.X -( Matrix[ 0, 2, 0 ] * App.DeltaTime );
App.CameraPosition.Z = App.CameraPosition.Z -( Matrix[ 0, 0, 0 ] * App.DeltaTime );
}``````

Kjell
Posts: 1721
Joined: Sat Feb 23, 2008 11:15 pm
Hi diki,

Can't believe this mistake has gone unnoticed for all this time. Change the 0 of "Matrix[K,2,2] = 0;" of "RotationMatrixZ" into 1 .. should do the trick.

Entschuldigung ~
K

diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:
Kjell wrote:Entschuldigung ~
hehe, no harm done :)

diki
Posts: 140
Joined: Thu Sep 11, 2008 7:53 pm
Location: GMT+1
Contact:
i've been trying to expand this library to 4x4-matrices so it could be used for calculating homogenous coordinates; since i don't know math, that is quite the mental challenge for me (the implementation would not be too hard). i'm stuck at the following: while i can find & understand the multiplication matrices needed to translate, rotate, scale etc., i don't know how to get the corresponding data back out of the matrix. i guess it is an obvious 'reverse' operation for people knowing their way around this topic; could you give me a hint/link/google keyword to search for?

AndersO
Posts: 4
Joined: Mon Feb 27, 2017 7:47 pm