Thursday, October 8, 2015

xna - Orbiting a specific point - Orbiting Camera


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?



Answer



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;
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...