VoxelEngine
 
Loading...
Searching...
No Matches
AABBCollider.h
1#pragma once
2
3#include <array>
4#include <glm/glm.hpp>
5#include <memory>
6#include <vector>
7
8namespace engine {
9
10 class World;
11 struct AABB;
12
14 static constexpr int AXIS_X_MASK = 1;
15 static constexpr int AXIS_Y_MASK = 2;
16 static constexpr int AXIS_Z_MASK = 4;
17
19 glm::vec3 correction{0.f};
20
25 glm::vec3 t{1.f};
26
28 std::array<std::vector<glm::vec3>, 3> hitPositions;
29
33 unsigned int axis : 3 = 0b000;
34
36 bool grounded : 1 = false;
37
39 float stepHeight = 0.0f;
40 };
41
43 public:
44 AABBCollider(std::shared_ptr<AABB> aabb, float stepHeight = 0.0f);
45
47 void setWorld(std::shared_ptr<World> world);
48
50 void setAABB(std::shared_ptr<AABB> aabb);
51
56 CollisionInfo collide(glm::vec3& moveStep, const glm::vec3& position, float dt);
57
58 protected:
61 static constexpr glm::vec3 s_checkBox{1.5f};
62 static constexpr float s_epsGap = std::numeric_limits<float>::epsilon() * 64.0f;
63
64 std::shared_ptr<World> m_world = nullptr;
65 std::shared_ptr<AABB> m_aabb;
66 float m_stepHeight;
67 float m_height;
68
69 std::vector<AABB> m_aabbCache;
70 glm::ivec3 m_lastPosition;
71 // TODO remove or implement. Should be indicating the direction/velocity for which the AABB cache is generated.
72 glm::vec3 m_cacheDirection;
73
74 float m_stepUpCooldown = 0.0f;
75
76
77 struct SweptResult {
78 float time;
79 int axis;
80 const AABB* bb;
81 };
82
83 void updateAABBCache(const glm::vec3& velocity, const glm::vec3& position);
84 SweptResult swept(const glm::vec3& velocity, const AABB& other) const;
85 float swept1D(int axis, float velocity, const AABB& other);
86
91 float tryStepUp(const glm::vec3& horizontalMove, const SweptResult& bestRes);
92 };
93
94} // namespace engine
Definition AABBCollider.h:42
CollisionInfo collide(glm::vec3 &moveStep, const glm::vec3 &position, float dt)
Checks if the AABB collides with any blocks in the world.
Definition AABBCollider.cpp:42
void setWorld(std::shared_ptr< World > world)
Sets the world for this collider.
Definition AABBCollider.cpp:34
void setAABB(std::shared_ptr< AABB > aabb)
Sets the AABB that will be checked for collisions against the world.
Definition AABBCollider.cpp:38
static constexpr glm::vec3 s_checkBox
The box around the AABB that will be generated, thus checked for collisions against the world.
Definition AABBCollider.h:61
float tryStepUp(const glm::vec3 &horizontalMove, const SweptResult &bestRes)
Attempts to step up when hitting a horizontal obstacle.
Definition AABBCollider.cpp:292
Definition AABBCollider.h:77
Definition AABB.h:11
Definition AABBCollider.h:13
std::array< std::vector< glm::vec3 >, 3 > hitPositions
The positions of the AABBs that will be hit after the move step is applied. (for each axis)
Definition AABBCollider.h:28
float stepHeight
The height that was stepped up (for stairs/slabs). 0 if no step.
Definition AABBCollider.h:39
glm::vec3 correction
The position correction that was applied during the collision resolution, so the AABB stops touching ...
Definition AABBCollider.h:19
glm::vec3 t
The time till the collision happens for all axis with the provided move step.
Definition AABBCollider.h:25
bool grounded
If the AABB is grounded, meaning it is touching the ground.
Definition AABBCollider.h:36