• Advertisement
Sign in to follow this  

Camera view matrix issue

This topic is 1248 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 faced with the issue when creating my camera in SharpDX.

Whaen I create camera instance I could set any camera lookAt vector and it works, but when I rebuild view matrix with other VectorUP (form Y to Z for ex) camera starts to look at the center of coordinates from the same position, but it must only change its orientation and not more.

public void SetUp(Vector3 _up)
            Matrix tmpMatrix = ViewMatrix;

            Vector3 currentLookAt = new Vector3(tmpMatrix.M13, tmpMatrix.M23, tmpMatrix.M33);

            ViewMatrix = Matrix.LookAtLH(position, currentLookAt, _up);

Vector3 scale;
            Quaternion rotation;
            Vector3 translation;

            ViewMatrix.Decompose(out scale, out rotation, out translation);
            rotation = Quaternion.Normalize(rotation);
 ViewMatrix = Matrix.Multiply(Matrix.Translation(Vector3.Negate(position)), Matrix.RotationQuaternion(rotation));
            xAxis = new Vector3(ViewMatrix.M11, ViewMatrix.M21, ViewMatrix.M31);
            yAxis = new Vector3(ViewMatrix.M12, ViewMatrix.M22, ViewMatrix.M32);
            zAxis = new Vector3(ViewMatrix.M13, ViewMatrix.M23, ViewMatrix.M33);

Can someone say what I am doing wrong here? 

Share this post

Link to post
Share on other sites

A transformation matrix expresses a position and an orientation (letting scaling aside here). The function Matrix.LookAtLH(...) expects the camera position, the target position, and the up vector as arguments. Your routine SetUp(...) is written so that it tries to read the target position (named currentLookAt therein) from the view's transformation matrix. But there is nothing within the matrix that stores those position, and there is not enough information available to reconstruct it. The only position within that matrix is the own (i.e. view's) position.


In other words, this line

ViewMatrix = Matrix.LookAtLH(position, currentLookAt, _up);

doesn't give you what you expect.


So the routine SetUp(...) need to be revised. I suggest to do something like the following:

1.) Get the scaling values from the matrix.

2.) Get the forward vector from the matrix.

3.) Compute the new side vector by using the cross-product of forward and new up vectors.

4.) Scale the three direction vectors accordingly to the values calculated in 1.)

5.) Insert the scaled up and new side vectors into the matrix.

Edited by haegarr

Share this post

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

  • Advertisement