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.

Recommended Posts

Can someone show me how to do this? I tried to google it but I could not understand anything I read...

Share on other sites
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 on other sites
Quote:
 Original post by coderx75Here'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 on other sites
Quote:
Original post by SteveDeFacto
Quote:
 Original post by coderx75Here'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 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 coderx75Here'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 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 coderx75Here'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 on other sites
I'm still confused. Is there a simple way to think about it?

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.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
17
5. 5

• 10
• 14
• 11
• 9
• 9
• Forum Statistics

• Total Topics
631756
• Total Posts
3002114
×