XNA: get Vector3 from matrix

I thought I understood matrix mathematics well enough, but apparently I don’t know

Here's the setting:

I have an object in [0,0,0] in world space. I have a camera class controlled by mouse movements to rotate and zoom in on an object so that it always looks at it. This is how I calculate my viewMatrix from the camera:

public Matrix viewMatrix { 
    get {
        return 
            Matrix.CreateFromAxisAngle(Vector3.Up, rotAngle)
                * Matrix.CreateFromAxisAngle(Vector3.Left, pitchAngle)
                * Matrix.CreateTranslation(0, 0, distance) 
        ;
    }
}

I need to get the position of the camera in world space so that I can distance myself from the box - especially its distance from each side of the box. How to get xyz camera position in world space coordinates?

I tried:

// all of these only return [0, 0, distance];
Vector3 pos = Vector3.Transform(Vector3.Zero, viewMatrix);
Vector3 pos = viewMatrix.Translation;
Vector3 pos = new Vector3(viewMatrix.M41, viewMatrix.M42, viewMatrix.M43);

It seems that rotation information is somehow lost. It is strange that the viewMatrix code works great for positioning the camera!

+3
2

:

Vector3 pos = Matrix.Invert(view).Translation;

+7

, :

. , , . "" .

Vector3 pos = Vector3.Transform(Vector3.Zero, Matrix.Invert(viewMatrix));
+2

Source: https://habr.com/ru/post/1761007/


All Articles