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(
65 for (Chunk &cur_chunk : chunks.Loaded()) {
67 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
68 candidates.push_back({ &cur_chunk, cur_dist });
72 if (candidates.empty()) return false;
76 coll.depth = std::numeric_limits<float>::infinity();
78 for (Candidate &cand : candidates) {
79 if (cand.dist > coll.depth) continue;
80 WorldCollision cur_coll;
81 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_coll)) {
82 if (cur_coll.depth < coll.depth) {
91 bool World::Intersection(
96 coll.entity = nullptr;
97 coll.depth = std::numeric_limits<float>::infinity();
98 for (Entity &cur_entity : entities) {
99 // TODO: better check for skipping self (because the check might not be for the player)
100 if (&cur_entity == player) {
104 glm::vec3 cur_normal;
105 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(player->ChunkCoords()), &cur_dist, &cur_normal)) {
106 // TODO: fine grained check goes here? maybe?
107 if (cur_dist < coll.depth) {
108 coll.entity = &cur_entity;
109 coll.depth = cur_dist;
110 coll.normal = cur_normal;
118 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
119 AABB box = e.Bounds();
120 glm::mat4 M = e.Transform(player->ChunkCoords());
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(player->ChunkCoords()), col)) {
136 Chunk &World::PlayerChunk() {
137 return chunks.ForceLoad(player->ChunkCoords());
140 Chunk &World::Next(const Chunk &to, const glm::ivec3 &dir) {
141 const Chunk::Pos tgt_pos = to.Position() + dir;
142 return chunks.ForceLoad(tgt_pos);
148 std::vector<WorldCollision> col;
152 void World::Update(int dt) {
153 for (Entity &entity : entities) {
156 for (Entity &entity : entities) {
158 if (entity.WorldCollidable() && Intersection(entity, col)) {
159 // entity collides with the world
160 Resolve(entity, col);
163 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
164 if (iter->CanRemove()) {
165 iter = entities.erase(iter);
170 chunks.Rebase(player->ChunkCoords());
174 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
175 // determine displacement for each cardinal axis and move entity accordingly
176 glm::vec3 min_disp(0.0f);
177 glm::vec3 max_disp(0.0f);
178 for (const WorldCollision &c : col) {
179 if (!c.Blocks()) continue;
180 glm::vec3 local_disp(c.normal * c.depth);
181 // swap if neccessary (normal may point away from the entity)
182 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
185 min_disp = min(min_disp, local_disp);
186 max_disp = max(max_disp, local_disp);
189 // if only one direction is set, use that as the final
190 // if both directions are set, use average
191 glm::vec3 final_disp(0.0f);
192 for (int axis = 0; axis < 3; ++axis) {
193 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
194 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
195 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
197 final_disp[axis] = min_disp[axis];
199 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
200 final_disp[axis] = max_disp[axis];
207 void World::Render(Viewport &viewport) {
208 viewport.WorldPosition(player->Transform(player->ChunkCoords()));
210 BlockLighting &chunk_prog = viewport.ChunkProgram();
211 chunk_prog.SetTexture(block_tex);
212 chunk_prog.SetFogDensity(fog_density);
214 for (Chunk &chunk : chunks.Loaded()) {
215 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
217 glm::mat4 mvp(chunk_prog.GetVP() * m);
218 if (!CullTest(Chunk::Bounds(), mvp)) {
223 DirectionalLighting &entity_prog = viewport.EntityProgram();
224 entity_prog.SetLightDirection(light_direction);
225 entity_prog.SetFogDensity(fog_density);
227 for (Entity &entity : entities) {
228 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);