2
\$\begingroup\$

I'm developing a third person 3D platformer in XNA. My problem is when I try to rotate the camera around the player.
I would like to rotate (and animate) the camera 90 degrees around the player. So the camera should rotate until it has reached 90 degrees from the starting position.
I cannot figure out how to keep track of the rotation, and when the rotation has made the full 90 degrees.

Currently my cameras update:

public void Update(Vector3 playerPosition) {
    if (rotateCamera) {
        position = Vector3.Transform(position - playerPosition, Matrix.CreateRotationY(0.1f)) + playerPosition;
    }
    this.viewMatrix = Matrix.CreateLookAt(position, playerPosition, Vector3.Up);
}

The initial position of the camera is set in the constructor.
The "rotateCamera" bool is set on keypress.

Thanks for the help in advance.

Cheers.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You can have a variable in your class that store the rotation angle that your camera have already done. In your code it seems you rotate around Y by 0.1 each frame. So you set the initial value of the variable to 0 and increment it by 0.1 each time you rotate. You check the value before rotating, if it's under 90 you continue, else you stop to rotate. The value 0.1 is in radians, wether you convert it back in degrees and compared with 90, or you can keep it in radians and compared with rad(90).

\$\endgroup\$
6
  • \$\begingroup\$ Completely agree with you, but I remember implementing this and I had to add or substract the value of the cos/sin of the angle depending on the current orientation and the future orientation. It's not the same a turn from Z- to X+ that a turn from X+ to Z- and so on. \$\endgroup\$
    – mikidelux
    Nov 25, 2011 at 10:08
  • \$\begingroup\$ You totally just made a lightbulb go off over my head. thank yo uso much for the answer. Cheers. o7 \$\endgroup\$
    – Nicholas
    Nov 25, 2011 at 10:16
  • \$\begingroup\$ One more small issue. The check is if it's ">= 90 degrees" then stop rotating. How do I figure out where to snap it to after? I.E. if the rotate overshoots the 90 degrees, how can I set it to the position it would've been if it had stopped at exactly 90 degrees? \$\endgroup\$
    – Nicholas
    Nov 25, 2011 at 10:20
  • 1
    \$\begingroup\$ Check for it before modyfing the position, if the angle is bigger than 90, reset it to 90 and do the calculations. \$\endgroup\$
    – mikidelux
    Nov 25, 2011 at 10:22
  • 1
    \$\begingroup\$ Nevemind, I got it. Instead of creating a matrix to store the position, I just created a vector3 and set it to the transform using a 90 degrees around the Y. Thanks for all the help guys. Cheers. \$\endgroup\$
    – Nicholas
    Nov 25, 2011 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .