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)
25 , world_collision(false)
31 void Entity::Position(const Chunk::Pos &c, const glm::vec3 &pos) noexcept {
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;
42 while (position.x < 0) {
43 position.x += Chunk::width;
46 while (position.y >= Chunk::height) {
47 position.y -= Chunk::height;
50 while (position.y < 0) {
51 position.y += Chunk::height;
54 while (position.z >= Chunk::depth) {
55 position.z -= Chunk::depth;
58 while (position.z < 0) {
59 position.z += Chunk::depth;
62 model.Position(position);
65 void Entity::Move(const glm::vec3 &delta) noexcept {
66 Position(Position() + delta);
69 void Entity::Rotate(const glm::quat &delta) noexcept {
70 Orientation(delta * Orientation());
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);
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;
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);
91 glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
93 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
98 glm::quat delta_rot(const glm::vec3 &av, float dt) {
99 glm::vec3 half(av * dt * 0.5f);
100 float mag = length(half);
102 float smag = std::sin(mag) / mag;
103 return glm::quat(std::cos(mag), half * smag);
105 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
111 void Entity::Update(int dt) noexcept {
112 float fdt = float(dt);
113 Move(velocity * fdt);
114 Rotate(delta_rot(angular_velocity, fdt));