engine/include/systems/collisions.h

61 lines
1.7 KiB
C
Raw Permalink 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 {
2023-05-01 12:55:49 +00:00
class PhysicsSystem : public System {
public:
PhysicsSystem(Scene* scene);
void OnUpdate(float ts) override;
2023-09-19 07:40:45 +00:00
void OnComponentInsert(Entity entity) override;
2023-05-01 12:55:49 +00:00
struct CollisionEvent {
bool is_collision_enter; // false == collision exit
2023-09-19 07:40:45 +00:00
Entity collided_entity; // the entity that this entity collided with
2023-05-01 12:55:49 +00:00
glm::vec3 normal; // the normal of the surface this entity collided with;
// ignored on collision exit
glm::vec3 point; // where the collision was detected
};
private:
// dynamic arrays to avoid realloc on every frame
// entity, aabb, is_trigger
2023-09-19 07:40:45 +00:00
std::vector<std::tuple<Entity, AABB, bool>> static_aabbs_{};
std::vector<std::tuple<Entity, AABB, bool>> dynamic_aabbs_{};
2023-05-01 12:55:49 +00:00
struct PossibleCollision {
2023-09-19 07:40:45 +00:00
PossibleCollision(Entity static_entity, AABB static_aabb,
bool static_trigger, Entity dynamic_entity,
2023-05-01 12:55:49 +00:00
AABB dynamic_aabb, bool dynamic_trigger)
: static_entity(static_entity),
static_aabb(static_aabb),
static_trigger(static_trigger),
dynamic_entity(dynamic_entity),
dynamic_aabb(dynamic_aabb),
dynamic_trigger(dynamic_trigger) {}
2023-09-19 07:40:45 +00:00
Entity static_entity;
2023-05-01 12:55:49 +00:00
AABB static_aabb;
bool static_trigger;
2023-09-19 07:40:45 +00:00
Entity dynamic_entity;
2023-05-01 12:55:49 +00:00
AABB dynamic_aabb;
bool dynamic_trigger;
};
std::vector<PossibleCollision> possible_collisions_{};
2023-09-19 07:40:45 +00:00
std::vector<std::pair<Entity, CollisionEvent>>
2023-05-01 12:55:49 +00:00
collision_infos_{}; // target entity, event info
};
} // namespace engine
#endif