Color legend plain property method
A controller drives a camera: once per frame, from inside the camera’s Update, it sets where the camera is
and which way it faces. The library has two, a free camera for flying under the player’s input and an
animated camera that plays a path, and you can write your own. Names are colored by what
they are: a property or a method; values and fields
keep the plain color.
Attaching a controller
Assign the controller to the camera’s Controller, or call the controller’s ApplyTo: the two do the
same.
camera.Controller = free; // fly
camera.Controller = tour; // play a path, from where the free camera left the camera
tour.ApplyTo(camera); // the same as the line above
camera.Controller = null; // drive the camera yourself
A camera has one controller at most, and a controller drives one camera at a time. When you attach one:
- The camera’s previous controller is detached. It keeps its own settings and state, ready to be attached again.
- A controller attached to another camera leaves it. That camera is left with no controller.
- The new controller takes over the camera’s state, once. A free camera adopts the camera’s orientation, roll included; an animated camera finds the point of its path nearest the camera and eases onto it.
So switching between controllers never makes the camera jump: each one starts from where the other left it.
Assigning the controller that is already attached does nothing; to make it take over the camera’s state again, set
Controller to null and back.
The camera calls its controller’s Update at the start of its own Update, with the same seconds. Don’t call
it yourself as well, or the controller moves twice in a frame.
Who owns the pose
While a controller is attached, it sets the camera’s pose in every update, and what you set by hand between two updates may not last:
| Controller | Position | Orientation |
|---|---|---|
| None | Yours. | Yours. |
FreeCameraController |
Moves on from the camera’s position, so a position you set is kept: teleporting works. | Its own, written to the camera in every update: one you set, or a LookAt, is replaced. Attach the controller again to make it adopt yours. |
AnimatedCameraController |
The path’s, written in every update once the path has a waypoint. | The path’s, in the same way. |
To turn a free camera toward something without detaching it, use SnapTo, see
snapping to an axis. For an instant turn, look first and attach again:
camera.Controller = null;
camera.LookAt(target, Vector3.Up);
camera.Controller = free; // adopts the new orientation
Camera controller property
| Property | Type | Description |
|---|---|---|
Controller |
BaseCameraController? |
The controller driving the camera, or null. Assigning one detaches the previous controller, takes the new one from any camera it drives, and lets it adopt this camera’s state once. |
The free camera
FreeCameraController flies with six degrees of freedom: it moves along and turns about the camera’s own
axes. It holds its orientation as a quaternion, so yaw always turns about the camera’s own up, pitch about its own
right and roll about its own forward. The controls match what is on screen however the camera is banked, and there is
no pitch limit: look up past the vertical and the world turns upside down, as in a space game or an editor’s fly mode.
A camera that should keep the horizon level is a controller of your own.
Three methods take the input. Calls within a frame add up, and the next update uses them and starts again from zero, so call them every frame the input is held:
Lookturns by mouse-style deltas: positive yaw looks right and positive pitch looks down, the directions in which the mouse’s x and y grow. Each unit turns byLookSensitivityradians: at the default 0.0025, a mouse moved 100 pixels turns the view by 14°. A delta is already a distance, so it isn’t scaled by the time.Movemoves in the camera’s own frame: x to the right, y up and −z forward, soVector3.Forwardmoves ahead andVector3.Uprises along the camera’s up. The sum of a frame is capped at unit length and moves atMoveSpeedUnitSec: a stick pushed halfway moves at half speed, and W and D held together move diagonally at full speed, not faster.Rollrolls clockwise for a positive value and counterclockwise for a negative one, atRollSpeedRadSecfor 1: hold a key and pass 1 every frame.
Move and roll are rates, scaled by the seconds of the update. Look isn’t: for a gamepad stick, scale the deflection by the time yourself.
var pad = GamePad.GetState(PlayerIndex.One);
var seconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
// left stick to move: MonoGame's stick y is up, the camera's forward is -z
free.Move(new Vector3(pad.ThumbSticks.Left.X, 0, -pad.ThumbSticks.Left.Y));
// triggers to rise and sink, in the camera's frame
free.Move(new Vector3(0, pad.Triggers.Right - pad.Triggers.Left, 0));
// right stick to look, 2 radians a second at full deflection
var look = 2.0f * seconds / free.LookSensitivity;
free.Look(pad.ThumbSticks.Right.X * look, -pad.ThumbSticks.Right.Y * look);
// shoulders to roll
if (pad.IsButtonDown(Buttons.RightShoulder)) free.Roll(1);
if (pad.IsButtonDown(Buttons.LeftShoulder)) free.Roll(-1);
The settings can change at any time, for example a sprint key:
free.MoveSpeedUnitSec = keyboard.IsKeyDown(Keys.LeftShift) ? 80 : 20;
For mouse look without holding a button, hide the cursor, read how far it moved from the center of the window, and put it back:
IsMouseVisible = false;
var center = new Point(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
var mouse = Mouse.GetState();
free.Look(mouse.X - center.X, mouse.Y - center.Y);
Mouse.SetPosition(center.X, center.Y);
FreeCameraController properties
| Property | Type | Description |
|---|---|---|
MoveSpeedUnitSec |
float |
The speed of a full Move, in units per second. 20 by default. |
RollSpeedRadSec |
float |
The speed of a full Roll, in radians per second. 2.5 by default. |
LookSensitivity |
float |
The turn of one unit of Look, in radians. 0.0025 by default. |
AxisSnapConvergenceSec |
float |
The seconds a SnapTo takes to cover 95 % of its turn. 0.18 by default. |
FreeCameraController methods
| Method | Description |
|---|---|
FreeCameraController() |
A free camera with the default speeds and sensitivity. |
Look(float deltaYaw, float deltaPitch) |
Turns by mouse-style deltas times LookSensitivity: positive yaw looks right, positive pitch looks down. Cancels a snap. |
Move(Vector3 delta) |
Moves in the camera’s own frame, x right, y up, −z forward, at up to MoveSpeedUnitSec: the frame’s sum is capped at unit length. |
Roll(float direction) |
Rolls clockwise for a positive value, counterclockwise for a negative one, at RollSpeedRadSec per unit. Cancels a snap. |
SnapTo(Vector3 forward) |
Turns smoothly to face the direction, leveled against world up. |
Update(float elapsedSeconds) |
Applies the frame’s input to the camera. The camera calls it. |
Snapping to an axis
SnapTo turns the free camera smoothly to face a direction, with its roll leveled against world +Y. It is made for
an orientation widget such as BorrowedTorch.SceneGizmo: pass the
opposite of the axis the player clicked, so that the axis points at the viewer.
free.SnapTo(-Vector3.UnitX); // look along -X, level
free.SnapTo(Vector3.Down); // a top view, the top of the screen toward -Z
The turn slows down as it closes in: it covers 95 % of the way in AxisSnapConvergenceSec, whatever the frame
rate, and settles on the exact orientation once it is within 0.0015 radians of it. A value of 0 or less snaps almost
at once. Any Look or Roll input cancels the snap and takes over; Move doesn’t, so the player can
keep flying while the view turns. A direction of length zero is ignored.
Writing a controller
Derive from BaseCameraController and override two methods:
Updatemoves the camera.Camerais the camera being driven, ornullwhile the controller is detached, so check it first.SyncFrom, optional, runs once when the controller is attached, withCameraalready set, to take over the camera’s state.
An orbit camera, for an editor or a third-person view: it circles a target, keeps the horizon level and never goes over the top.
using BorrowedTorch.Cameras.Controllers.Base;
using Microsoft.Xna.Framework;
public class OrbitCameraController : BaseCameraController
{
private float _yaw; // radians about +Y; 0 is on the +Z side of the target
private float _pitch; // radians; positive is above the target, looking down
public Vector3 Target { get; set; }
public float Distance { get; set; } = 10;
// positive yaw circles to the camera's right, positive pitch rises
public void Orbit(float deltaYaw, float deltaPitch)
{
_yaw += deltaYaw;
_pitch = MathHelper.Clamp(_pitch + deltaPitch, -1.5f, 1.5f); // short of straight up and down
}
// on attachment: the orbit that puts the camera where it already is
protected override void SyncFrom()
{
if (Camera == null) return;
var toCamera = Camera.Position - Target;
Distance = MathF.Max(toCamera.Length(), 0.01f);
_yaw = MathF.Atan2(toCamera.X, toCamera.Z);
_pitch = MathF.Asin(MathHelper.Clamp(toCamera.Y / Distance, -1, 1));
}
public override void Update(float elapsedSeconds)
{
if (Camera == null) return;
// no roll, so the horizon stays level
var rotation = Quaternion.CreateFromYawPitchRoll(_yaw, -_pitch, 0);
Camera.Orientation = rotation;
Camera.Position = Target + Vector3.Transform(Vector3.Backward, rotation) * Distance;
}
}
The camera ends up Distance behind its own forward from the target, so it always looks at the target. Attached in
place of a free camera, it picks up the orbit the camera is already on and turns it to face the target. The
protected constant ConvergenceAmount, 0.95, is the share of the way that the free camera’s snap covers in its
convergence time; with MathUtils from BorrowedTorch.Math, it gives a controller of your own the same frame-rate
independent easing.
BaseCameraController members
| Member | Description |
|---|---|
Camera |
Protected, read-only. The camera this controller drives, or null while it is detached. |
ConvergenceAmount |
Protected constant. 0.95, the share of the way an easing covers in its convergence time. |
ApplyTo(Camera camera) |
Attaches the controller to the camera, the same as assigning camera.Controller. Throws ArgumentNullException for null. |
SyncFrom() |
Protected, virtual. Called once on attachment, with Camera set, to take over the camera’s state. Does nothing by default. |
Update(float elapsedSeconds) |
Abstract. Moves the camera; the camera calls it at the start of its own update. |