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.

How would I create a camera that with the mouse, it allows you to orbit around a specifc position, with the camera always facing that point? I would also like it to be able to always be at a constant distance from the point, and be able to orbit around it in every direction.

Any ideas?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

What you're looking for is an ArcBall Camera. I've got a full snippet over here http://roy-t.nl/index.php/2010/02/21/xna-simple-arcballcamera/ but just to explain the general idea:

You set a look-at point in space which you want to orbit. Then you create a vector from that point by rotate around it using the Pitch Yaw and Roll. You then lengthen the vector to your desired distance from the object.

Anyway the most interesting stuff happens in this method:

private void ReCreateViewMatrix()
        {
            //Calculate the relative position of the camera
            position = Vector3.Transform(Vector3.Backward, Matrix.CreateFromYawPitchRoll(yaw, pitch, 0));
            //Convert the relative position to the absolute position
            position *= zoom;
            position += lookAt;

            //Calculate a new viewmatrix
            viewMatrix = Matrix.CreateLookAt(position, lookAt, Vector3.Up);
            viewMatrixDirty = false;
        }
share|improve this answer
    
Cool thanks, could you please provide a working example of this? –  Darestium Mar 16 '12 at 7:13
    
The snippet I linked is fully functional, you just need to plug it into any working XNA app and use its view and projection Matrix. –  Roy T. Mar 16 '12 at 8:18
    
So, when you change the (yaw) rotation value you (the camera) are rotating around the object, rather then you rotating around it (the objects)? And if so, do you have any idea how I could control it with the mouse? And nice snippet! Thanks for the help –  Darestium Mar 31 '12 at 1:44
    
Yes the camera orbits the point in space. You could easily wire the yaw and pitch variables to the X and Y changes of the mouse since last frame. Just do something like MousePDelta = prevMouseP - MouseP; arcballCamera.Yaw += mousePDelta.X; arcBallCamera.Pitch += mousePDelta.Y; prevMouseP = MouseP; –  Roy T. Mar 31 '12 at 6:28

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.