2 #include "EntityState.hpp"
6 #include "ChunkIndex.hpp"
7 #include "EntityCollision.hpp"
8 #include "WorldCollision.hpp"
9 #include "../app/Assets.hpp"
10 #include "../graphics/Format.hpp"
11 #include "../graphics/Viewport.hpp"
16 #include <glm/gtx/io.hpp>
17 #include <glm/gtx/quaternion.hpp>
18 #include <glm/gtx/transform.hpp>
23 Entity::Entity() noexcept
30 , world_collision(false)
36 void Entity::Position(const glm::ivec3 &c, const glm::vec3 &b) noexcept {
41 void Entity::Position(const glm::vec3 &pos) noexcept {
42 state.block_pos = pos;
43 state.AdjustPosition();
46 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
47 glm::mat4 transform = Transform(chunk_offset);
48 glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
50 glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
52 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
57 glm::quat delta_rot(const glm::vec3 &av, float dt) {
58 glm::vec3 half(av * dt * 0.5f);
59 float mag = length(half);
61 float smag = std::sin(mag) / mag;
62 return glm::quat(std::cos(mag), half * smag);
64 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
70 void Entity::Update(int dt) noexcept {
75 EntityState::EntityState()
79 , orient(1.0f, 0.0f, 0.0f, 0.0f)
84 void EntityState::Update(int dt) noexcept {
85 float fdt = float(dt);
86 block_pos += velocity * fdt;
87 orient = delta_rot(ang_vel, fdt) * orient;
91 void EntityState::AdjustPosition() noexcept {
92 while (block_pos.x >= Chunk::width) {
93 block_pos.x -= Chunk::width;
96 while (block_pos.x < 0) {
97 block_pos.x += Chunk::width;
100 while (block_pos.y >= Chunk::height) {
101 block_pos.y -= Chunk::height;
104 while (block_pos.y < 0) {
105 block_pos.y += Chunk::height;
108 while (block_pos.z >= Chunk::depth) {
109 block_pos.z -= Chunk::depth;
112 while (block_pos.z < 0) {
113 block_pos.z += Chunk::depth;
118 glm::mat4 EntityState::Transform(const glm::ivec3 &reference) const noexcept {
119 const glm::vec3 translation = RelativePosition(reference);
120 glm::mat4 transform(toMat4(orient));
121 transform[3].x = translation.x;
122 transform[3].y = translation.y;
123 transform[3].z = translation.z;
128 Player::Player(Entity &e, ChunkIndex &c)
139 void Player::Update(int dt) {
140 chunks.Rebase(entity.ChunkCoords());
144 World::World(const BlockTypeRegistry &types, const Config &config)
150 , light_direction(config.light_direction)
151 , fog_density(config.fog_density) {
160 Player *World::AddPlayer(const std::string &name) {
161 for (Player &p : players) {
162 if (p.Name() == name) {
166 Entity &entity = AddEntity();
168 entity.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
169 entity.WorldCollidable(true);
170 ChunkIndex &index = chunks.MakeIndex(entity.ChunkCoords(), 6);
171 players.emplace_back(entity, index);
172 return &players.back();
175 Player *World::AddPlayer(const std::string &name, std::uint32_t id) {
176 for (Player &p : players) {
177 if (p.Name() == name) {
181 Entity *entity = AddEntity(id);
186 entity->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
187 entity->WorldCollidable(true);
188 ChunkIndex &index = chunks.MakeIndex(entity->ChunkCoords(), 6);
189 players.emplace_back(*entity, index);
190 return &players.back();
193 Entity &World::AddEntity() {
194 if (entities.empty()) {
195 entities.emplace_back();
196 entities.back().ID(1);
197 return entities.back();
199 if (entities.back().ID() < std::numeric_limits<std::uint32_t>::max()) {
200 std::uint32_t id = entities.back().ID() + 1;
201 entities.emplace_back();
202 entities.back().ID(id);
203 return entities.back();
205 std::uint32_t id = 1;
206 auto position = entities.begin();
207 auto end = entities.end();
208 while (position != end && position->ID() == id) {
212 auto entity = entities.emplace(position);
217 Entity *World::AddEntity(std::uint32_t id) {
218 if (entities.empty() || entities.back().ID() < id) {
219 entities.emplace_back();
220 entities.back().ID(id);
221 return &entities.back();
224 auto position = entities.begin();
225 auto end = entities.end();
226 while (position != end && position->ID() < id) {
229 if (position != end && position->ID() == id) {
232 auto entity = entities.emplace(position);
237 Entity &World::ForceAddEntity(std::uint32_t id) {
238 if (entities.empty() || entities.back().ID() < id) {
239 entities.emplace_back();
240 entities.back().ID(id);
241 return entities.back();
244 auto position = entities.begin();
245 auto end = entities.end();
246 while (position != end && position->ID() < id) {
249 if (position != end && position->ID() == id) {
252 auto entity = entities.emplace(position);
265 bool CandidateLess(const Candidate &a, const Candidate &b) {
266 return a.dist < b.dist;
269 std::vector<Candidate> candidates;
273 bool World::Intersection(
276 const Chunk::Pos &reference,
281 for (Chunk &cur_chunk : chunks) {
283 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
284 candidates.push_back({ &cur_chunk, cur_dist });
288 if (candidates.empty()) return false;
290 std::sort(candidates.begin(), candidates.end(), CandidateLess);
292 coll.chunk = nullptr;
294 coll.depth = std::numeric_limits<float>::infinity();
296 for (Candidate &cand : candidates) {
297 if (cand.dist > coll.depth) continue;
298 WorldCollision cur_coll;
299 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
300 if (cur_coll.depth < coll.depth) {
309 bool World::Intersection(
312 const Entity &reference,
313 EntityCollision &coll
315 coll.entity = nullptr;
316 coll.depth = std::numeric_limits<float>::infinity();
317 for (Entity &cur_entity : entities) {
318 if (&cur_entity == &reference) {
322 glm::vec3 cur_normal;
323 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
324 // TODO: fine grained check goes here? maybe?
325 if (cur_dist < coll.depth) {
326 coll.entity = &cur_entity;
327 coll.depth = cur_dist;
328 coll.normal = cur_normal;
336 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
337 AABB box = e.Bounds();
338 Chunk::Pos reference = e.ChunkCoords();
339 glm::mat4 M = e.Transform(reference);
341 for (Chunk &cur_chunk : chunks) {
342 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
343 // chunk is not one of the 3x3x3 surrounding the entity
344 // since there's no entity which can extent over 16 blocks, they can be skipped
347 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
357 std::vector<WorldCollision> col;
361 void World::Update(int dt) {
362 for (Entity &entity : entities) {
365 for (Entity &entity : entities) {
367 if (entity.WorldCollidable() && Intersection(entity, col)) {
368 // entity collides with the world
369 Resolve(entity, col);
372 for (Player &player : players) {
375 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
376 if (iter->CanRemove()) {
377 iter = RemoveEntity(iter);
384 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
385 // determine displacement for each cardinal axis and move entity accordingly
386 glm::vec3 min_disp(0.0f);
387 glm::vec3 max_disp(0.0f);
388 for (const WorldCollision &c : col) {
389 if (!c.Blocks()) continue;
390 glm::vec3 local_disp(c.normal * c.depth);
391 // swap if neccessary (normal may point away from the entity)
392 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
395 min_disp = min(min_disp, local_disp);
396 max_disp = max(max_disp, local_disp);
399 // if only one direction is set, use that as the final
400 // if both directions are set, use average
401 glm::vec3 final_disp(0.0f);
402 for (int axis = 0; axis < 3; ++axis) {
403 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
404 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
405 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
407 final_disp[axis] = min_disp[axis];
409 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
410 final_disp[axis] = max_disp[axis];
413 e.Position(e.Position() + final_disp);
416 World::EntityHandle World::RemoveEntity(EntityHandle &eh) {
418 for (auto player = players.begin(), end = players.end(); player != end;) {
419 if (&player->GetEntity() == &*eh) {
420 chunks.UnregisterIndex(player->GetChunks());
421 player = players.erase(player);
427 return entities.erase(eh);
431 void World::Render(Viewport &viewport) {
432 DirectionalLighting &entity_prog = viewport.EntityProgram();
433 entity_prog.SetLightDirection(light_direction);
434 entity_prog.SetFogDensity(fog_density);
436 for (Entity &entity : entities) {
437 entity.Render(entity.Transform(players.front().GetEntity().ChunkCoords()), entity_prog);