]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.cpp
special treatment for players
[blank.git] / src / world / Entity.cpp
1 #include "Entity.hpp"
2
3 #include "../model/Shape.hpp"
4
5 #include <cmath>
6 #include <glm/gtx/quaternion.hpp>
7 #include <glm/gtx/transform.hpp>
8
9 namespace {
10
11 blank::EntityModel::Buffer model_buffer;
12
13 }
14
15 namespace blank {
16
17 Entity::Entity() noexcept
18 : model()
19 , name("anonymous")
20 , bounds()
21 , velocity(0, 0, 0)
22 , chunk(0, 0, 0)
23 , angular_velocity(0.0f)
24 , ref_count(0)
25 , world_collision(false)
26 , dead(false) {
27
28 }
29
30
31 void Entity::Position(const Chunk::Pos &c, const glm::vec3 &pos) noexcept {
32         chunk = c;
33         model.Position(pos);
34 }
35
36 void Entity::Position(const glm::vec3 &pos) noexcept {
37         glm::vec3 position(pos);
38         while (position.x >= Chunk::width) {
39                 position.x -= Chunk::width;
40                 ++chunk.x;
41         }
42         while (position.x < 0) {
43                 position.x += Chunk::width;
44                 --chunk.x;
45         }
46         while (position.y >= Chunk::height) {
47                 position.y -= Chunk::height;
48                 ++chunk.y;
49         }
50         while (position.y < 0) {
51                 position.y += Chunk::height;
52                 --chunk.y;
53         }
54         while (position.z >= Chunk::depth) {
55                 position.z -= Chunk::depth;
56                 ++chunk.z;
57         }
58         while (position.z < 0) {
59                 position.z += Chunk::depth;
60                 --chunk.z;
61         }
62         model.Position(position);
63 }
64
65 void Entity::Move(const glm::vec3 &delta) noexcept {
66         Position(Position() + delta);
67 }
68
69 void Entity::Rotate(const glm::quat &delta) noexcept {
70         Orientation(delta * Orientation());
71 }
72
73 glm::mat4 Entity::ChunkTransform(const Chunk::Pos &chunk_offset) const noexcept {
74         const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent());
75         return glm::translate(translation);
76 }
77
78 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
79         const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent()) + Position();
80         glm::mat4 transform(toMat4(Orientation()));
81         transform[3].x = translation.x;
82         transform[3].y = translation.y;
83         transform[3].z = translation.z;
84         return transform;
85 }
86
87 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
88         glm::mat4 transform = Transform(chunk_offset);
89         glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
90         from /= from.w;
91         glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
92         to /= to.w;
93         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
94 }
95
96 namespace {
97
98 glm::quat delta_rot(const glm::vec3 &av, float dt) {
99         glm::vec3 half(av * dt * 0.5f);
100         float mag = length(half);
101         if (mag > 0.0f) {
102                 float smag = std::sin(mag) / mag;
103                 return glm::quat(std::cos(mag), half * smag);
104         } else {
105                 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
106         }
107 }
108
109 }
110
111 void Entity::Update(int dt) noexcept {
112         float fdt = float(dt);
113         Move(velocity * fdt);
114         Rotate(delta_rot(angular_velocity, fdt));
115 }
116
117 }