Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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).

share|improve this answer
    
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. –  mikidelux Nov 25 '11 at 10:08
    
You totally just made a lightbulb go off over my head. thank yo uso much for the answer. Cheers. o7 –  Nicholas Nov 25 '11 at 10:16
    
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? –  Nicholas Nov 25 '11 at 10:20
1  
Check for it before modyfing the position, if the angle is bigger than 90, reset it to 90 and do the calculations. –  mikidelux Nov 25 '11 at 10:22
1  
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. –  Nicholas Nov 25 '11 at 10:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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