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
25 , angular_velocity(0.0f)
26 , rotation(1.0f, 0.0f, 0.0f, 0.0f)
27 , world_collision(false)
33 void Entity::SetShape(const Shape *s, const glm::vec3 &color) {
36 shape->Vertices(model_buffer.vertices, model_buffer.normals, model_buffer.indices);
37 model_buffer.colors.resize(shape->VertexCount(), color);
38 model.Update(model_buffer);
41 void Entity::SetShapeless() noexcept {
46 void Entity::Velocity(const glm::vec3 &vel) noexcept {
50 void Entity::Position(const Chunk::Pos &c, const Block::Pos &pos) noexcept {
55 void Entity::Position(const Block::Pos &pos) noexcept {
57 while (position.x >= Chunk::width) {
58 position.x -= Chunk::width;
61 while (position.x < 0) {
62 position.x += Chunk::width;
65 while (position.y >= Chunk::height) {
66 position.y -= Chunk::height;
69 while (position.y < 0) {
70 position.y += Chunk::height;
73 while (position.z >= Chunk::depth) {
74 position.z -= Chunk::depth;
77 while (position.z < 0) {
78 position.z += Chunk::depth;
83 void Entity::Move(const glm::vec3 &delta) noexcept {
84 Position(position + delta);
87 void Entity::AngularVelocity(const glm::vec3 &v) noexcept {
91 void Entity::Rotation(const glm::quat &rot) noexcept {
95 void Entity::Rotate(const glm::quat &delta) noexcept {
96 Rotation(delta * Rotation());
99 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
100 const glm::vec3 translation = glm::vec3((chunk - chunk_offset) * Chunk::Extent()) + position;
101 glm::mat4 transform(toMat4(Rotation()));
102 transform[3].x = translation.x;
103 transform[3].y = translation.y;
104 transform[3].z = translation.z;
108 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
109 glm::mat4 transform = Transform(chunk_offset);
110 glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
112 glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
114 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
119 glm::quat delta_rot(const glm::vec3 &av, float dt) {
120 glm::vec3 half(av * dt * 0.5f);
121 float mag = length(half);
123 float smag = std::sin(mag) / mag;
124 return glm::quat(std::cos(mag), half * smag);
126 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
132 void Entity::Update(int dt) noexcept {
133 float fdt = float(dt);
134 Move(velocity * fdt);
135 Rotate(delta_rot(angular_velocity, fdt));