-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
80 lines (65 loc) · 2.29 KB
/
Camera.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <algorithm>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtc\quaternion.hpp>
#include "Generators\PhysicsGenerator.h"
#include "Math\PhysicsOps.h"
#include "Physics.h"
#include "Camera.h"
Camera::Camera(float centerPitch, glm::vec2 yawLimits, glm::vec2 pitchLimits)
: centerPitch(centerPitch), yawLimits(yawLimits), pitchLimits(pitchLimits), currentYaw(0), currentPitch(0)
{
}
void Camera::Initialize(btRigidBody* playerObject)
{
this->playerObject = playerObject;
rotateAmounts = glm::vec3(0, 1, 0);
angle = 90;
UpdateCamera();
lastMatrices.push_back(glm::mat4_cast(GetViewOrientation()) * glm::translate(glm::mat4(), -GetViewPosition()));
}
void Camera::UpdateCamera()
{
glm::vec3 pos = PhysicsGenerator::GetBodyPosition(playerObject);
glm::quat playerOrientation = PhysicsGenerator::GetBodyRotation(playerObject);
glm::quat travelRotation = playerOrientation * glm::rotate(glm::quat(), glm::radians(-90.0f), glm::vec3(1, 0, 0));
glm::vec3 forwardsVector = PhysicsOps::ForwardsVector(travelRotation);
glm::vec3 upVector = PhysicsOps::UpVector(travelRotation);
// TODO configurable distance from the player.
cameraPos = pos - (upVector * 1.0f + forwardsVector * 3.5f);
cameraRotation = glm::rotate(glm::quat(), glm::radians(centerPitch + currentPitch), glm::vec3(1, 0, 0))
* glm::rotate(glm::quat(), glm::radians(currentYaw), glm::vec3(0, 0, 1))
* glm::conjugate(playerOrientation);
}
void Camera::Update(float elapsedTime)
{
UpdateCamera();
lastMatrices.push_back(glm::mat4_cast(GetViewOrientation()) * glm::translate(glm::mat4(), -GetViewPosition()));
if (lastMatrices.size() > 7)
{
lastMatrices.pop_front();
}
}
void Camera::Yaw(float factor)
{
currentYaw += factor;
currentYaw = std::min(currentYaw, yawLimits.y);
currentYaw = std::max(currentYaw, yawLimits.x);
}
void Camera::Pitch(float factor)
{
currentPitch += factor;
currentPitch = std::min(currentPitch, pitchLimits.y);
currentPitch = std::max(currentPitch, pitchLimits.x);
}
const glm::vec3 Camera::GetViewPosition() const
{
return cameraPos;
}
const glm::quat Camera::GetViewOrientation() const
{
return cameraRotation;
}
const glm::mat4 Camera::GetViewMatrix() const
{
return PhysicsOps::Average(lastMatrices);
}