• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

ZMaster

Matrix Inverse

15 posts in this topic

Hi, I''m looking for a good algo to calculate the inverse of a 4x4 matrix. I didn''t find anything good with google. I mean, I can have a lot of information about calculating the inverse of a matrix on paper, but that''s mostly impossible to implement as C code So how do you guys calculate matrix inverses in your engines/games/projects? Any code or information would be useful. Thanks, Flo
0

Share this post


Link to post
Share on other sites
code snipeds:


//#define _float double

#ifndef _float
#define _float float
#endif

class MATRIX{// Last row is 0, 0, 0, 1, else change class functions( remove // and /* ), frustum changes last row

union{
_float m[4*4]; // m[x][y]= [((x-1)*4) + (y-1)]

struct { // mxy !!!(in definition/here reversed)=>

_float m11, m12, m13, m14, // m11 m21 m31 m41

m21, m22, m23, m24, // m12 m22 m32 m42

m31, m32, m33, m34, // m13 m23 m33 m43

m41, m42, m43, m44; // m14 m24 m34 m44

};
};
...



_float MATRIX::GetM3Determinant(const MATRIX3 &matrix){
return ( matrix.x.x * (matrix.y.y * matrix.z.z - matrix.y.z * matrix.z.y) -
matrix.y.x * (matrix.x.y * matrix.z.z - matrix.x.z * matrix.z.y) +
matrix.z.x * (matrix.x.y * matrix.y.z - matrix.x.z * matrix.y.y) );
}

MATRIX::MATRIX3 MATRIX::GetSubMatrix(long x, long y){
MATRIX3 tmp;

long xoffset= 0;
for(long i=0; i < 4; i++)
if(i != x){
long yoffset= 0;
for(long j= 0; j < 4; j++)
if(j != y){
*( ((_float *) &tmp) + xoffset*3 + yoffset)= m[i*4 + j];
yoffset++;
}
xoffset++;
}
return tmp;
}

_float MATRIX::GetDeterminant(){
_float result= 0;
long i= 1;

MATRIX3 tmp;
for(long n= 0; n < 4; n++, i*=-1){
tmp= GetSubMatrix(0, n);
result+= m[n] * GetM3Determinant(tmp) * i;
}
return result;
}


MATRIX MATRIX::GetCorrectInverse(){
MATRIX inverse;
MATRIX3 tmp;

_float m4determinant= GetDeterminant();

if(fabs(m4determinant) < bias)
inverse= MATRIX(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);// Matrix has no inverse

else{
for( long i=0; i < 4; i++)
for(long j=0; j < 4; j++){
long sign= 1-((i+j)%2)*2;
tmp= GetSubMatrix(i, j);
inverse.m[i + j*4]= GetM3Determinant(tmp) * sign / m4determinant;
}
}
return inverse;
}



i hope you can read the important part, but i think i will edit my post in few minutes...

edit:

class VECTOR3D{
public:
_float x, y, z;
...


struct MATRIX3{
VECTOR3D x,y,z;
};

uhm i dont think it will compile, i have not posted overloaded operators etc...


edit2:

_float MATRIX::GetDeterminant(){//of a 4x4 Matrix!

_float result= 0;
long i= 1;

MATRIX3 tmp;
for(long n= 0; n < 4; n++, i*=-1){
tmp= GetSubMatrix(0, n);
result+= m[n] * GetM3Determinant(tmp) * i;
}
return result;
}

edit3: added source tags (this board eats them...


T2k

[edited by - T2k on September 13, 2003 6:36:55 AM]

[edited by - T2k on September 13, 2003 6:40:18 AM]

[edited by - T2k on September 13, 2003 6:43:13 AM]
0

Share this post


Link to post
Share on other sites
That''s a bit of code
I think I''ll have to read this many times to understand.

So do you obtain submatrices until you get a 2x2 matrix and calculate the determinants like this, or what?

|a b|
|c d| det = ab - cd

I''ll try to implement this in my matrix class then

0

Share this post


Link to post
Share on other sites
Please, help me remember what inversed matrix are used for ? Inverse kynematic maybe ? and what else ?
0

Share this post


Link to post
Share on other sites
Just an observation. While it is important to have a good general method for finding an inverse, always keep in mind that there are several special cases that can greatly simplify things.

For example, if a transformation matrix is built only from rotations, then the inverse of the matrix is guaranteed to be the transpose.

You can also often get an inverse matrix by inverting parameters. For example, a matrix given by
glTranslatef(10.0,10.0,10.0) is guaranteed to be the inverse of
glTranslatef(-10.0, -10.0, -10.0).


[edited by - UnIcron on September 15, 2003 12:20:12 PM]
0

Share this post


Link to post
Share on other sites
MV, in OpenGL is the most common use of the inverse if you want to set the camera. Set it as an object but invert before sendinf it to OpenGL.
0

Share this post


Link to post
Share on other sites
In that case, isn''t it easier to store the camera''s position and orientation as a vector and a quaternion ?


SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.
The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
0

Share this post


Link to post
Share on other sites
Well, thanks. I got a good routine now. I''m building submatrices (first 3x3, then 2x2) and just calculate the inverse of those (like T2k suggested).

Thank for the tips UnIcron. Should I implement some different routines for performance reason? I''ll think about that - maybe I should test it

Basicly: The matrix multiplied by it''s inverse equals the identity matrix.
I don''t have a practical example in mind now, but you must sometimes use a matrix inverse when converting coordinates to different spaces. I''m coding some math classes for my engine, and I wanted to implement an Invert routine.

@rodzilla: That''s easier, sure, but it always depends on what camera you want to set up. I finished my 3rd person camera class some minutes ago. I use matrices in it very frequently. There is a function Perspective(), that calculates a perspective projection matrix and uses it in OpenGL (just like gluPerspective) for example. Moreover I have a matrix that takes the camera transforms (rotation, translation). I''s multiplied with the current modelview matrix every frame, so I could easily write a function, that automatically tracks an object (like glLookAt, but more automatic) just by making some changes to the camera transform matrix.
0

Share this post


Link to post
Share on other sites
float Determinant4f(const float m[16])
{
return
m[12]*m[9]*m[6]*m[3]-
m[8]*m[13]*m[6]*m[3]-
m[12]*m[5]*m[10]*m[3]+
m[4]*m[13]*m[10]*m[3]+
m[8]*m[5]*m[14]*m[3]-
m[4]*m[9]*m[14]*m[3]-
m[12]*m[9]*m[2]*m[7]+
m[8]*m[13]*m[2]*m[7]+
m[12]*m[1]*m[10]*m[7]-
m[0]*m[13]*m[10]*m[7]-
m[8]*m[1]*m[14]*m[7]+
m[0]*m[9]*m[14]*m[7]+
m[12]*m[5]*m[2]*m[11]-
m[4]*m[13]*m[2]*m[11]-
m[12]*m[1]*m[6]*m[11]+
m[0]*m[13]*m[6]*m[11]+
m[4]*m[1]*m[14]*m[11]-
m[0]*m[5]*m[14]*m[11]-
m[8]*m[5]*m[2]*m[15]+
m[4]*m[9]*m[2]*m[15]+
m[8]*m[1]*m[6]*m[15]-
m[0]*m[9]*m[6]*m[15]-
m[4]*m[1]*m[10]*m[15]+
m[0]*m[5]*m[10]*m[15];
}

BOOL GenerateInverseMatrix4f(float i[16], const float m[16])
{
float x=Determinant4f(m);
if (x==0) return FALSE;

i[0]= (-m[13]*m[10]*m[7] +m[9]*m[14]*m[7] +m[13]*m[6]*m[11]
-m[5]*m[14]*m[11] -m[9]*m[6]*m[15] +m[5]*m[10]*m[15])/x;
i[4]= ( m[12]*m[10]*m[7] -m[8]*m[14]*m[7] -m[12]*m[6]*m[11]
+m[4]*m[14]*m[11] +m[8]*m[6]*m[15] -m[4]*m[10]*m[15])/x;
i[8]= (-m[12]*m[9]* m[7] +m[8]*m[13]*m[7] +m[12]*m[5]*m[11]
-m[4]*m[13]*m[11] -m[8]*m[5]*m[15] +m[4]*m[9]* m[15])/x;
i[12]=( m[12]*m[9]* m[6] -m[8]*m[13]*m[6] -m[12]*m[5]*m[10]
+m[4]*m[13]*m[10] +m[8]*m[5]*m[14] -m[4]*m[9]* m[14])/x;
i[1]= ( m[13]*m[10]*m[3] -m[9]*m[14]*m[3] -m[13]*m[2]*m[11]
+m[1]*m[14]*m[11] +m[9]*m[2]*m[15] -m[1]*m[10]*m[15])/x;
i[5]= (-m[12]*m[10]*m[3] +m[8]*m[14]*m[3] +m[12]*m[2]*m[11]
-m[0]*m[14]*m[11] -m[8]*m[2]*m[15] +m[0]*m[10]*m[15])/x;
i[9]= ( m[12]*m[9]* m[3] -m[8]*m[13]*m[3] -m[12]*m[1]*m[11]
+m[0]*m[13]*m[11] +m[8]*m[1]*m[15] -m[0]*m[9]* m[15])/x;
i[13]=(-m[12]*m[9]* m[2] +m[8]*m[13]*m[2] +m[12]*m[1]*m[10]
-m[0]*m[13]*m[10] -m[8]*m[1]*m[14] +m[0]*m[9]* m[14])/x;
i[2]= (-m[13]*m[6]* m[3] +m[5]*m[14]*m[3] +m[13]*m[2]*m[7]
-m[1]*m[14]*m[7] -m[5]*m[2]*m[15] +m[1]*m[6]* m[15])/x;
i[6]= ( m[12]*m[6]* m[3] -m[4]*m[14]*m[3] -m[12]*m[2]*m[7]
+m[0]*m[14]*m[7] +m[4]*m[2]*m[15] -m[0]*m[6]* m[15])/x;
i[10]=(-m[12]*m[5]* m[3] +m[4]*m[13]*m[3] +m[12]*m[1]*m[7]
-m[0]*m[13]*m[7] -m[4]*m[1]*m[15] +m[0]*m[5]* m[15])/x;
i[14]=( m[12]*m[5]* m[2] -m[4]*m[13]*m[2] -m[12]*m[1]*m[6]
+m[0]*m[13]*m[6] +m[4]*m[1]*m[14] -m[0]*m[5]* m[14])/x;
i[3]= ( m[9]* m[6]* m[3] -m[5]*m[10]*m[3] -m[9]* m[2]*m[7]
+m[1]*m[10]*m[7] +m[5]*m[2]*m[11] -m[1]*m[6]* m[11])/x;
i[7]= (-m[8]* m[6]* m[3] +m[4]*m[10]*m[3] +m[8]* m[2]*m[7]
-m[0]*m[10]*m[7] -m[4]*m[2]*m[11] +m[0]*m[6]* m[11])/x;
i[11]=( m[8]* m[5]* m[3] -m[4]*m[9]* m[3] -m[8]* m[1]*m[7]
+m[0]*m[9]* m[7] +m[4]*m[1]*m[11] -m[0]*m[5]* m[11])/x;
i[15]=(-m[8]* m[5]* m[2] +m[4]*m[9]* m[2] +m[8]* m[1]*m[6]
-m[0]*m[9]* m[6] -m[4]*m[1]*m[10] +m[0]*m[5]* m[10])/x;

return TRUE;
}

[edited by - circuit on September 16, 2003 2:08:36 PM]
0

Share this post


Link to post
Share on other sites
rodzilla, I think that matrices are better for the camera because they are easier to set/read than quaternions for the average programmer.
http://www.sjbaker.org/steve/omniv/matrices_can_be_your_friends.html
0

Share this post


Link to post
Share on other sites
Here is another technique that I use for finding the inverse of a 4x4 matrix.
Note: the matrices used are the OpenGL Column major format

Step 1. Transpose the 3x3 rotation portion of the 4x4 matrix to get the inverse rotation

MatrixInvRotate(mtxin, mtxout);

the MatrixInvRotate code is:

void MatrixInvRotate(glMatrix mtxin, glMatrix mtxout)
{
for(int i=0 ; i<3; i++) {
for(int j=0; j<3; j++) {
mtxout[j][i] = mtxin[i][j];
}
}
}

Step 2. negate the translation vector portion of the 4x4 matrix and then rotate it using the newly constructed 3x3 rotation matrix

msVec3 vTmp, vTmp2;

vTmp[0] = -mtxin[3][0];
vTmp[1] = -mtxin[3][1];
vTmp[2] = -mtxin[3][2];

VectorRotate(vTmp, mtxout, vTmp2);


The VectorRotate code is straight forward:

void VectorRotate (const msVec3 vin, const glMatrix mtx, msVec3 vout)
{
vout[0] = vin[0]*mtx[0][0] + vin[1]*mtx[1][0] + vin[2]*mtx[2][0];
vout[1] = vin[0]*mtx[0][1] + vin[1]*mtx[1][1] + vin[2]*mtx[2][1];
vout[2] = vin[0]*mtx[0][2] + vin[1]*mtx[1][2] + vin[2]*mtx[2][2];
}

Step 3. put the new translation vector into the new 4x4 matrix

mtxout[3][0] = vTmp2[0];
mtxout[3][1] = vTmp2[1];
mtxout[3][2] = vTmp2[2];

Step 4. do some house cleaning so that the matrix can be used with OpenGL

mtxout[0][3] = mtxout[1][3] = mtxout[2][3] = 0.0f;
mtxout[3][3] = 1.0f;


You now have the inverse of the original 4x4 matrix.

I use inverse matrices for cameras, bone animation and skinning using bone weights, and object picking.

1

Share this post


Link to post
Share on other sites
quote:

I use inverse matrices for cameras, bone animation and skinning using bone weights, and object picking.



Could you make a bit more details ?
For bone animation : is it for inverse kynematics ?
For skinning using bone weights : are you using vertex buffers ?

0

Share this post


Link to post
Share on other sites
For bone animation the inverse matrix representing bone position and rotation is required for doing both forward and inverse kynematics. The inverse matrix of the bone position and rotation is also required for skinning because I store the vertices in object space rather than bone space since each vertex can be attached to more than one bone. Doing it this way makes for slightly simpler code that runs faster especially as a vertex program on a GPU(ie vertex shader).

For the skinning I support two rendering paths using the OpenGL API. If the card supports ARB vertex program then all the vertex attribute data (position vector, bone weights etc) is offloaded to the GPU and vertex blending is executed on the GPU and its very fast. If the graphics card has a fixed function pipeline then the vertex blending is done on the CPU so I don''t use vertex buffers.
0

Share this post


Link to post
Share on other sites
quote:
Original post by nfz
For bone animation the inverse matrix representing bone position and rotation is required for doing both forward and inverse kynematics.


What is forward kynematic ?

quote:
Original post by nfz
The inverse matrix of the bone position and rotation is also required for skinning because I store the vertices in object space rather than bone space since each vertex can be attached to more than one bone.


Yeah, that''s a problem for me... Did you deeply study the subject, and is it the only solution you found ? Cause using this method, you should make vertex per vertex matrix computation that should be very heavy in thems of CPU work. You should at least make 2 matrix multiplication for EACH vertex, in my case this should kill my frame rate


quote:
Doing it this way makes for slightly simpler code that runs faster especially as a vertex program on a GPU(ie vertex shader).

i''m afraid my english is a bit weak, so I didnt all understand :D, but I think this means the same I wrote before ???
0

Share this post


Link to post
Share on other sites
Forward kinematics (FK) is the propagation of motion from the parent joint down the chain to all of its children. An example is if the parent rotates then all the children rotate with the parent. FK is the simple part of kinematics to solve and is used in most applications and games that support bone animation.

Inverse kinematics (IK) is the reverse, ie if the child joint moves it effects its parent and on up the chain. IK in real time is much harder to solve and code and is more computationaly expensive so you don''t see it used in games much.

There are a few different ways of performing vertex blending and no I wouldn''t say I have studied it in depth so the way I am doing it now may not be the most efficient but it works and gives decent frame rates on middle of the road pc hardware. My target frame rate is 40fps with 10 animated objects(machines and creatures) in the forground at a screen resolution of 1024x768 24bpp using a GF2 and 1Ghz Athlon. Each model is low poly having about 800 triangles and the scenery has about 1800 visible triangles so in total there are no more than 10000 triangles being pumped through. On my dev machine at work (Radeon 8500 128 meg and AthlonXP 1.63 Ghz) I get about 130fps (CPU vertex blending) with each model having 4 animation streams being blended at the same time at a resolution of 1280x1024 32 bpp. Using a vertex shader and a vertex array on the GPU, the FPS jumps to about 270. Todays hardware allows these techniques to be used so now I am learning to use them. A year and a half ago I wouldn''t have considered it.

The bone matrix calculations are done at the beginning of each frame but before vertex processing. But, yes, vertex blending is expensive in CPU time and its a balance you have to play between good animation and frame rates.
0

Share this post


Link to post
Share on other sites