3 #include "../model/Shape.hpp"
6 #include <glm/gtx/quaternion.hpp>
7 #include <glm/gtx/transform.hpp>
11 blank::EntityModel::Buffer model_buffer;
17 Entity::Entity() noexcept
23 , angular_velocity(0.0f)
24 , world_collision(false)
30 void Entity::Position(const Chunk::Pos &c, const glm::vec3 &pos) noexcept {
35 void Entity::Position(const glm::vec3 &pos) noexcept {
36 glm::vec3 position(pos);
37 while (position.x >= Chunk::width) {
38 position.x -= Chunk::width;
41 while (position.x < 0) {
42 position.x += Chunk::width;
45 while (position.y >= Chunk::height) {
46 position.y -= Chunk::height;
49 while (position.y < 0) {
50 position.y += Chunk::height;
53 while (position.z >= Chunk::depth) {
54 position.z -= Chunk::depth;
57 while (position.z < 0) {
58 position.z += Chunk::depth;
61 model.Position(position);
64 void Entity::Move(const glm::vec3 &delta) noexcept {
65 Position(Position() + delta);
68 void Entity::Rotate(const glm::quat &delta) noexcept {
69 Orientation(delta * Orientation());
72 glm::mat4 Entity::ChunkTransform(const Chunk::Pos &chunk_offset) const noexcept {
73 const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent());
74 return glm::translate(translation);
77 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
78 const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent()) + Position();
79 glm::mat4 transform(toMat4(Orientation()));
80 transform[3].x = translation.x;
81 transform[3].y = translation.y;
82 transform[3].z = translation.z;
86 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
87 glm::mat4 transform = Transform(chunk_offset);
88 glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
90 glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
92 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
97 glm::quat delta_rot(const glm::vec3 &av, float dt) {
98 glm::vec3 half(av * dt * 0.5f);
99 float mag = length(half);
101 float smag = std::sin(mag) / mag;
102 return glm::quat(std::cos(mag), half * smag);
104 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
110 void Entity::Update(int dt) noexcept {
111 float fdt = float(dt);
112 Move(velocity * fdt);
113 Rotate(delta_rot(angular_velocity, fdt));