aboutsummaryrefslogtreecommitdiff
path: root/src/ThirdPersonCamera.cpp
blob: 2404dafa36069ad4e897726913c96bb08fb8a295 (plain)
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
#include "../include/ThirdPersonCamera.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <stdio.h>

#define HALF_PI 1.5707963267948966f
#define PI 3.141592653589793f
#define TAU 6.283185307179586f

namespace amalgine {
    ThirdPersonCamera::ThirdPersonCamera() : target_pos(glm::vec3()), camera_distance(3.0f), angle_horizontal(0.0f), angle_vertical(0.0f) {

    }

    void ThirdPersonCamera::zoom(float distance) {
        camera_distance += distance;
        if(camera_distance < 0.1f)
            camera_distance = 0.1f;
    }

    void ThirdPersonCamera::rotate_horizontal(float radians) {
        angle_horizontal = std::fmod(angle_horizontal + radians, TAU);
    }

    void ThirdPersonCamera::rotate_vertical(float radians) {
        const float max_angle = 1.56079632679f;

        angle_vertical = std::fmod(angle_vertical + radians, TAU);
        if(angle_vertical > max_angle)
            angle_vertical = max_angle;
        else if(angle_vertical < -max_angle)
            angle_vertical = -max_angle;
    }

    void ThirdPersonCamera::set_target_position(const glm::vec3 &target_pos) {
        this->target_pos = target_pos;
    }

    glm::mat4 ThirdPersonCamera::get_matrix() const {
        glm::vec3 eye = target_pos - get_forward_vector() * camera_distance;
        return glm::lookAt(eye, target_pos, glm::vec3(0.0f, 0.0f, 1.0f));
    }

    glm::vec3 ThirdPersonCamera::get_forward_vector() const {
        glm::vec3 vec;
        vec.x = std::sin(angle_horizontal) * std::cos(angle_vertical);
        vec.y = std::cos(angle_horizontal) * std::cos(angle_vertical);
        vec.z = std::sin(angle_vertical);
        return vec;
    }

    glm::vec3 ThirdPersonCamera::get_right_vector() const {
        glm::vec3 vec = get_forward_vector();
        float temp = vec.x;
        vec.x = vec.y;
        vec.y = -temp;
        return vec;
    }
}