i want an orbit camera in jiglibx, how to do this?

I may not be the best person to answer this question. I wrote my camera module ages ago and I've never tidied it. Let's give it a shot anyway.

```
Vector3 CameraTarget, CameraPosition;
float Distance;
Vector2 Rotation;
```

Because normally your camera doesn't need to rotate around its Z axis (or roll), we just use a Vector2. The axis in particular we're interested in are the Y rotation (the angle around your object) and the X rotation (the angle from the horizon). These represent the direction from the CameraTarget. Also worth noting, we're going to measure rotation in Radians. Keep in mind that the X component represents the camera's vertical movement.

When you move your camera, you'll be updating CameraTarget (the lookat position), Rotation and Distance. You'll need to write functions to increment those with your controls.

In your update loop you can do all your new camera transformations. Firstly, you'll want to clamp your camera movement so it can't flip upside down:

```
if (Rotation.X > 1.5f & !Virtual) Rotation.X = 1.5f;
if (Rotation.X < -1.5f & !Virtual) Rotation.X = -1.5f;
```

Smarter kids than me would have used Math.PI/2 rather than 1.5, but either is fine. You can change this to limit the camera from looking up at the sky, or moving directly overhead.

Now you'll want to calculate your CameraPosition. This is where it gets tricky:

```
Matrix CamRot = Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationY(Rotation.Y);
Vector3 CamDist = new Vector3(0f, 0f, Distance);
Position = Vector3.Transform(CamDist, CamRot) + CameraTarget;
```

Note that the order of CreateRotationX and Y is important. If you change this around you'll get some slightly different results.

You can get your camera orientation matrix for your view and projection by doing a Matrix.CreateLookAt(). The Vector3.Up in this instance is the roll value we neglected when setting up our rotation. This keeps the camera upright.

`View = Matrix.CreateLookAt(Position, CameraTarget, Vector3.Up);`

Finally, you'll want to know how to actually transform your CameraTarget object in relation to the view:

```
public void Move(Vector3 Direction)
{
Matrix translation = Matrix.CreateTranslation(Direction) * Matrix.CreateRotationY(RotationTarget.Y);
CameraTarget += translation;
}
```

The trick to figuring out matrices in your head is to simply visualise them in order. Every matrix begins unrotated, untransformed. From there we transform the Identity matrix (which is another word for a matrix at the center of the world with no rotation or transformation), then translate that movement by the camera rotation amount. That way if you pushed the forward key, you move forward in relation to the camera.

Other functions that don't need explaining:

```
public void Orbit(float x, float y)
{
Rotation.X += x;
Rotation.Y += y;
}
public void Zoom(float z)
{
if (z < 0)
{
Distance *= 1.3f;
}
else if (z > 0)
{
Distancet *= 0.7f;
}
}
```

For that second function, just supply the scrollwheel state.

Check

MSDN Virtual: http://msdn.microsoft.com/en-us/library/9fkccyh4(VS.71).aspx

MSDN 10.5.3 Virtual Methods: http://msdn.microsoft.com/en-us/library/aa645767(VS.71).aspx

and

C# Virtual properties and methods explained in plain language: http://www.mikeduncan.com/csharp-virtual-explained/

A fast way to explain is that it checks if it executes the base function or the function that is overridden.