|
Who needs this function ?
--------------------------
Users that are porting code from other 3D engines might want to use this function.
All the rest can skip this function
General background
------------------
Please note that when moving/turning an object the STATE engine never modifies the X,Y,Z values of the points of the polygons of a dynamic object.
When the object is rotated scaled or moved, the engine only modify the object matrix.
Not modifing all the object points saves a lot of time.
What is this matrix ?
---------------------
To learn more about transformation matrixes you should read a computer graphics math book.
This is just if you are interested in the subject. One of the great things about the STATE engine
is that it makes it completely unnecessary to learn these subjects.
Anyhow, since you are still reading it means that you want to know more, so here it is:
the first top 9 cells of the matrix are the rotation matrix
The bottom row is the translation values.
The diagnal is the scale values
S R R 0
R S R 0
R R S 0
T T T 1
Above is the matrix. T stands for translation. S for scale. R for rotation.
If you don't know what we are talking about then you should read a book in the subject
By taking a point in object space and multiplying it by the matrix we get the point in World Space.
for example the point (0,0,0) in object space is the center of the object.
when multiplying (0,0,0) by the matrix we get (T T T) which means that
(T,T,T) is the object location
The code below shows how to multiply a point (vec) by the matrix (mat).
The result is saved in dst.
void mul_vec_mat(vector dst, vector vec, matrix mat) does dst=vec*mat
{
double res[4];
double one_over_res3;
res[0]=vec[0]*mat[0][0] + vec[1]*mat[1][0] + vec[2]*mat[2][0] + mat[3][0];
res[1]=vec[0]*mat[0][1] + vec[1]*mat[1][1] + vec[2]*mat[2][1] + mat[3][1];
res[2]=vec[0]*mat[0][2] + vec[1]*mat[1][2] + vec[2]*mat[2][2] + mat[3][2];
res[3]=vec[0]*mat[0][3] + vec[1]*mat[1][3] + vec[2]*mat[2][3] + mat[3][3];
if(res[3]<1e-20 && res[3]>-1e-20) { protect agains floating point errors.
ERROR_MSG(SCREEN,"mul_vec_mat: devision by zero\n");
dst[0]=res[0];
dst[1]=res[1];
dst[2]=res[2];
return;
}
one_over_res3=1/res[3];
dst[0]=res[0]*one_over_res3;
dst[1]=res[1]*one_over_res3;
dst[2]=res[2]*one_over_res3;
}
Example:
double matrix[4][4];
STATE_object_get_transformation_matrix(my_object, matrix);
|