3 #include "EntityCollision.hpp"
4 #include "WorldCollision.hpp"
5 #include "../app/Assets.hpp"
6 #include "../app/TextureIndex.hpp"
7 #include "../graphics/Format.hpp"
8 #include "../graphics/Viewport.hpp"
11 #include <glm/gtx/io.hpp>
12 #include <glm/gtx/transform.hpp>
17 World::World(const Assets &assets, const Config &config, const WorldSave &save)
20 , generate(config.gen)
21 , chunks(config.load, block_type, generate, save)
24 , light_direction(config.light_direction)
25 , fog_density(config.fog_density) {
26 TextureIndex tex_index;
27 assets.LoadBlockTypes("default", block_type, tex_index);
30 assets.LoadTextures(tex_index, block_tex);
31 block_tex.FilterNearest();
35 generate.Solids({ 1, 4, 7, 10 });
37 player = &AddEntity();
38 player->Name("player");
39 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
40 player->WorldCollidable(true);
41 player->Position(config.spawn);
43 chunks.QueueSurrounding(player->ChunkCoords());
54 std::vector<Candidate> candidates;
58 bool World::Intersection(
61 const Chunk::Pos &reference,
66 for (Chunk &cur_chunk : chunks.Loaded()) {
68 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
69 candidates.push_back({ &cur_chunk, cur_dist });
73 if (candidates.empty()) return false;
77 coll.depth = std::numeric_limits<float>::infinity();
79 for (Candidate &cand : candidates) {
80 if (cand.dist > coll.depth) continue;
81 WorldCollision cur_coll;
82 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
83 if (cur_coll.depth < coll.depth) {
92 bool World::Intersection(
95 const Entity &reference,
98 coll.entity = nullptr;
99 coll.depth = std::numeric_limits<float>::infinity();
100 for (Entity &cur_entity : entities) {
101 if (&cur_entity == &reference) {
105 glm::vec3 cur_normal;
106 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
107 // TODO: fine grained check goes here? maybe?
108 if (cur_dist < coll.depth) {
109 coll.entity = &cur_entity;
110 coll.depth = cur_dist;
111 coll.normal = cur_normal;
119 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
120 AABB box = e.Bounds();
121 Chunk::Pos reference = e.ChunkCoords();
122 glm::mat4 M = e.Transform(reference);
124 for (Chunk &cur_chunk : chunks.Loaded()) {
125 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
126 // chunk is not one of the 3x3x3 surrounding the entity
127 // since there's no entity which can extent over 16 blocks, they can be skipped
130 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
138 Chunk &World::PlayerChunk() {
139 return chunks.ForceLoad(player->ChunkCoords());
145 std::vector<WorldCollision> col;
149 void World::Update(int dt) {
150 for (Entity &entity : entities) {
153 for (Entity &entity : entities) {
155 if (entity.WorldCollidable() && Intersection(entity, col)) {
156 // entity collides with the world
157 Resolve(entity, col);
160 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
161 if (iter->CanRemove()) {
162 iter = entities.erase(iter);
167 chunks.Rebase(player->ChunkCoords());
171 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
172 // determine displacement for each cardinal axis and move entity accordingly
173 glm::vec3 min_disp(0.0f);
174 glm::vec3 max_disp(0.0f);
175 for (const WorldCollision &c : col) {
176 if (!c.Blocks()) continue;
177 glm::vec3 local_disp(c.normal * c.depth);
178 // swap if neccessary (normal may point away from the entity)
179 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
182 min_disp = min(min_disp, local_disp);
183 max_disp = max(max_disp, local_disp);
186 // if only one direction is set, use that as the final
187 // if both directions are set, use average
188 glm::vec3 final_disp(0.0f);
189 for (int axis = 0; axis < 3; ++axis) {
190 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
191 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
192 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
194 final_disp[axis] = min_disp[axis];
196 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
197 final_disp[axis] = max_disp[axis];
204 void World::Render(Viewport &viewport) {
205 viewport.WorldPosition(player->Transform(player->ChunkCoords()));
207 BlockLighting &chunk_prog = viewport.ChunkProgram();
208 chunk_prog.SetTexture(block_tex);
209 chunk_prog.SetFogDensity(fog_density);
211 for (Chunk &chunk : chunks.Loaded()) {
212 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
214 glm::mat4 mvp(chunk_prog.GetVP() * m);
215 if (!CullTest(Chunk::Bounds(), mvp)) {
220 DirectionalLighting &entity_prog = viewport.EntityProgram();
221 entity_prog.SetLightDirection(light_direction);
222 entity_prog.SetFogDensity(fog_density);
224 for (Entity &entity : entities) {
225 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);