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)
20 , generate(config.gen)
21 , chunks(config.load, types, generate, save)
24 , light_direction(config.light_direction)
25 , fog_density(config.fog_density) {
28 generate.Solids({ 1, 4, 7, 10 });
32 Entity *World::AddPlayer(const std::string &name) {
33 for (Entity *e : players) {
34 if (e->Name() == name) {
38 Entity &player = AddEntity();
40 // TODO: load from save file here
41 player.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
42 player.WorldCollidable(true);
43 player.Position(config.spawn);
44 players.push_back(&player);
45 chunks.QueueSurrounding(player.ChunkCoords());
57 bool CandidateLess(const Candidate &a, const Candidate &b) {
58 return a.dist < b.dist;
61 std::vector<Candidate> candidates;
65 bool World::Intersection(
68 const Chunk::Pos &reference,
73 for (Chunk &cur_chunk : chunks.Loaded()) {
75 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
76 candidates.push_back({ &cur_chunk, cur_dist });
80 if (candidates.empty()) return false;
82 std::sort(candidates.begin(), candidates.end(), CandidateLess);
86 coll.depth = std::numeric_limits<float>::infinity();
88 for (Candidate &cand : candidates) {
89 if (cand.dist > coll.depth) continue;
90 WorldCollision cur_coll;
91 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
92 if (cur_coll.depth < coll.depth) {
101 bool World::Intersection(
104 const Entity &reference,
105 EntityCollision &coll
107 coll.entity = nullptr;
108 coll.depth = std::numeric_limits<float>::infinity();
109 for (Entity &cur_entity : entities) {
110 if (&cur_entity == &reference) {
114 glm::vec3 cur_normal;
115 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
116 // TODO: fine grained check goes here? maybe?
117 if (cur_dist < coll.depth) {
118 coll.entity = &cur_entity;
119 coll.depth = cur_dist;
120 coll.normal = cur_normal;
128 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
129 AABB box = e.Bounds();
130 Chunk::Pos reference = e.ChunkCoords();
131 glm::mat4 M = e.Transform(reference);
133 for (Chunk &cur_chunk : chunks.Loaded()) {
134 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
135 // chunk is not one of the 3x3x3 surrounding the entity
136 // since there's no entity which can extent over 16 blocks, they can be skipped
139 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
149 std::vector<WorldCollision> col;
153 void World::Update(int dt) {
154 for (Entity &entity : entities) {
157 for (Entity &entity : entities) {
159 if (entity.WorldCollidable() && Intersection(entity, col)) {
160 // entity collides with the world
161 Resolve(entity, col);
164 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
165 if (iter->CanRemove()) {
166 iter = entities.erase(iter);
171 // TODO: make flexible
172 chunks.Rebase(players[0]->ChunkCoords());
176 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
177 // determine displacement for each cardinal axis and move entity accordingly
178 glm::vec3 min_disp(0.0f);
179 glm::vec3 max_disp(0.0f);
180 for (const WorldCollision &c : col) {
181 if (!c.Blocks()) continue;
182 glm::vec3 local_disp(c.normal * c.depth);
183 // swap if neccessary (normal may point away from the entity)
184 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
187 min_disp = min(min_disp, local_disp);
188 max_disp = max(max_disp, local_disp);
191 // if only one direction is set, use that as the final
192 // if both directions are set, use average
193 glm::vec3 final_disp(0.0f);
194 for (int axis = 0; axis < 3; ++axis) {
195 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
196 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
197 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
199 final_disp[axis] = min_disp[axis];
201 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
202 final_disp[axis] = max_disp[axis];
209 void World::Render(Viewport &viewport) {
210 DirectionalLighting &entity_prog = viewport.EntityProgram();
211 entity_prog.SetLightDirection(light_direction);
212 entity_prog.SetFogDensity(fog_density);
214 for (Entity &entity : entities) {
215 entity.Render(entity.ChunkTransform(players[0]->ChunkCoords()), entity_prog);