From b58765b671969aa810b9a345be55423523e893fb Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 14 Nov 2024 22:22:49 +0100 Subject: saved reference in storage struct to send to handeler --- src/crepe/system/CollisionSystem.cpp | 78 ++++++++++++++++++++++++------------ src/crepe/system/CollisionSystem.h | 22 +++++----- 2 files changed, 65 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index e6172b7..a9d05ad 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include "CollisionSystem.h" @@ -23,39 +25,57 @@ void CollisionSystem::update() { ComponentManager & mgr = ComponentManager::get_instance(); std::vector> boxcolliders = mgr.get_components_by_type(); std::vector> circlecolliders = mgr.get_components_by_type(); - std::vector> collided = check_collisions(boxcolliders,circlecolliders); + std::vector> collided = check_collisions(boxcolliders,circlecolliders); std::cout << "DEBUG INFO" << std::endl; - for (const std::pair& collision : collided) { - - if (const BoxCollider* box_collider1 = std::get_if(&collision.first)) { - std::cout << "Processing a BoxCollider\n"; - if (const BoxCollider* box_collider2 = std::get_if(&collision.second)) { - std::cout << "Processing a BoxCollider\n"; - } else if (const CircleCollider* circle_collider2 = std::get_if(&collision.first)) { - std::cout << "Processing a CircleCollider\n"; - } - } else if (const CircleCollider* circle_collider1 = std::get_if(&collision.first)) { - std::cout << "Processing a CircleCollider\n"; - if (const BoxCollider* box_collider2 = std::get_if(&collision.first)) { - std::cout << "Processing a BoxCollider\n"; - } else if (const CircleCollider* circle_collider2 = std::get_if(&collision.first)) { - std::cout << "Processing a CircleCollider\n"; - } - } - + for (const auto& collision_pair : collided) { + call_collision_handler(collision_pair.first,collision_pair.second); // First collider + call_collision_handler(collision_pair.second,collision_pair.first); // First collider } + if(collided.empty()) { std::cout << "No objects collided" << std::endl; } } -void CollisionSystem::call_collision_handler(const Rigidbody& rigidbody1,const Rigidbody& rigidbody2){ - +void CollisionSystem::call_collision_handler(const CollidedInfoStor& data1,const CollidedInfoStor& data2){ + game_object_id_t first = 0,second = 0; + // if (std::holds_alternative(collider1)) { + // if (std::holds_alternative(collider2)) { + // const BoxCollider& box_collider1 = std::get(collider1); + // const BoxCollider& box_collider2 = std::get(collider2); + // first = box_collider1.game_object_id; + // second = box_collider2.game_object_id; + // } + // else { + // const BoxCollider& box_collider = std::get(collider1); + // const CircleCollider& circle_collider = std::get(collider2); + // first = box_collider.game_object_id; + // second = circle_collider.game_object_id; + // } + // } + // else { + // if (std::holds_alternative(collider2)) { + // const CircleCollider& circle_collider1 = std::get(collider1); + // const CircleCollider& circle_collider2 = std::get(collider2); + // first = circle_collider1.game_object_id; + // second = circle_collider2.game_object_id; + // } + // else { + // const CircleCollider& circle_collider = std::get(collider1); + // const BoxCollider& box_collider = std::get(collider2); + // first = circle_collider.game_object_id; + // second = box_collider.game_object_id; + // } + // } + + // Rigidbody rigidbody1 = mgr.get_components_by_id(first).front().get(); + // Rigidbody rigidbody2 = mgr.get_components_by_id(second).front().get(); + } -std::vector> CollisionSystem::check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders) { +std::vector> CollisionSystem::check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders) { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> collisions_ret; + std::vector> collisions_ret; //if no colliders skip //check if colliders has rigibocdy if not skip @@ -84,7 +104,9 @@ std::vector #include #include namespace crepe { -class Collider; -class BoxCollider; -class CircleCollider; -class Transform; -class Rigidbody; class CollisionSystem { +private: + using collider_stor = std::variant; +private: + struct CollidedInfoStor { + std::tuple collider_info; +}; public: CollisionSystem(); void update(); private: - using collider_stor = std::variant; -private: - void call_collision_handler(const Rigidbody& rigidbody1,const Rigidbody& rigidbody2); - std::vector> check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders); + void call_collision_handler(const CollidedInfoStor& data1,const CollidedInfoStor& data2); + std::vector> check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders); bool check_box_box_collision(const BoxCollider& box1, const BoxCollider& box2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); bool check_box_circle_collision(const BoxCollider& box1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); bool check_circle_circle_collision(const CircleCollider& circle1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); -- cgit v1.2.3