# Orbit camera around a model • ### Question

• Hi,

I'm totally new to DirectX, and am sorry if the problem appears simple. I have to orbit the camera around a 3D volume. But it has to be done about both the X-axis as well as the Y-axis. The user can alternate between the two or stick to any one (there are two buttons, OrbitX and OrbitY).

How do I go about doing this?!? The camera eye has to be positioned at it's new place on the imaginary sphere surrounding the model. Also it's Up vector has to be calculated, whereas the LookAt point remains the same. I heard that quaternions help in achieving smooth rotations, but how should it be used?

Any help on this will really be appreciated! Thanks.

Tuesday, March 27, 2007 11:52 AM

• I feel there might be a simpler way to do this; take the angles that represent the rotation around the X-axis (pitch), followed by the angles that represent rotation around the Y-axis (yaw).

Take a look at my example; it uses the two angles (cam_pitch and cam_yaw) to show orientation around the model (Vector3 model_coords), and the matrix view for the view matrix.

Code Snippet

Vector3 v = Vector3.Backward;

v = Vector3.Transform(v, pitch_rotation);

v = Vector3.Transform(v, yaw_rotation);

view = Matrix.CreateLookAt(model_coords + v, model_coords, Vector3.Up);

Obviously the camera angles are expressed in degrees, and as for the perpendicular Up Vector for the camera, that's really not necessary unless you don't want it upside-down. If you do, just repeat the 3rd, 4th, and 5th lines, replacing v with up and Vector3.Backward to Vector3.Up.

Basically what happens is we express our location as 2 angles, and we take those angles and turn them into rotation matrices, which are used to transform an initial vector. If both angles are 0, then the camera will be in front of the model, looking behind at the model. The transformed vector is then translated by the model's coordinates so the camera will orbit around the model, not the origin. Finally, we create our lookat matrix using the new camera point and the model's coordinates. This is the same technique that games use for lock-on features, as well as for looking around in the game.

Wednesday, April 18, 2007 2:11 AM

### All replies

• well, when i had to orbit my camera around a stationary object i envisioned a metal ring that the camera just slid around.

i knew how to create a simple 2d circle by calculating points around a center with a given radius....using degrees you can essentially get a point-position value for 0-359 degrees

then you just updated the camera's position X and Z for orbit around the Y axis and
update camera's position Y and Z for X axis orbit
Wednesday, March 28, 2007 3:12 PM
• Hi DD,

I can easily calculate the new coordinates for the camera eye, the look at point being the same.

Let's say, that I have computed the new position on the imaginary sphere (by jumping one longitude and latitude unit each). I have only one vector (the line of sight between the camera eye and look at point).

How will I get the look up vector which I want to be perpendicular to the line of sight at all times?

Monday, April 2, 2007 10:39 AM
• I feel there might be a simpler way to do this; take the angles that represent the rotation around the X-axis (pitch), followed by the angles that represent rotation around the Y-axis (yaw).

Take a look at my example; it uses the two angles (cam_pitch and cam_yaw) to show orientation around the model (Vector3 model_coords), and the matrix view for the view matrix.

Code Snippet

Vector3 v = Vector3.Backward;

v = Vector3.Transform(v, pitch_rotation);

v = Vector3.Transform(v, yaw_rotation);

view = Matrix.CreateLookAt(model_coords + v, model_coords, Vector3.Up);

Obviously the camera angles are expressed in degrees, and as for the perpendicular Up Vector for the camera, that's really not necessary unless you don't want it upside-down. If you do, just repeat the 3rd, 4th, and 5th lines, replacing v with up and Vector3.Backward to Vector3.Up.

Basically what happens is we express our location as 2 angles, and we take those angles and turn them into rotation matrices, which are used to transform an initial vector. If both angles are 0, then the camera will be in front of the model, looking behind at the model. The transformed vector is then translated by the model's coordinates so the camera will orbit around the model, not the origin. Finally, we create our lookat matrix using the new camera point and the model's coordinates. This is the same technique that games use for lock-on features, as well as for looking around in the game.

Wednesday, April 18, 2007 2:11 AM
• Sir,

Thank You for the suggestion.

It is working fine while using it with geometry structures.

But when volume texture is being used and the camera is rotated,

the rotation of camera is correct .

The effect which is acheived is correct for the angle in range of -90 to +90.

For the other angles it appears as though the volume rotates in the opposite direction .

Any idea why is it behaving like this.

Thursday, May 3, 2007 4:34 AM