Jump to content
  • Advertisement
Sign in to follow this  
SteveDeFacto

Inverse of 4x4 matrix

This topic is 2782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.

SUB PhysicsInvertMatrix (Dest AS SINGLE PTR, Source AS SINGLE PTR)
DIM X AS INTEGER
DIM Y AS INTEGER
DIM Index AS INTEGER
DIM Minor(11) AS SINGLE
DIM Adjoint(11) AS SINGLE
DIM AS SINGLE Determinant = Source[0] * (Source[5] * Source[10] - Source[9] * Source[6]) - _
Source[4] * (Source[1] * Source[10] - Source[9] * Source[2]) + _
Source[8] * (Source[1] * Source[6] - Source[5] * Source[2])
DIM AS SINGLE DetRec = 1.0 / Determinant

Minor(0) = Source[5] * Source[10] - Source[9] * Source[6]
Minor(1) = Source[4] * Source[10] - Source[8] * Source[6]
Minor(2) = Source[4] * Source[9] - Source[8] * Source[5]
Minor(4) = Source[1] * Source[10] - Source[9] * Source[2]
Minor(5) = Source[0] * Source[10] - Source[8] * Source[2]
Minor(6) = Source[0] * Source[9] - Source[8] * Source[1]
Minor(8) = Source[1] * Source[6] - Source[5] * Source[2]
Minor(9) = Source[0] * Source[6] - Source[4] * Source[2]
Minor(10) = Source[0] * Source[5] - Source[4] * Source[1]

'Shouldn't I be multiplying each of these by DetRec and...
Adjoint(0) = Minor(0)
Adjoint(1) = -Minor(4)
Adjoint(2) = Minor(8)
Adjoint(4) = -Minor(1)
Adjoint(5) = Minor(5)
Adjoint(6) = -Minor(9)
Adjoint(8) = Minor(2)
Adjoint(9) = -Minor(6)
Adjoint(10) = Minor(10)

'...getting rid of these loops?
FOR Y = 0 TO 2
FOR X = 0 TO 2
Index = Y * 4 + X
Dest[Index] = DetRec * Adjoint(Index)
NEXT X
NEXT Y

Dest[3] = Source[3]
Dest[7] = Source[7]
Dest[11] = Source[11]
END SUB

Share this post


Link to post
Share on other sites
Quote:
Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


a little confused what is going on in that function...

Share this post


Link to post
Share on other sites
Quote:
Original post by SteveDeFacto
Quote:
Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


a little confused what is going on in that function...

Yeah, me too. It works though. It takes two pointers to a source and a destination matrix. Here's an example of it's use:

DIM AS SINGLE PTR rotation = dBodyGetRotation (BodyID)
DIM AS SINGLE inverse(12)

PhysicsInvertMatrix (@inverse(0), rotation)

*I'm using SINGLE (float in C++) to match the parameters of the function though this wouldn't be recommended when using ODE functions (use dReal instead) but that's a different topic.

I painstakingly pieced this function together from what information I could glean from information online about inverse matrices. I've read the proof but this was two years ago and I have absolutely no idea how this works anymore. There's plenty of info out there if you really want to get into the specifics. But, for all intents and purposes, this does the job.

Share this post


Link to post
Share on other sites
For such problems there is a very nice book called Numerical Recipes. The older version is available online.

Quote:
Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


The indexes go from 0 to 11 in that algorithm, so it can't compute the inverse of a general 4x4 matrix. It probably assumes that the lat row has a know from, maybe 0,0,0,1 .

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
For such problems there is a very nice book called Numerical Recipes. The older version is available online.

Quote:
Original post by coderx75
Here's a BASIC implementation used for inverting an ODE matrix for rendering in OpenGL.
*** Source Snippet Removed ***


The indexes go from 0 to 11 in that algorithm, so it can't compute the inverse of a general 4x4 matrix. It probably assumes that the lat row has a know from, maybe 0,0,0,1 .

Yes, this is for use with ODE matrices which are 4x3. For most purposes, a set of 0, 0, 0, 1 for the missing row works.

@kloffy: Excellent paper. I hadn't come across that one before but it explains it better than anything I had read.

Share this post


Link to post
Share on other sites
Solving by using the cofactors is actually much harder and laborious than solving it directly.

Inverting a square matrix is the same as solving a system of four equations. This can be easily done by appending the identity matrix and converting the left half to reduced row echelon form.

Example:

Find inverse of:

[ a b c d ]
| e f g h |
| i j k l |
[ m n o p ]

1. So append the identity matrix:

[ a b c d | 1 0 0 0 ]
| e f g h | 0 1 0 0 |
| i j k l | 0 0 1 0 |
[ m n o p | 0 0 0 1 ]

2. Convert the left half to the identity matrix:

[ 1 0 0 0 | a' b' c' d' ]
| 0 1 0 0 | e' f' g' h' |
| 0 0 1 0 | i' j' k' l' |
[ 0 0 0 1 | m' n' o' p' ]

The right hand side is your matrix inverse:

[ a' b' c' d' ]
| e' f' g' h' |
| i' j' k' l' |
[ m' n' o' p' ]

Step 2 is the meat and potatoes of your problem, but is far less work than finding the cofactor matrix.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!