From 355b0178eaaf3602b00975adb8f56e2141dcd982 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 14 Nov 2024 22:39:08 +0100 Subject: added function to call collision handlers --- src/crepe/system/CollisionSystem.cpp | 102 +++++++++++++++++++++-------------- src/crepe/system/CollisionSystem.h | 6 ++- 2 files changed, 66 insertions(+), 42 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index a9d05ad..ac35026 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -38,39 +38,47 @@ void CollisionSystem::update() { } void CollisionSystem::call_collision_handler(const CollidedInfoStor& data1,const CollidedInfoStor& data2){ + + // Check collision type and get values for handler 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(); - + if (std::holds_alternative(data1.collider)) { + if (std::holds_alternative(data2.collider)) { + const BoxCollider& box_collider1 = std::get(data1.collider); + const BoxCollider& box_collider2 = std::get(data2.collider); + first = box_collider1.game_object_id; + second = box_collider2.game_object_id; + } + else { + const BoxCollider& box_collider = std::get(data1.collider); + const CircleCollider& circle_collider = std::get(data2.collider); + first = box_collider.game_object_id; + second = circle_collider.game_object_id; + } + } + else { + if (std::holds_alternative(data2.collider)) { + const CircleCollider& circle_collider1 = std::get(data1.collider); + const CircleCollider& circle_collider2 = std::get(data2.collider); + first = circle_collider1.game_object_id; + second = circle_collider2.game_object_id; + } + else { + const CircleCollider& circle_collider = std::get(data1.collider); + const BoxCollider& box_collider = std::get(data2.collider); + first = circle_collider.game_object_id; + second = box_collider.game_object_id; + } + } + + // check rigidbody type + if(data1.rigidbody.data.body_type != Rigidbody::BodyType::STATIC) + { + // If second body is static move back + if(data2.rigidbody.data.body_type == Rigidbody::BodyType::STATIC) return; + //call static handler (is bounce true?) + + // call script handler + } } std::vector> CollisionSystem::check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders) { @@ -89,8 +97,10 @@ std::vector(game_object_id_1).front().get(); + if(!transform1.active) continue; Rigidbody& rigidbody1 = mgr.get_components_by_id(game_object_id_1).front().get(); - + if(!rigidbody1.active) continue; + // Check CircleCollider vs CircleCollider for (size_t j = i + 1; j < boxcolliders.size(); ++j) { if(!boxcolliders[j].get().active) continue; @@ -100,13 +110,15 @@ std::vector(boxcolliders[j].get().game_object_id).front().get(); + if(!transform2.active) continue; Rigidbody & rigidbody2 = mgr.get_components_by_id(boxcolliders[j].get().game_object_id).front().get(); - + if(!rigidbody2.active) continue; // Check collision if (check_box_box_collision(boxcolliders[i], boxcolliders[j], transform1, transform2, rigidbody1, rigidbody2)) { collisions_ret.emplace_back(std::make_pair( - std::make_tuple(boxcolliders[i], transform1,rigidbody1), - std::make_tuple(boxcolliders[j], transform2,rigidbody2))); + CollidedInfoStor{boxcolliders[i], transform1, rigidbody1}, + CollidedInfoStor{boxcolliders[j], transform2, rigidbody2} + )); } } @@ -119,13 +131,17 @@ std::vector(circlecolliders[j].get().game_object_id).front().get(); + if(!transform2.active) continue; Rigidbody & rigidbody2 = mgr.get_components_by_id(circlecolliders[j].get().game_object_id).front().get(); + if(!rigidbody2.active) continue; // Check collision if (check_box_circle_collision(boxcolliders[i], circlecolliders[j], transform1, transform2, rigidbody1, rigidbody2)) { + collisions_ret.emplace_back(std::make_pair( - std::make_tuple(boxcolliders[i], transform1,rigidbody1), - std::make_tuple(circlecolliders[j], transform2,rigidbody2))); + CollidedInfoStor{boxcolliders[i], transform1, rigidbody1}, + CollidedInfoStor{circlecolliders[j], transform2, rigidbody2} + )); } } } @@ -135,7 +151,10 @@ std::vector(circlecolliders[i].get().game_object_id).front().get(); + if(!transform1.active) continue; Rigidbody & rigidbody1 = mgr.get_components_by_id(circlecolliders[i].get().game_object_id).front().get(); + if(!rigidbody1.active) continue; + for (size_t j = i + 1; j < circlecolliders.size(); ++j) { if(!circlecolliders[j].get().active) continue; // Skip self collision @@ -144,13 +163,16 @@ std::vector(circlecolliders[j].get().game_object_id).front().get(); + if(!transform2.active) continue; Rigidbody & rigidbody2 = mgr.get_components_by_id(circlecolliders[j].get().game_object_id).front().get(); + if(!rigidbody2.active) continue; // Check collision if (check_circle_circle_collision(circlecolliders[i], circlecolliders[j], transform1, transform2, rigidbody1, rigidbody2)) { collisions_ret.emplace_back(std::make_pair( - std::make_tuple(circlecolliders[i], transform1,rigidbody1), - std::make_tuple(circlecolliders[j], transform2,rigidbody2))); + CollidedInfoStor{circlecolliders[i], transform1, rigidbody1}, + CollidedInfoStor{circlecolliders[j], transform2, rigidbody2} + )); } } } diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 4970579..9a72948 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -17,8 +17,10 @@ private: using collider_stor = std::variant; private: struct CollidedInfoStor { - std::tuple collider_info; -}; + collider_stor collider; // Store either BoxCollider or CircleCollider + Transform transform; // Transform data + Rigidbody rigidbody; // Rigidbody data + }; public: CollisionSystem(); void update(); -- cgit v1.2.3