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:
bool NullShape::Intersects(
const glm::mat4 &,
const AABB &,
- const glm::mat4 &
+ const glm::mat4 &,
+ float &,
+ glm::vec3 &
) const noexcept {
return false;
}
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);
}
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);
}
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;
};
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;
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;
#include "../model/BlockModel.hpp"
#include "../model/geometry.hpp"
+#include <vector>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
namespace blank {
class BlockType;
+class WorldCollision;
/// cube of size 16 (256 tiles, 4096 blocks)
class Chunk {
bool Intersection(
const AABB &box,
const glm::mat4 &Mbox,
- const glm::mat4 &Mchunk) const noexcept;
+ const glm::mat4 &Mchunk,
+ std::vector<WorldCollision> &) const noexcept;
void Position(const Pos &pos) noexcept { position = pos; }
const Pos &Position() const noexcept { return position; }
#include "World.hpp"
+#include "WorldCollision.hpp"
#include "../graphics/BlockLighting.hpp"
#include "../graphics/DirectionalLighting.hpp"
return chunk;
}
-bool World::Intersection(const Entity &e) {
+bool World::Intersection(const Entity &e, std::vector<WorldCollision> &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;
}
}
}
+namespace {
+
+std::vector<WorldCollision> 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<WorldCollision> &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();
#include "../model/shapes.hpp"
#include <list>
+#include <vector>
#include <glm/glm.hpp>
class BlockLighting;
class DirectionalLighting;
+class WorldCollision;
class World {
float &dist,
glm::vec3 &normal);
- bool Intersection(const Entity &e);
+ bool Intersection(const Entity &e, std::vector<WorldCollision> &);
+ void Resolve(const Entity &e, std::vector<WorldCollision> &);
BlockTypeRegistry &BlockTypes() { return blockType; }
--- /dev/null
+#ifndef BLANK_WORLD_WORLDCOLLISION_HPP_
+#define BLANK_WORLD_WORLDCOLLISION_HPP_
+
+#include <glm/glm.hpp>
+
+
+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
#include "ChunkLoader.hpp"
#include "Generator.hpp"
+#include "WorldCollision.hpp"
#include <algorithm>
#include <iostream>
bool Chunk::Intersection(
const AABB &box,
const glm::mat4 &Mbox,
- const glm::mat4 &Mchunk
+ const glm::mat4 &Mchunk,
+ std::vector<WorldCollision> &col
) const noexcept {
+ bool any = false;
float penetration;
glm::vec3 normal;
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;
}