-1
\$\begingroup\$

PROBLEM

I am trying to translate and rotate my view, the translation works fine but when rotating the rotation is happening on the center of the world(0,0,0) instead of the view position.

EXAMPLE

What is happening:

enter image description here

What i need to happen:

enter image description here

CODE

This is the code i use

    Matrix Rotation = Matrix.RotationX(gameObject.transform.Rotation.X) *
                          Matrix.RotationY(gameObject.transform.Rotation.Y) *
                          Matrix.RotationZ(gameObject.transform.Rotation.Z);

    Vector3 Target = Vector3.UnitZ;

    Target += gameObject.transform.Position;

    Matrix Translation = LookAtLH(gameObject.transform.Position, Target, Vector3.UnitY);

    View = Matrix.Multiply(Rotation, Translation);

i followed directxtutorial.com to create this code What should i do to rotate on the center of my view

\$\endgroup\$
  • 1
    \$\begingroup\$ You probably jusy need to swap translate and rotate. Translate * rotate != Rotate * translate \$\endgroup\$ – Andrew Wilson May 30 '18 at 20:47
  • \$\begingroup\$ @AndrewWilson I know that but if i do that the movement bugs for example if i do x++ instead of going forward where i am looking at it will go forward according to the world like it is explained in the website i liked \$\endgroup\$ – pekira May 30 '18 at 21:17
0
\$\begingroup\$

I tend to use Polar Coordinates. This means I tend to only mess with the Theta (in your case) or the Phi for any controls regarding looking (and is very intuitive for me) and means that any controls for panning just take the angles into account.

Here's part of my camera's implementation below:

 public void HandleKeyboardInput()
    {
        // TODO: Mark that the camera pos or target changed when it does

        bool w = Keyboard.GetState().IsKeyDown(Keys.W);
        bool a = Keyboard.GetState().IsKeyDown(Keys.A);
        bool s = Keyboard.GetState().IsKeyDown(Keys.S);
        bool d = Keyboard.GetState().IsKeyDown(Keys.D);

        if (!(w || a || s || d))
        {
            return;
        }

        updateCamera = true;
        bool shift = Keyboard.GetState().IsKeyDown(Keys.LeftShift);

        // camera panning
        if (!shift)
        {
            if (w)
            {
                pos.X += (float)Math.Sin(theta);
                pos.Z += (float)Math.Cos(theta);
            }
            if (s)
            {
                pos.X -= (float)Math.Sin(theta);
                pos.Z -= (float)Math.Cos(theta);
            }

            if (a)
            {
                pos.X += (float)Math.Cos(theta);
                pos.Z -= (float)Math.Sin(theta);
            }
            if (d)
            {
                pos.X -= (float)Math.Cos(theta);
                pos.Z += (float)Math.Sin(theta);
            }
        }
        // camera twisting
        else
        {
            if (w && phi < -Utilities.thirdPi)
            {
                phi += (float)Math.PI / 180;
            }
            if (s && phi > Utilities.halfPi * -0.97)
            {
                phi -= (float)Math.PI / 180;
            }

            if (a)
            {
                theta += (float)Math.PI / 180;
                if (theta > Math.PI * 2)
                {
                    theta = 0;
                }
            }
            if (d)
            {
                theta -= (float)Math.PI / 180;
                if (theta < 0)
                {
                    theta = (float)Math.PI * 2;
                }
            }
        }

        //Target is based on Position, that way they are always synchronus
        tar.X = pos.X + radius * (float)Math.Sin(theta) * (float)Math.Cos(phi);
        tar.Z = pos.Z + radius * (float)Math.Cos(theta) * (float)Math.Cos(phi);
        tar.Y = pos.Y + radius * (float)Math.Sin(phi);
    }

    const float FIELD_OF_VIEW = Utilities.halfPi;
    public Matrix BuildProjection()
    {
            return Matrix.CreatePerspectiveFieldOfView(
                               FIELD_OF_VIEW,
                               gd.DisplayMode.AspectRatio,
                               1f, 500f);
    }

    public Matrix BuildView()
    {
        return Matrix.CreateLookAt(pos, tar, new Vector3(0f, 1f, 0f));
    }

    public Matrix BuildWorld()
    {
        return Matrix.Identity;// CreateWorld(tar, Vector3.Forward, Vector3.Up);
    }

I use similar code for twisting models, pointing models at things, etc. Let me know if I can make this answer more helpful to you.

\$\endgroup\$
  • \$\begingroup\$ thank you a lot for your answer but my problem is simply that the camera is rotating on center of the world instead of its own center so your answer doesn't really help. \$\endgroup\$ – pekira May 31 '18 at 14:32
  • \$\begingroup\$ If you specifically look at how I adjust the target of the camera based on the position of the camera, I think you may change your mind. Note: If you were to apply similar tracking of Theta in your code before the line "LookAtLH(gameObject.transform.Position, Target, Vector3.UnitY);" you should have a fix. I pasted much more code so you could see the context of it, in case you had questions. \$\endgroup\$ – blurry May 31 '18 at 14:53
  • \$\begingroup\$ how do i know the value of radius? \$\endgroup\$ – pekira May 31 '18 at 15:00
  • \$\begingroup\$ Apologies that I didn't give that context. "Radius" is defined in my code as the distance between Pos and Tar; and I adjusted Radius as a way to "Zooming in/out." In your case, you don't need anything regarding Phi I don't think (until you're changing it.) NOTE: In my code my "ground" plane is the XY plane, and my "depth" is on the Z axis. \$\endgroup\$ – blurry May 31 '18 at 15:04
  • \$\begingroup\$ I tried theta but didn't work. \$\endgroup\$ – pekira May 31 '18 at 15:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.