Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Camera view matrix issue

Started by BlackJoker 1 reply 1.5k views
Original Post
BlackJoker
BlackJoker

Hello.

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?

BlackJoker
BlackJoker

This issue is not actual already.

haegarr
haegarr

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.

Topic Locked

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

Sign in to reply to this topic.