engine/include/systems/collisions.h

68 lines
1.5 KiB
C
Raw Normal View History

2023-05-01 12:55:49 +00:00
#ifndef ENGINE_INCLUDE_SYSTEMS_COLLISIONS_H_
#define ENGINE_INCLUDE_SYSTEMS_COLLISIONS_H_
2023-01-18 14:42:09 +00:00
2023-04-29 14:22:25 +00:00
#include <cstdint>
2023-05-01 12:55:49 +00:00
#include <vector>
2023-02-02 17:32:19 +00:00
2023-05-02 11:02:43 +00:00
#include <glm/mat4x4.hpp>
2023-05-01 13:13:35 +00:00
#include "components/collider.h"
2023-09-19 07:40:45 +00:00
#include "ecs.h"
2023-04-29 14:22:25 +00:00
2023-01-18 14:42:09 +00:00
namespace engine {
struct Ray {
glm::vec3 origin;
glm::vec3 direction;
2023-05-01 12:55:49 +00:00
};
struct Raycast {
glm::vec3 location;
Entity hit_entity;
float distance;
bool hit;
};
class CollisionSystem : public System {
public:
CollisionSystem(Scene* scene);
void OnComponentInsert(Entity entity) override;
void OnUpdate(float ts) override;
Raycast GetRaycast(Ray ray);
private:
// one node of the BVH
struct BiTreeNode {
enum class Type : uint8_t { BoundingVolume, Entity, Empty };
AABB box1;
AABB box2;
uint32_t index1; // index for either tree entry or entity, depending on type
uint32_t index2;
Type type1;
Type type2;
}; // 60 bytes with alignment of 4 allows for tight packing
// an array of these is used to build the BVH
struct PrimitiveInfo {
Entity entity;
AABB box;
glm::vec3 centroid;
PrimitiveInfo(const AABB& aabb, Entity entity_idx);
};
size_t colliders_size_last_update_ = 0;
size_t colliders_size_now_ = 0;
std::vector<BiTreeNode> bvh_{};
bool RaycastTreeNode(const Ray& ray, const BiTreeNode& node, glm::vec3& location, float& t, Entity& object_index);
static int BuildNode(std::vector<PrimitiveInfo>& prims, std::vector<BiTreeNode>& tree_nodes);
};
} // namespace engine
2023-05-01 12:55:49 +00:00
#endif