SharpDX Camera Snapping

Viewed 18

So i am making a game engine with SharpDX!

Now my problem is when i rotate the camera above 90° or below -90° it snaps and flips on the X.

How can i prevent this?

This is my code:

float rotationX = 0;
        float rotationY = 0;
        float dist = 1;

        Window.MouseMove += (s, e) =>
        {
            float cos(float b) { return (float)Math.Cos(b); }
            float sin(float b) { return (float)Math.Sin(b); }

            if (e.Button == MouseButtons.Left)
            {
                var yRotate = lastX - e.X;
                var xRotate = lastY - e.Y;
                rotationX += xRotate * moveFactor;
                rotationY += yRotate * moveFactor;
                lastY = e.Y;
                lastX = e.X;

                float h = cos(rotationX) * dist;
                Vector3 cameraTarget = new Vector3(cos(rotationY) * h, sin(rotationX) * dist, sin(rotationY) * h);

                viewMatrix = Matrix.LookAtRH(cameraPosition, cameraPosition + cameraTarget, cameraUp);
            }
        };

Any Help appreciated!

0 Answers
Related