Game Development Reference
In-Depth Information
Vector3
target
=
eye
+
targetOffset
// For y-up world
Vector3
up
=
Vector3
(0, 1, 0)
cameraMatrix
= CreateLookAt(
eye
,
target
,
up
)
end
// Updates based on the incremental yaw/pitch
angles for this frame
function
Update(
float
yaw
,
float
pitch
)
totalYaw
+=
yaw
totalPitch
+=
pitch
// Clamp the total pitch based on restric-
tions
// In this case, 45 degrees (approx 0.78 rad)
totalPitch
= Clamp(
totalPitch
, -0.78, 0.78)
// The target offset is before any rotations,
// actual is after rotations.
Vector3
actualOffset
=
targetOffset
// Rotate actual offset about y-up for yaw
Quaternion
quatYaw
= CreateFromAxisAngle(
Vector3
(0,1,0),
totalYaw
)
actualOffset
= Transform(
actualOffset
,
quatYaw
)
// Calculate left for pitch
// Forward after yaw just the actualOffset
(normalized)
Vector3
forward
=
actualOffset
forward
.Normalize()
Vector3
left
= CrossProduct(
Vector3
(0, 1, 0),
forward
)
left
.Normalize()
// Rotate actual offset about left for pitch