Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with matrix !

Started by artoisval Oct 15, 2005 at 4:26 PM 6 replies 950+ views
Original Post
artoisval
artoisval
Hi all, im kinda newbie with matrix and 3d math, but i succesfully implemented my 3rd person camera, doing some simple math. However since 3d math isnt my best skill i'm stuck at getting the camera position. Here is the code The x,y,z is the position of the where the cam is looking, ie : The back of the avatar head ...

MATRIX4X4 rMat;
		 
	  m_mat.LoadIdentity();
	    
	  m_mat.matrix[12]=0;
	  m_mat.matrix[13]=0;
	  m_mat.matrix[14]=myCam.m_camDist*4;
	 	
	  rMat.LoadRoationX( ( ( -gfkCamXAngle) * piover180 ) );
	  m_mat = m_mat * rMat;

 	  rMat.LoadRoationY( ( ( -gfkCamYAngle) * piover180 ) );
	  m_mat = m_mat * rMat;
	
	  rMat.LoadIdentity();
		
	  rMat.matrix[12] =- x;
	  rMat.matrix[13] =- (y-myCam.m_camDist);
	  rMat.matrix[14] =- ( z );
	
	  m_mat = m_mat * rMat;


The matrix is loaded into OpenGl later when i render the scene. But i whant to know where the cam. And i will appreciate any help or tip from pro ;-)
Zakwayda
Zakwayda
I think I follow what you're doing. The composite camera matrix first translates the world so that the player is at the origin, then rotates the world, then translates the world along the z axis to put the camera behind the player. I haven't done much with 3rd-person cameras, so I won't comment on the method itself. As for finding the camera position though, I think it will just be the fourth column of M-1, where M is your composite camera matrix. Although it wouldn't be the most efficient way to do it, if you have a matrix inversion function you could just invert M and extract the translation vector. If you don't have such a function but are interested in this method, I could explain a shortcut that will do the same thing. (I'm not 100% sure though that my answer is correct...)
ury
ury
Exactly what jyk said. :)

Instead of inverting the matrix, we can exploit its propeties and find the solution like this:

Let's start by giving names.
Here is M:
( R R R X )
( R R R X )
( R R R X )
( 0 0 0 1 )

R is 3x3 sub-matrix.
X is a 3d vector.

Since R is orthogonal, the position (X of M-1), is given by:
position = -Rt*X

I guess that this is the shortcut that jyk was refering to.
Zakwayda
Zakwayda
Quote:
I guess that this is the shortcut that jyk was refering to.
Indeed it is :-)
artoisval
artoisval
Thanks for the help ;-) So all i do is multiply the sub 3x3 matrix vs the X part ?


I will check into it and keep you inform.

Zakwayda
Zakwayda
Quote:
Thanks for the help ;-) So all i do is multiply the sub 3x3 matrix vs the X part ?
Almost, but take a closer look at ury's solution: it's the transpose of the 3x3 submatrix and the negative of the X part.
artoisval
artoisval
I finally managed to get the cam position. As I said, I'm like Homer Simpson with math. It's been about a year since I really did intesive matrix math, and six months since I touched the cam code. I know it's not the best way to do cam code but it worked for my little game engine.

I used one of your functions to get the inverse of a matrix (you posted it on another topic), it's worked out of the box after a cut and paste ;-) (I hope you dont mind).

I must look in my notes about anything related to matrix.

Now I'm working to make the cam slide on the ground (it was passing through it). I get the cam intersect position and "block" it on the Y.

Thanks again !
Zipster
Zipster
You really don't need to get the general inverse of the matrix, or any homogeneous transformation matrix you'll be working with. Just keep in mind what ury said. If you have the matrix:

( R | T )
( ----- )
{ 0 | 1 )

Where R and T are your 3x3 rotation matrix translation vector, respectively, then:

( RT | -RTT )
( --------- )
( 0 | 1 )

Is your inverse matrix. The vertical and horizontal lines represent a way of writing matrices in terms of other matrices and vectors. You can still use the general inverse code if you want, but it's more complex and you have to watch out for degenerate cases.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.