3 #include "EntityCollision.hpp"
4 #include "WorldCollision.hpp"
5 #include "../app/Assets.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 BlockTypeRegistry &types, const Config &config, const WorldSave &save)
18 , generate(config.gen)
19 , chunks(config.load, types, generate, save)
22 , light_direction(config.light_direction)
23 , fog_density(config.fog_density) {
26 generate.Solids({ 1, 4, 7, 10 });
28 player = &AddEntity();
29 player->Name("player");
30 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
31 player->WorldCollidable(true);
32 player->Position(config.spawn);
34 chunks.QueueSurrounding(player->ChunkCoords());
45 std::vector<Candidate> candidates;
49 bool World::Intersection(
52 const Chunk::Pos &reference,
57 for (Chunk &cur_chunk : chunks.Loaded()) {
59 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
60 candidates.push_back({ &cur_chunk, cur_dist });
64 if (candidates.empty()) return false;
68 coll.depth = std::numeric_limits<float>::infinity();
70 for (Candidate &cand : candidates) {
71 if (cand.dist > coll.depth) continue;
72 WorldCollision cur_coll;
73 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
74 if (cur_coll.depth < coll.depth) {
83 bool World::Intersection(
86 const Entity &reference,
89 coll.entity = nullptr;
90 coll.depth = std::numeric_limits<float>::infinity();
91 for (Entity &cur_entity : entities) {
92 if (&cur_entity == &reference) {
97 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
98 // TODO: fine grained check goes here? maybe?
99 if (cur_dist < coll.depth) {
100 coll.entity = &cur_entity;
101 coll.depth = cur_dist;
102 coll.normal = cur_normal;
110 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
111 AABB box = e.Bounds();
112 Chunk::Pos reference = e.ChunkCoords();
113 glm::mat4 M = e.Transform(reference);
115 for (Chunk &cur_chunk : chunks.Loaded()) {
116 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
117 // chunk is not one of the 3x3x3 surrounding the entity
118 // since there's no entity which can extent over 16 blocks, they can be skipped
121 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
129 Chunk &World::PlayerChunk() {
130 return chunks.ForceLoad(player->ChunkCoords());
136 std::vector<WorldCollision> col;
140 void World::Update(int dt) {
141 for (Entity &entity : entities) {
144 for (Entity &entity : entities) {
146 if (entity.WorldCollidable() && Intersection(entity, col)) {
147 // entity collides with the world
148 Resolve(entity, col);
151 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
152 if (iter->CanRemove()) {
153 iter = entities.erase(iter);
158 chunks.Rebase(player->ChunkCoords());
162 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
163 // determine displacement for each cardinal axis and move entity accordingly
164 glm::vec3 min_disp(0.0f);
165 glm::vec3 max_disp(0.0f);
166 for (const WorldCollision &c : col) {
167 if (!c.Blocks()) continue;
168 glm::vec3 local_disp(c.normal * c.depth);
169 // swap if neccessary (normal may point away from the entity)
170 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
173 min_disp = min(min_disp, local_disp);
174 max_disp = max(max_disp, local_disp);
177 // if only one direction is set, use that as the final
178 // if both directions are set, use average
179 glm::vec3 final_disp(0.0f);
180 for (int axis = 0; axis < 3; ++axis) {
181 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
182 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
183 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
185 final_disp[axis] = min_disp[axis];
187 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
188 final_disp[axis] = max_disp[axis];
195 void World::Render(Viewport &viewport) {
196 DirectionalLighting &entity_prog = viewport.EntityProgram();
197 entity_prog.SetLightDirection(light_direction);
198 entity_prog.SetFogDensity(fog_density);
200 for (Entity &entity : entities) {
201 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);