3 #include "EntityCollision.hpp"
4 #include "WorldCollision.hpp"
5 #include "../app/Assets.hpp"
6 #include "../graphics/Format.hpp"
7 #include "../graphics/Viewport.hpp"
11 #include <glm/gtx/io.hpp>
12 #include <glm/gtx/transform.hpp>
17 World::World(const BlockTypeRegistry &types, const Config &config, const WorldSave &save)
19 , generate(config.gen)
20 , chunks(config.load, types, generate, save)
23 , light_direction(config.light_direction)
24 , fog_density(config.fog_density) {
27 generate.Solids({ 1, 4, 7, 10 });
29 player = &AddEntity();
30 player->Name("player");
31 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
32 player->WorldCollidable(true);
33 player->Position(config.spawn);
35 chunks.QueueSurrounding(player->ChunkCoords());
46 bool CandidateLess(const Candidate &a, const Candidate &b) {
47 return a.dist < b.dist;
50 std::vector<Candidate> candidates;
54 bool World::Intersection(
57 const Chunk::Pos &reference,
62 for (Chunk &cur_chunk : chunks.Loaded()) {
64 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
65 candidates.push_back({ &cur_chunk, cur_dist });
69 if (candidates.empty()) return false;
71 std::sort(candidates.begin(), candidates.end(), CandidateLess);
75 coll.depth = std::numeric_limits<float>::infinity();
77 for (Candidate &cand : candidates) {
78 if (cand.dist > coll.depth) continue;
79 WorldCollision cur_coll;
80 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
81 if (cur_coll.depth < coll.depth) {
90 bool World::Intersection(
93 const Entity &reference,
96 coll.entity = nullptr;
97 coll.depth = std::numeric_limits<float>::infinity();
98 for (Entity &cur_entity : entities) {
99 if (&cur_entity == &reference) {
103 glm::vec3 cur_normal;
104 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
105 // TODO: fine grained check goes here? maybe?
106 if (cur_dist < coll.depth) {
107 coll.entity = &cur_entity;
108 coll.depth = cur_dist;
109 coll.normal = cur_normal;
117 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
118 AABB box = e.Bounds();
119 Chunk::Pos reference = e.ChunkCoords();
120 glm::mat4 M = e.Transform(reference);
122 for (Chunk &cur_chunk : chunks.Loaded()) {
123 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
124 // chunk is not one of the 3x3x3 surrounding the entity
125 // since there's no entity which can extent over 16 blocks, they can be skipped
128 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
136 Chunk &World::PlayerChunk() {
137 return chunks.ForceLoad(player->ChunkCoords());
143 std::vector<WorldCollision> col;
147 void World::Update(int dt) {
148 for (Entity &entity : entities) {
151 for (Entity &entity : entities) {
153 if (entity.WorldCollidable() && Intersection(entity, col)) {
154 // entity collides with the world
155 Resolve(entity, col);
158 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
159 if (iter->CanRemove()) {
160 iter = entities.erase(iter);
165 chunks.Rebase(player->ChunkCoords());
169 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
170 // determine displacement for each cardinal axis and move entity accordingly
171 glm::vec3 min_disp(0.0f);
172 glm::vec3 max_disp(0.0f);
173 for (const WorldCollision &c : col) {
174 if (!c.Blocks()) continue;
175 glm::vec3 local_disp(c.normal * c.depth);
176 // swap if neccessary (normal may point away from the entity)
177 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
180 min_disp = min(min_disp, local_disp);
181 max_disp = max(max_disp, local_disp);
184 // if only one direction is set, use that as the final
185 // if both directions are set, use average
186 glm::vec3 final_disp(0.0f);
187 for (int axis = 0; axis < 3; ++axis) {
188 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
189 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
190 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
192 final_disp[axis] = min_disp[axis];
194 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
195 final_disp[axis] = max_disp[axis];
202 void World::Render(Viewport &viewport) {
203 DirectionalLighting &entity_prog = viewport.EntityProgram();
204 entity_prog.SetLightDirection(light_direction);
205 entity_prog.SetFogDensity(fog_density);
207 for (Entity &entity : entities) {
208 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);