-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathOrbitCamera.cpp
More file actions
49 lines (43 loc) · 1.48 KB
/
OrbitCamera.cpp
File metadata and controls
49 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "OrbitCamera.h"
#include "Actor.h"
OrbitCamera::OrbitCamera(Actor* owner)
:CameraComponent(owner)
,mOffset(-400.0f, 0.0f, 0.0f)
,mUp(Vector3::UnitZ)
,mPitchSpeed(0.0f)
,mYawSpeed(0.0f)
{
}
void OrbitCamera::Update(float deltaTime)
{
CameraComponent::Update(deltaTime);
// Create a quaternion for yaw about world up
Quaternion yaw(Vector3::UnitZ, mYawSpeed * deltaTime);
// Transform offset and up by yaw
mOffset = Vector3::Transform(mOffset, yaw);
mUp = Vector3::Transform(mUp, yaw);
// Compute camera forward/right from these vectors
// Forward owner.position - (owner.position + offset)
// = -offset
Vector3 forward = -1.0f * mOffset;
forward.Normalize();
Vector3 right = Vector3::Cross(mUp, forward);
right.Normalize();
// Create quaternion for pitch about camera right
Quaternion pitch(right, mPitchSpeed * deltaTime);
// Transform camera offset and up by pitch
mOffset = Vector3::Transform(mOffset, pitch);
mUp = Vector3::Transform(mUp, pitch);
// Compute transform matrix
Vector3 target = mOwner->GetPosition();
Vector3 cameraPos = target + mOffset;
Matrix4 view = Matrix4::CreateLookAt(cameraPos, target, mUp);
SetViewMatrix(view);
}