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
24 , angular_velocity(0.0f)
26 , world_collision(false)
32 void Entity::Position(const Chunk::Pos &c, const glm::vec3 &pos) noexcept {
37 void Entity::Position(const glm::vec3 &pos) noexcept {
38 glm::vec3 position(pos);
39 while (position.x >= Chunk::width) {
40 position.x -= Chunk::width;
43 while (position.x < 0) {
44 position.x += Chunk::width;
47 while (position.y >= Chunk::height) {
48 position.y -= Chunk::height;
51 while (position.y < 0) {
52 position.y += Chunk::height;
55 while (position.z >= Chunk::depth) {
56 position.z -= Chunk::depth;
59 while (position.z < 0) {
60 position.z += Chunk::depth;
63 model.Position(position);
66 void Entity::Move(const glm::vec3 &delta) noexcept {
67 Position(Position() + delta);
70 void Entity::Rotate(const glm::quat &delta) noexcept {
71 Orientation(delta * Orientation());
74 glm::mat4 Entity::ChunkTransform(const Chunk::Pos &chunk_offset) const noexcept {
75 const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent());
76 return glm::translate(translation);
79 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
80 const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent()) + Position();
81 glm::mat4 transform(toMat4(Orientation()));
82 transform[3].x = translation.x;
83 transform[3].y = translation.y;
84 transform[3].z = translation.z;
88 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
89 glm::mat4 transform = Transform(chunk_offset);
90 glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
92 glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
94 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
99 glm::quat delta_rot(const glm::vec3 &av, float dt) {
100 glm::vec3 half(av * dt * 0.5f);
101 float mag = length(half);
103 float smag = std::sin(mag) / mag;
104 return glm::quat(std::cos(mag), half * smag);
106 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
112 void Entity::Update(int dt) noexcept {
113 float fdt = float(dt);
114 Move(velocity * fdt);
115 Rotate(delta_rot(angular_velocity, fdt));