Saturday, October 20, 2012


I finally got the camera controls to work properly. The orbiting, panning, zooming, scrolling and keyboard-movement now work the way I planned. All the movement is done in the CameraMan-class, a member of the Camera-class.

The problem was that I couldn't get orbiting around an object to work without messing up rotations. After a lot of googling I found a useful way to accomplish this. The idea of setting the position to a transformed Vector3.Backward and scaling it by the distance of the actual position and the target works like a charm.

I begin the orbiting by determining the amount the camera should rotate. I use the relative mouse position and divide that by 500. I add those numbers to a Rotation-vector3 saved in the Camera-class. When that is done, I use Matrix.CreateFromYawPitchRoll with my Rotation-vector3 to get my Rotation-matrix, which is also saved in the Camera class.

After that I create a float called 'dist' which is the distance from the actual camera position to the target.
Then I transform the position with Vector3.Transform(Vector3.Backward, Camera.rotationMatrix);
I am using Backward to easily reposition my Camera. After the transform, I scale the position by the 'dist' and voila. I have a rotated camera. It only assumes the cameraTarget is 0,0,0 at the moment. So just add the cameraTarget-vector to the camera's position.

The rotation matrix will be updated whenever the camera is rotated, so you will always have a good rotation matrix for transforming the up-vector and the right-vector. This makes panning the camera very easy. It's nothing more than Camera.Up = Vector3.Transform( Vector3.Up, Camera.rotationMatrix );

Now that this is settled, I will work on the caching/storing of the assets built by the XNA pipeline and Picking/Selecting objects.

The answer on this question helped me to fix this situation; http://gamedev.stackexchange.com/questions/25448/orbiting-a-specific-point-orbiting-camera

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!