Sign in to follow this  

Getting the Camera Position from the View Matrix

This topic is 3338 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

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.

Share this post


Link to post
Share on other sites
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 position
cViewMatrix = 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites

This topic is 3338 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this