Pan-Tilt-Zoom Camera Controller
Summary#
The PanTiltZoomCameraController allows you to place a camera in a specific Position and then control where it's looking at via the Pan, Tilt, and Zoom properties. This controller type is well suited for simulating a fixed "security" or "TV set" camera.
- Adjusting
Panturns the camera left or right; - Adjusting
Tilttilts the camera up or down; - Adjusting
Zoomzooms the camera in or out.
Example Usage#
// One time setup:
var controller = camera.CreateController<PanTiltZoomCameraController>(); // (1)!
controller.Position = Location.Origin; // (2)!
controller.ZeroPanTiltDirection = Direction.Forward; // (3)!
controller.UpDirection = Direction.Up; // (4)!
// Per-frame:
controller.AdjustAllViaDefaultControls(input.KeyboardAndMouse, deltaTime); // (5)!
controller.AdjustAllViaDefaultControls(input.GameControllersCombined, deltaTime); // (6)!
controller.Progress(deltaTime); // (7)!
- This creates the controller, attached to the given
camera. - This sets the camera's position in the world.
- This sets the direction the camera should point when
PanandTiltare zero (i.e. this is the default look direction of the camera). -
This sets which way is "up" for the camera. When tilting upwards, this is the direction the camera will tilt towards.
-
This manipulates the camera according to the default keyboard and mouse scheme. The pan/tilt/zoom properties will change according to any registered user inputs for this frame.
You can replace this with more specific control code if desired (see below); or remove it entirely if you do not wish to allow user keyboard/mouse input control.
-
This manipulates the camera according to the default game controller scheme for all game controllers combined. The pan/tilt/zoom properties will change according to any registered user inputs for this frame.
You can replace this with more specific control code if desired (see below); or remove it entirely if you do not wish to allow user gamepad input control.
-
Calling
Progress()once per frame is required on all camera controllers in order for them to actually alter their targetCamera's parameters.
Properties#
Per-Frame Targets#
-
Pan -
Sets the left/right turn amount of the camera.
Increasing this value turns the camera to the left; decreasing to the right.
The leftward direction is derived from the configured
ZeroPanTiltDirectionandUpDirectionproperties.Defaults to
0°. -
Tilt -
Sets the up/down turn amount of the camera.
Increasing this value tilts the camera upward; decreasing downward.
The upward direction is set via the
UpDirectionproperty.Defaults to
0°. -
Zoom -
A normalized value between
0fand1f(representing 0% to 100% zoom). Sets how zoomed-in the camera is.A value of
0fis 0% zoomed in (e.g. fully zoomed out); a value of1fis 100% zoomed in.Zoom is simulated by altering the camera's field of view; see
MaxZoomInFov/MaxZoomOutFovbelow.Defaults to
0.5f(i.e. 50%).
Configuration#
-
Position -
The location of the camera in the world.
Defaults to
Location.Origin. -
UpDirection -
The local "up" direction of this camera. This is the direction the camera will tilt towards when increasing
Tilt.Defaults to
Direction.Up. -
ZeroPanTiltDirection -
This is the direction the camera should point when
PanandTiltare at zero.Defaults to
Direction.Forward.
-
PanRange -
Can be set to any value between
0°and360°to set the maximum amount the camera is permitted to pan left/right from itsZeroPanTiltDirection.This value is applied 50% in either direction equally (i.e. a value of
160°results in the camera being able to pan 80° left and 80° right).Can be set to
nullto remove any limit.Defaults to
160°. -
MaxTiltUp -
Can be set to any value between
0°and180°to set the maximum amount the camera is permitted to tilt upward from itsZeroPanTiltDirection.Defaults to
35°. -
MaxTiltDown -
Can be set to any value between
0°and180°to set the maximum amount the camera is permitted to tilt upward from itsZeroPanTiltDirection.Defaults to
55°. -
MaxZoomInFov -
Determines the vertical camera FOV at max zoom in. This should be smaller than or equal to
MaxZoomOutFov.Defaults to
15°. -
MaxZoomOutFov -
Determines the vertical camera FOV at max zoom in. This should be smaller than or equal to
MaxZoomOutFov.Defaults to
90°.
Reacting to Input#
As camera controllers are often meant to be affected by user input, there are some convenience methods supplied for controlling the primary per-frame target properties:
Adjusting Pan#
Keyboard / Mouse#
-
AdjustPanViaMouseCursor(...) -
Adjusts
Panaccording to the captured mouse cursor movement for this frame.The
axissets which cursor movement direction will be used (defaults toX, e.g. left/right).The
adjustmentPerPixelvalue is the angle to add toPanfor each pixel moved according to the givenaxis. If null,DefaultPanSensitivityMouseCursorwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustPanViaMouseWheel(...) -
Adjusts
Panaccording to the captured mouse wheel movement for this frame.The
adjustmentPerWheelIncrementvalue is the angle to add toPanfor each scroll increment on the mouse wheel. If null,DefaultPanSensitivityMouseWheelwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustPanViaKeyPress(...) -
Adjusts
Panaccording to whether a certain key is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
keyToTestForis the key that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two keys in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toPanfor each second this key is depressed. If null,DefaultPanSensitivityKeyOrButtonPresswill be used.
Gamepad#
-
AdjustPanViaControllerStick(...) -
Adjusts
Panaccording to the captured controller stick position for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
axissets which stick movement direction will be used (defaults toX, e.g. left/right).The
maxAdjustmentPerSecvalue is the angle to add toPanwhen the stick is fully displaced along the givenaxis. If null,DefaultPanSensitivityControllerStickwill be used.If
useLeftStickis true, the left controller stick will be measured; otherwise the right stick will be measured. Defaults tofalse.If
invertStickControlistrue, the calculated adjustment will be reversed. -
AdjustPanViaControllerTriggers(...) -
Adjusts
Panaccording to the captured controller trigger positions for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
maxAdjustmentPerSecvalue is the angle to add toPanwhen the trigger is fully displaced. If null,DefaultPanSensitivityControllerTriggerwill be used.If
leftTriggerPansAnticlockwiseis true, the left trigger will pan left and the right trigger pan right; otherwise these directions will be reversed. Defaults totrue. -
AdjustPanViaButtonPress(...) -
Adjusts
Panaccording to whether a certain button is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
buttonToTestForis the button that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two buttons in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toPanfor each second this button is depressed. If null,DefaultPanSensitivityKeyOrButtonPresswill be used.
Other#
-
AdjustPan(...) -
Adjusts
Panaccording to the given turn rate (adjustmentPerSec) and time step (deltaTime).This method does not inspect any user input data but is provided as a convenience for building custom per-frame control code.
Adjusting Tilt#
Keyboard / Mouse#
-
AdjustTiltViaMouseCursor(...) -
Adjusts
Tiltaccording to the captured mouse cursor movement for this frame.The
axissets which cursor movement direction will be used (defaults toY, e.g. up/down).The
adjustmentPerPixelvalue is the angle to add toTiltfor each pixel moved according to the givenaxis. If null,DefaultTiltSensitivityMouseCursorwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustTiltViaMouseWheel(...) -
Adjusts
Tiltaccording to the captured mouse wheel movement for this frame.The
adjustmentPerWheelIncrementvalue is the angle to add toTiltfor each scroll increment on the mouse wheel. If null,DefaultTiltSensitivityMouseWheelwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustTiltViaKeyPress(...) -
Adjusts
Tiltaccording to whether a certain key is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
keyToTestForis the key that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two keys in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toTiltfor each second this key is depressed. If null,DefaultTiltSensitivityKeyOrButtonPresswill be used.
Gamepad#
-
AdjustTiltViaControllerStick(...) -
Adjusts
Tiltaccording to the captured controller stick position for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
axissets which stick movement direction will be used (defaults toY, e.g. up/down).The
maxAdjustmentPerSecvalue is the angle to add toTiltwhen the stick is fully displaced along the givenaxis. If null,DefaultTiltSensitivityControllerStickwill be used.If
useLeftStickis true, the left controller stick will be measured; otherwise the right stick will be measured. Defaults tofalse.If
invertStickControlistrue, the calculated adjustment will be reversed. -
AdjustTiltViaControllerTriggers(...) -
Adjusts
Tiltaccording to the captured controller trigger positions for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
maxAdjustmentPerSecvalue is the angle to add toTiltwhen the trigger is fully displaced. If null,DefaultTiltSensitivityControllerTriggerwill be used.If
leftTriggerTiltsUpwardis true, the left trigger will tilt up and the right trigger tilt down; otherwise these directions will be reversed. Defaults totrue. -
AdjustTiltViaButtonPress(...) -
Adjusts
Tiltaccording to whether a certain button is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
buttonToTestForis the button that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two buttons in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toTiltfor each second this button is depressed. If null,DefaultTiltSensitivityKeyOrButtonPresswill be used.
Other#
-
AdjustTilt(...) -
Adjusts
Tiltaccording to the given turn rate (adjustmentPerSec) and time step (deltaTime).This method does not inspect any user input data but is provided as a convenience for building custom per-frame control code.
Adjusting Zoom#
Note that Zoom is a normalized 0f–1f value, so all of the sensitivity parameters below are float? rather than Angle?.
Keyboard / Mouse#
-
AdjustZoomViaMouseCursor(...) -
Adjusts
Zoomaccording to the captured mouse cursor movement for this frame.The
axissets which cursor movement direction will be used (defaults toY, e.g. up/down).The
adjustmentPerPixelvalue is the amount to add toZoomfor each pixel moved according to the givenaxis. If null,DefaultZoomSensitivityMouseCursorwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustZoomViaMouseWheel(...) -
Adjusts
Zoomaccording to the captured mouse wheel movement for this frame.The
adjustmentPerWheelIncrementvalue is the amount to add toZoomfor each scroll increment on the mouse wheel. If null,DefaultZoomSensitivityMouseWheelwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustZoomViaKeyPress(...) -
Adjusts
Zoomaccording to whether a certain key is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
keyToTestForis the key that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two keys in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the amount to add toZoomfor each second this key is depressed. If null,DefaultZoomSensitivityKeyOrButtonPresswill be used.
Gamepad#
-
AdjustZoomViaControllerStick(...) -
Adjusts
Zoomaccording to the captured controller stick position for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
axissets which stick movement direction will be used (defaults toY, e.g. up/down).The
maxAdjustmentPerSecvalue is the amount to add toZoomwhen the stick is fully displaced along the givenaxis. If null,DefaultZoomSensitivityControllerStickwill be used.If
useLeftStickis true, the left controller stick will be measured; otherwise the right stick will be measured. Defaults tofalse.If
invertStickControlistrue, the calculated adjustment will be reversed. -
AdjustZoomViaControllerTriggers(...) -
Adjusts
Zoomaccording to the captured controller trigger positions for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
maxAdjustmentPerSecvalue is the amount to add toZoomwhen the trigger is fully displaced. If null,DefaultZoomSensitivityControllerTriggerwill be used.If
rightTriggerZoomsInis true, the right trigger will zoom in and the left trigger zoom out; otherwise these directions will be reversed. Defaults totrue. -
AdjustZoomViaButtonPress(...) -
Adjusts
Zoomaccording to whether a certain button is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
buttonToTestForis the button that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two buttons in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the amount to add toZoomfor each second this button is depressed. If null,DefaultZoomSensitivityKeyOrButtonPresswill be used.
Other#
-
AdjustZoom(...) -
Adjusts
Zoomaccording to the given rate (adjustmentPerSec) and time step (deltaTime).This method does not inspect any user input data but is provided as a convenience for building custom per-frame control code.
Default Controls#
The following snippets show the implementation of AdjustAllViaDefaultControls(...) for keyboard/mouse and gamepad respectively:
// AdjustAllViaDefaultControls(input.KeyboardAndMouse, deltaTime):
AdjustPanViaMouseCursor(input, panAdjustmentPerPixel, invertMouseControl: invertPanControl);
AdjustTiltViaMouseCursor(input, tiltAdjustmentPerPixel, invertMouseControl: invertTiltControl);
AdjustZoomViaMouseWheel(input, zoomAdjustmentPerWheelIncrement, invertMouseControl: invertZoomControl);
// AdjustAllViaDefaultControls(input.GameControllersCombined, deltaTime):
AdjustPanViaControllerStick(input, deltaTime, maxPanAdjustmentPerSec, invertStickControl: invertPanControl);
AdjustTiltViaControllerStick(input, deltaTime, maxTiltAdjustmentPerSec, invertStickControl: invertTiltControl);
AdjustZoomViaControllerTriggers(input, deltaTime, maxZoomAdjustmentPerSec, rightTriggerZoomsIn: !invertZoomControl);
Smoothing#
Smoothing changes how quickly the controller adjusts the camera to match the current target properties.
The Pan, Tilt, and Zoom target properties can have smoothing applied.
// Set properties' smoothing individually:
controller.PanSmoothingStrength = Strength.VeryMild;
controller.TiltSmoothingStrength = Strength.VeryMild;
controller.ZoomSmoothingStrength = Strength.VeryMild;
// Set all properties' smoothing simutaneously:
controller.SetGlobalSmoothing(Strength.VeryMild);
The default smoothing for all properties is VeryMild. You can choose from VeryMild, Mild, Moderate, Strong, VeryStrong, or None.
- Smoothing makes the camera feel more 'real' or physical.
- Higher strengths increase this feeling but also increase the latency between setting a target value and the camera actually meeting that target.
- Setting the smoothing to
Nonedisables smoothing entirely. This means the camera will always be updated to meet exactly the target value of each property on each frame; reducing latency to 0 but making the camera feel less physical.
Custom Smoothing Values
If the enum-based approach is not specific enough for your needs, every property can instead have a custom smoothing strength applied via a method named like "SetCustom[...]SmoothingStrength".
This method takes a single float parameter that indicates the half-life of decay between the current value of a property and its target value.
For example, if the current value of X is 50 and the target value of X is 100, a half-life of 1f would move X to 75 after one second, and then 87.5 after the next second, and so-on.
Advanced: Smoothing is implemented via critically-damped spring. The smoothingHalfLife parameter is translated to become the Ω of the spring equation via the formula Ω = 1.6783469f / smoothingHalfLife.