]> git.localhorst.tv Git - blank.git/blob - src/world/World.cpp
use collision structures for ray tests
[blank.git] / src / world / World.cpp
1 #include "World.hpp"
2
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"
9
10 #include <limits>
11 #include <glm/gtx/io.hpp>
12 #include <glm/gtx/transform.hpp>
13
14
15 namespace blank {
16
17 World::World(const Assets &assets, const Config &config, const WorldSave &save)
18 : block_type()
19 , block_tex()
20 , generate(config.gen)
21 , chunks(config.load, block_type, generate, save)
22 , player()
23 , entities()
24 , light_direction(config.light_direction)
25 , fog_density(config.fog_density) {
26         TextureIndex tex_index;
27         assets.LoadBlockTypes("default", block_type, tex_index);
28
29         block_tex.Bind();
30         assets.LoadTextures(tex_index, block_tex);
31         block_tex.FilterNearest();
32
33         generate.Space(0);
34         generate.Light(13);
35         generate.Solids({ 1, 4, 7, 10 });
36
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);
42
43         chunks.QueueSurrounding(player->ChunkCoords());
44 }
45
46
47 namespace {
48
49 struct Candidate {
50         Chunk *chunk;
51         float dist;
52 };
53
54 std::vector<Candidate> candidates;
55
56 }
57
58 bool World::Intersection(
59         const Ray &ray,
60         const glm::mat4 &M,
61         WorldCollision &coll
62 ) {
63         candidates.clear();
64
65         for (Chunk &cur_chunk : chunks.Loaded()) {
66                 float cur_dist;
67                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
68                         candidates.push_back({ &cur_chunk, cur_dist });
69                 }
70         }
71
72         if (candidates.empty()) return false;
73
74         coll.chunk = nullptr;
75         coll.block = -1;
76         coll.depth = std::numeric_limits<float>::infinity();
77
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) {
83                                 coll = cur_coll;
84                         }
85                 }
86         }
87
88         return coll.chunk;
89 }
90
91 bool World::Intersection(
92         const Ray &ray,
93         const glm::mat4 &M,
94         EntityCollision &coll
95 ) {
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) {
101                         continue;
102                 }
103                 float cur_dist;
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;
111                         }
112                 }
113         }
114
115         return coll.entity;
116 }
117
118 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
119         AABB box = e.Bounds();
120         glm::mat4 M = e.Transform(player->ChunkCoords());
121         bool any = false;
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
126                         continue;
127                 }
128                 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
129                         any = true;
130                 }
131         }
132         return any;
133 }
134
135
136 Chunk &World::PlayerChunk() {
137         return chunks.ForceLoad(player->ChunkCoords());
138 }
139
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);
143 }
144
145
146 namespace {
147
148 std::vector<WorldCollision> col;
149
150 }
151
152 void World::Update(int dt) {
153         for (Entity &entity : entities) {
154                 entity.Update(dt);
155         }
156         for (Entity &entity : entities) {
157                 col.clear();
158                 if (entity.WorldCollidable() && Intersection(entity, col)) {
159                         // entity collides with the world
160                         Resolve(entity, col);
161                 }
162         }
163         for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
164                 if (iter->CanRemove()) {
165                         iter = entities.erase(iter);
166                 } else {
167                         ++iter;
168                 }
169         }
170         chunks.Rebase(player->ChunkCoords());
171         chunks.Update(dt);
172 }
173
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) {
183                         local_disp *= -1;
184                 }
185                 min_disp = min(min_disp, local_disp);
186                 max_disp = max(max_disp, local_disp);
187         }
188         // for each axis
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;
196                         } else {
197                                 final_disp[axis] = min_disp[axis];
198                         }
199                 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
200                         final_disp[axis] = max_disp[axis];
201                 }
202         }
203         e.Move(final_disp);
204 }
205
206
207 void World::Render(Viewport &viewport) {
208         viewport.WorldPosition(player->Transform(player->ChunkCoords()));
209
210         BlockLighting &chunk_prog = viewport.ChunkProgram();
211         chunk_prog.SetTexture(block_tex);
212         chunk_prog.SetFogDensity(fog_density);
213
214         for (Chunk &chunk : chunks.Loaded()) {
215                 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
216                 chunk_prog.SetM(m);
217                 glm::mat4 mvp(chunk_prog.GetVP() * m);
218                 if (!CullTest(Chunk::Bounds(), mvp)) {
219                         chunk.Draw();
220                 }
221         }
222
223         DirectionalLighting &entity_prog = viewport.EntityProgram();
224         entity_prog.SetLightDirection(light_direction);
225         entity_prog.SetFogDensity(fog_density);
226
227         for (Entity &entity : entities) {
228                 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);
229         }
230 }
231
232 }