-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarCamera.h
More file actions
61 lines (46 loc) · 1.2 KB
/
Copy pathcarCamera.h
File metadata and controls
61 lines (46 loc) · 1.2 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
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef CAR_CAMERA_H
#define CAR_CAMERA_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include "gameObject.hpp"
class CarCamera
{
public:
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;
float Zoom = 70.0f;
glm::vec3 offset;
GameObject *targetCar;
CarCamera()
{
}
void initialize(GameObject *car, glm::vec3 camOffset = glm::vec3(0.0f, 2.6f, -6.6f))
{
targetCar = car;
offset = camOffset;
WorldUp = glm::vec3(0.0f, 1.0f, 0.0f);
Position = targetCar->pos;
}
void update(float deltaTime)
{
if (!targetCar)
return;
glm::vec3 carPos = targetCar->pos;
btQuaternion btQuat = targetCar->rigidBody->getOrientation();
glm::quat rotation(btQuat.w(), btQuat.x(), btQuat.y(), btQuat.z());
glm::vec3 rotatedOffset = rotation * offset;
Position = glm::mix(Position, carPos + rotatedOffset, 6 * deltaTime);
Front = glm::normalize(carPos - Position);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
glm::mat4 GetViewMatrix()
{
return glm::lookAt(Position, targetCar->pos, Up);
}
};
#endif