Getting the Camera Position from the View Matrix

Started by
3 comments, last by deadlydog 15 years, 5 months ago
I'm using C#/XNA and am wondering if it is possible to get the position of the Camera (in world coordinates) from the View Matrix or not, and if so, how do you do it. Thanks.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Advertisement
The last column of the view matrix describes the position of the camera in world space.
GBS
That's what I thought originally, but the position in the last column of the View Matrix has the untransformed coordinates (before any rotations are applied). Here is how I calculate my view matrix (taken from the XNA Particle3D Sample):

// Set up the View matrix according to the Camera's arc, rotation, and distance from the Offset positioncViewMatrix = Matrix.CreateTranslation(msCamera.cFixedCameraLookAtOffset) *Matrix.CreateRotationY(MathHelper.ToRadians(msCamera.fCameraRotation)) *Matrix.CreateRotationX(MathHelper.ToRadians(msCamera.fCameraArc)) *Matrix.CreateLookAt(new Vector3(0, 0, -msCamera.fCameraDistance),                    new Vector3(0, 0, 0), Vector3.Up);


Basically my Camera is fixed to always look at the msCamera.cFixedCameraLookAtOffset Position (which is (0, 50, 0), right above the origin). I then Yaw the Camera according to the CameraRotation value and Pitch it according to the CameraArc value. This allows me to rotate around the FixedCameraLookAtOffset Position while always looking at it. After performing the above operations and looking at the last column of the ViewMatrix, instead of holding the (x,y,z) values of the Camera Position it seems to be holding (0 , CameraArc, CameraDistance). So for example, rotating the Camera around the origin does not change the values in the last column at all. What am I missing here? Thanks.
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Quote:The last column of the view matrix describes the position of the camera in world space.
That's incorrect. However, if you invert the view matrix, the last column of the resulting matrix (or row, depending on convention) should then contain the camera position.
Quote:Original post by jyk
Quote:The last column of the view matrix describes the position of the camera in world space.
That's incorrect. However, if you invert the view matrix, the last column of the resulting matrix (or row, depending on convention) should then contain the camera position.


Thank you so much, that solved my problem!!
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA

This topic is closed to new replies.

Advertisement