From: Daniel Karbach Date: Thu, 25 Jun 2015 15:44:41 +0000 (+0200) Subject: collect collisions for each entity X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=32909aa3224ec0ed5656721178eb6ad31cd047df;p=blank.git collect collisions for each entity --- diff --git a/src/model/Shape.hpp b/src/model/Shape.hpp index 39dd50f..212e439 100644 --- a/src/model/Shape.hpp +++ b/src/model/Shape.hpp @@ -79,7 +79,9 @@ struct Shape { virtual bool Intersects( const glm::mat4 &M, const AABB &box, - const glm::mat4 &box_M + const glm::mat4 &box_M, + float &depth, + glm::vec3 &normal ) const noexcept = 0; protected: diff --git a/src/model/shape.cpp b/src/model/shape.cpp index 8aa1c25..df98f93 100644 --- a/src/model/shape.cpp +++ b/src/model/shape.cpp @@ -83,7 +83,9 @@ bool NullShape::Intersects( bool NullShape::Intersects( const glm::mat4 &, const AABB &, - const glm::mat4 & + const glm::mat4 &, + float &, + glm::vec3 & ) const noexcept { return false; } @@ -178,10 +180,10 @@ bool CuboidShape::Intersects( bool CuboidShape::Intersects( const glm::mat4 &M, const AABB &box, - const glm::mat4 &box_M + const glm::mat4 &box_M, + float &depth, + glm::vec3 &normal ) const noexcept { - float depth; - glm::vec3 normal; return Intersection(bb, M, box, box_M, depth, normal); } @@ -347,10 +349,11 @@ bool StairShape::Intersects( bool StairShape::Intersects( const glm::mat4 &M, const AABB &box, - const glm::mat4 &box_M + const glm::mat4 &box_M, + float &depth, + glm::vec3 &normal ) const noexcept { - float depth; - glm::vec3 normal; + // TODO: this is wrong, but simple. so for now will have to do return Intersection(bot, M, box, box_M, depth, normal) || Intersection(top, M, box, box_M, depth, normal); } diff --git a/src/model/shapes.hpp b/src/model/shapes.hpp index 8ead23d..4820530 100644 --- a/src/model/shapes.hpp +++ b/src/model/shapes.hpp @@ -17,7 +17,7 @@ public: NullShape(); bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; - bool Intersects(const glm::mat4 &, const AABB &, const glm::mat4 &) const noexcept override; + bool Intersects(const glm::mat4 &, const AABB &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; }; @@ -29,7 +29,7 @@ public: CuboidShape(const AABB &bounds); bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; - bool Intersects(const glm::mat4 &, const AABB &, const glm::mat4 &) const noexcept override; + bool Intersects(const glm::mat4 &, const AABB &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; private: AABB bb; @@ -44,7 +44,7 @@ public: StairShape(const AABB &bounds, const glm::vec2 &clip); bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; - bool Intersects(const glm::mat4 &, const AABB &, const glm::mat4 &) const noexcept override; + bool Intersects(const glm::mat4 &, const AABB &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; private: AABB top, bot; diff --git a/src/world/Chunk.hpp b/src/world/Chunk.hpp index 8a2ded6..02f146e 100644 --- a/src/world/Chunk.hpp +++ b/src/world/Chunk.hpp @@ -6,6 +6,7 @@ #include "../model/BlockModel.hpp" #include "../model/geometry.hpp" +#include #include #include @@ -13,6 +14,7 @@ namespace blank { class BlockType; +class WorldCollision; /// cube of size 16 (256 tiles, 4096 blocks) class Chunk { @@ -146,7 +148,8 @@ public: bool Intersection( const AABB &box, const glm::mat4 &Mbox, - const glm::mat4 &Mchunk) const noexcept; + const glm::mat4 &Mchunk, + std::vector &) const noexcept; void Position(const Pos &pos) noexcept { position = pos; } const Pos &Position() const noexcept { return position; } diff --git a/src/world/World.cpp b/src/world/World.cpp index 4ab116c..cb8186d 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -1,5 +1,6 @@ #include "World.hpp" +#include "WorldCollision.hpp" #include "../graphics/BlockLighting.hpp" #include "../graphics/DirectionalLighting.hpp" @@ -175,13 +176,13 @@ bool World::Intersection( return chunk; } -bool World::Intersection(const Entity &e) { +bool World::Intersection(const Entity &e, std::vector &col) { AABB box = e.Bounds(); glm::mat4 M = e.Transform(player->ChunkCoords()); // TODO: this only needs to check the chunks surrounding the entity's chunk position // need find out if that is quicker than the rough chunk bounds test for (Chunk &cur_chunk : chunks.Loaded()) { - if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()))) { + if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) { return true; } } @@ -199,20 +200,31 @@ Chunk &World::Next(const Chunk &to, const glm::tvec3 &dir) { } +namespace { + +std::vector col; + +} + void World::Update(int dt) { for (Entity &entity : entities) { entity.Update(dt); } for (Entity &entity : entities) { - if (entity.WorldCollidable() && Intersection(entity)) { + col.clear(); + if (entity.WorldCollidable() && Intersection(entity, col)) { // entity collides with the world - std::cout << entity.Name() << " entity intersects world" << std::endl; + Resolve(entity, col); } } chunks.Rebase(player->ChunkCoords()); chunks.Update(dt); } +void World::Resolve(const Entity &e, std::vector &col) { + std::cout << e.Name() << " entity intersects world at " << col.size() << " blocks" << std::endl; +} + void World::Render(BlockLighting &chunk_prog, DirectionalLighting &entity_prog) { chunk_prog.Activate(); diff --git a/src/world/World.hpp b/src/world/World.hpp index 7f38942..183fa60 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -8,6 +8,7 @@ #include "../model/shapes.hpp" #include +#include #include @@ -15,6 +16,7 @@ namespace blank { class BlockLighting; class DirectionalLighting; +class WorldCollision; class World { @@ -44,7 +46,8 @@ public: float &dist, glm::vec3 &normal); - bool Intersection(const Entity &e); + bool Intersection(const Entity &e, std::vector &); + void Resolve(const Entity &e, std::vector &); BlockTypeRegistry &BlockTypes() { return blockType; } diff --git a/src/world/WorldCollision.hpp b/src/world/WorldCollision.hpp new file mode 100644 index 0000000..eb2e5e2 --- /dev/null +++ b/src/world/WorldCollision.hpp @@ -0,0 +1,26 @@ +#ifndef BLANK_WORLD_WORLDCOLLISION_HPP_ +#define BLANK_WORLD_WORLDCOLLISION_HPP_ + +#include + + +namespace blank { + +class Chunk; + +struct WorldCollision { + + const Chunk *chunk; + int block; + + float depth; + glm::vec3 normal; + + WorldCollision(const Chunk *c, int b, float d, const glm::vec3 &n) + : chunk(c), block(b), depth(d), normal(n) { } + +}; + +} + +#endif diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index 7591266..2c5e7ec 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -3,6 +3,7 @@ #include "ChunkLoader.hpp" #include "Generator.hpp" +#include "WorldCollision.hpp" #include #include @@ -485,8 +486,10 @@ bool Chunk::Intersection( bool Chunk::Intersection( const AABB &box, const glm::mat4 &Mbox, - const glm::mat4 &Mchunk + const glm::mat4 &Mchunk, + std::vector &col ) const noexcept { + bool any = false; float penetration; glm::vec3 normal; @@ -500,13 +503,14 @@ bool Chunk::Intersection( if (!type.visible) { continue; } - if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox)) { - return true; + if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) { + col.emplace_back(this, idx, penetration, normal); + any = true; } } } } - return false; + return any; }