2022-11-29 16:34:42 +01:00
|
|
|
#pragma once
|
|
|
|
|
#include "BVH.h"
|
|
|
|
|
#include "Component/Collider.h"
|
|
|
|
|
#include "Component/Transform.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Containers/Array.h"
|
|
|
|
|
#include "Containers/Map.h"
|
|
|
|
|
#include <entt/entt.hpp>
|
2022-11-29 16:34:42 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
|
|
|
|
|
namespace Seele {
|
|
|
|
|
struct Collision {
|
2022-11-29 16:34:42 +01:00
|
|
|
entt::entity a, b;
|
|
|
|
|
};
|
2024-06-09 12:20:04 +02:00
|
|
|
class CollisionSystem {
|
|
|
|
|
public:
|
2023-01-21 18:43:21 +01:00
|
|
|
CollisionSystem(entt::registry& registry);
|
2022-11-29 16:34:42 +01:00
|
|
|
virtual ~CollisionSystem();
|
2023-01-21 18:43:21 +01:00
|
|
|
void detectCollisions(Array<Collision>& collisions);
|
2024-06-09 12:20:04 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct Witness {
|
2023-01-21 18:43:21 +01:00
|
|
|
Vector p;
|
|
|
|
|
Vector n;
|
2022-11-29 16:34:42 +01:00
|
|
|
// for finding p
|
|
|
|
|
uint32_t point1Index;
|
|
|
|
|
// and the face where the plane lies on
|
|
|
|
|
uint32_t point2Index;
|
|
|
|
|
uint32_t point3Index;
|
|
|
|
|
uint32_t point4Index = UINT32_MAX;
|
|
|
|
|
entt::entity source;
|
|
|
|
|
entt::entity other;
|
|
|
|
|
};
|
|
|
|
|
BVH bvh;
|
2023-01-21 18:43:21 +01:00
|
|
|
entt::registry& registry;
|
2022-11-29 16:34:42 +01:00
|
|
|
Map<Pair<entt::entity, entt::entity>, Witness> cachedWitness;
|
|
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
bool checkCollision(Pair<entt::entity, entt::entity> pair);
|
2022-11-29 16:34:42 +01:00
|
|
|
|
2023-01-21 18:43:21 +01:00
|
|
|
void updateWitness(Witness& witness, const Vector& point, const Vector& v1, const Vector& v2);
|
2022-11-29 16:34:42 +01:00
|
|
|
|
|
|
|
|
// returns true if a collision was found
|
|
|
|
|
bool createWitness(Witness& witness, const Component::ShapeBase& source, const Component::ShapeBase& other);
|
|
|
|
|
|
|
|
|
|
// returns true if a collision was found
|
|
|
|
|
bool witnessValid(const Witness& witness, const Component::ShapeBase& shape1, const Component::ShapeBase& shape2);
|
|
|
|
|
};
|
|
|
|
|
} // namespace Seele
|