3 #include "WorldCollision.hpp"
4 #include "../app/Assets.hpp"
5 #include "../app/TextureIndex.hpp"
6 #include "../graphics/Format.hpp"
7 #include "../graphics/Viewport.hpp"
10 #include <glm/gtx/io.hpp>
11 #include <glm/gtx/transform.hpp>
16 World::World(const Assets &assets, const Config &config, const WorldSave &save)
19 , generate(config.gen)
20 , chunks(config.load, block_type, generate, save)
23 , light_direction(config.light_direction)
24 , fog_density(config.fog_density) {
25 TextureIndex tex_index;
26 assets.LoadBlockTypes("default", block_type, tex_index);
29 assets.LoadTextures(tex_index, block_tex);
30 block_tex.FilterNearest();
34 generate.Solids({ 1, 4, 7, 10 });
36 player = &AddEntity();
37 player->Name("player");
38 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
39 player->WorldCollidable(true);
40 player->Position(config.spawn);
42 chunks.QueueSurrounding(player->ChunkCoords());
53 std::vector<Candidate> candidates;
57 bool World::Intersection(
67 for (Chunk &cur_chunk : chunks.Loaded()) {
69 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
70 candidates.push_back({ &cur_chunk, cur_dist });
74 if (candidates.empty()) return false;
77 dist = std::numeric_limits<float>::infinity();
80 for (Candidate &cand : candidates) {
81 if (cand.dist > dist) continue;
85 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
86 if (cur_dist < dist) {
98 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
99 AABB box = e.Bounds();
100 glm::mat4 M = e.Transform(player->ChunkCoords());
102 for (Chunk &cur_chunk : chunks.Loaded()) {
103 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
104 // chunk is not one of the 3x3x3 surrounding the entity
105 // since there's no entity which can extent over 16 blocks, they can be skipped
108 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
116 Chunk &World::PlayerChunk() {
117 return chunks.ForceLoad(player->ChunkCoords());
120 Chunk &World::Next(const Chunk &to, const glm::ivec3 &dir) {
121 const Chunk::Pos tgt_pos = to.Position() + dir;
122 return chunks.ForceLoad(tgt_pos);
128 std::vector<WorldCollision> col;
132 void World::Update(int dt) {
133 for (Entity &entity : entities) {
136 for (Entity &entity : entities) {
138 if (entity.WorldCollidable() && Intersection(entity, col)) {
139 // entity collides with the world
140 Resolve(entity, col);
143 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
144 if (iter->CanRemove()) {
145 iter = entities.erase(iter);
150 chunks.Rebase(player->ChunkCoords());
154 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
155 // determine displacement for each cardinal axis and move entity accordingly
156 glm::vec3 min_disp(0.0f);
157 glm::vec3 max_disp(0.0f);
158 for (const WorldCollision &c : col) {
159 if (!c.Blocks()) continue;
160 glm::vec3 local_disp(c.normal * c.depth);
161 // swap if neccessary (normal may point away from the entity)
162 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
165 min_disp = min(min_disp, local_disp);
166 max_disp = max(max_disp, local_disp);
169 // if only one direction is set, use that as the final
170 // if both directions are set, use average
171 glm::vec3 final_disp(0.0f);
172 for (int axis = 0; axis < 3; ++axis) {
173 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
174 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
175 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
177 final_disp[axis] = min_disp[axis];
179 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
180 final_disp[axis] = max_disp[axis];
187 void World::Render(Viewport &viewport) {
188 viewport.WorldPosition(player->Transform(player->ChunkCoords()));
190 BlockLighting &chunk_prog = viewport.ChunkProgram();
191 chunk_prog.SetTexture(block_tex);
192 chunk_prog.SetFogDensity(fog_density);
194 for (Chunk &chunk : chunks.Loaded()) {
195 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
197 glm::mat4 mvp(chunk_prog.GetVP() * m);
198 if (!CullTest(Chunk::Bounds(), mvp)) {
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);