aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/CollisionSystem.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system/CollisionSystem.h')
-rw-r--r--src/crepe/system/CollisionSystem.h121
1 files changed, 72 insertions, 49 deletions
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
index 85ae5ad..6b5a4bb 100644
--- a/src/crepe/system/CollisionSystem.h
+++ b/src/crepe/system/CollisionSystem.h
@@ -1,38 +1,37 @@
#pragma once
-#include <vector>
-#include <variant>
#include <optional>
+#include <variant>
+#include <vector>
-#include "api/Rigidbody.h"
-#include "api/Transform.h"
#include "api/BoxCollider.h"
#include "api/CircleCollider.h"
#include "api/Metadata.h"
+#include "api/Rigidbody.h"
+#include "api/Transform.h"
#include "api/Vector2.h"
#include "Collider.h"
#include "System.h"
-
namespace crepe {
-
//! A system responsible for detecting and handling collisions between colliders.
class CollisionSystem : public System {
public:
using System::System;
+
private:
-
//! A variant type that can hold either a BoxCollider or a CircleCollider.
- using collider_variant = std::variant<std::reference_wrapper<BoxCollider>, std::reference_wrapper<CircleCollider>>;
+ using collider_variant = std::variant<std::reference_wrapper<BoxCollider>,
+ std::reference_wrapper<CircleCollider>>;
//! Enum representing the types of collider pairs for collision detection.
enum class CollisionInternalType {
- BOX_BOX,
- CIRCLE_CIRCLE,
- BOX_CIRCLE,
- CIRCLE_BOX,
+ BOX_BOX,
+ CIRCLE_CIRCLE,
+ BOX_CIRCLE,
+ CIRCLE_BOX,
};
/**
@@ -45,21 +44,21 @@ private:
*/
struct CollisionInternal {
game_object_id_t id = 0;
- collider_variant collider;
- Transform& transform;
- Rigidbody& rigidbody;
+ collider_variant collider;
+ Transform & transform;
+ Rigidbody & rigidbody;
};
-
+
//! Enum representing movement directions during collision resolution.
enum class Direction {
//! No movement required.
- NONE,
+ NONE,
//! Movement in the X direction.
- X_DIRECTION,
+ X_DIRECTION,
//! Movement in the Y direction.
- Y_DIRECTION,
+ Y_DIRECTION,
//! Movement in both X and Y directions.
- BOTH
+ BOTH
};
public:
@@ -68,15 +67,15 @@ public:
*
* Includes information about the colliding objects and the resolution data for handling the collision.
*/
- struct CollisionInfo{
- Collider& this_collider;
- Transform& this_transform;
- Rigidbody& this_rigidbody;
- Metadata& this_metadata;
- Collider& other_collider;
- Transform& other_transform;
- Rigidbody& other_rigidbody;
- Metadata& other_metadata;
+ struct CollisionInfo {
+ Collider & this_collider;
+ Transform & this_transform;
+ Rigidbody & this_rigidbody;
+ Metadata & this_metadata;
+ Collider & other_collider;
+ Transform & other_transform;
+ Rigidbody & other_rigidbody;
+ Metadata & other_metadata;
//! The resolution vector for the collision.
vec2 resolution;
//! The direction of movement for resolving the collision.
@@ -84,11 +83,10 @@ public:
};
public:
-
//! Updates the collision system by checking for collisions between colliders and handling them.
void update() override;
-private:
+private:
/**
* \brief Determines the type of collider pair from two colliders.
*
@@ -98,7 +96,8 @@ private:
* \param collider2 Second collider variant (BoxCollider or CircleCollider).
* \return The combined type of the two colliders.
*/
- CollisionInternalType get_collider_type(const collider_variant& collider1,const collider_variant& collider2) const;
+ CollisionInternalType get_collider_type(const collider_variant & collider1,
+ const collider_variant & collider2) const;
/**
* \brief Calculates the current position of a collider.
@@ -110,10 +109,10 @@ private:
* \param rigidbody The Rigidbody of the associated game object.
* \return The calculated position of the collider.
*/
- vec2 get_current_position(const vec2& collider_offset, const Transform& transform, const Rigidbody& rigidbody) const;
+ vec2 get_current_position(const vec2 & collider_offset, const Transform & transform,
+ const Rigidbody & rigidbody) const;
private:
-
/**
* \brief Handles collision resolution between two colliders.
*
@@ -122,7 +121,7 @@ private:
* \param data1 Collision data for the first collider.
* \param data2 Collision data for the second collider.
*/
- void collision_handler_request(CollisionInternal& data1,CollisionInternal& data2);
+ void collision_handler_request(CollisionInternal & data1, CollisionInternal & data2);
/**
* \brief Resolves collision between two colliders and calculates the movement required.
@@ -134,7 +133,9 @@ private:
* \param type The type of collider pair.
* \return A pair containing the resolution vector and direction for the first collider.
*/
- std::pair<vec2,Direction> collision_handler(CollisionInternal& data1,CollisionInternal& data2 ,CollisionInternalType type);
+ std::pair<vec2, Direction> collision_handler(CollisionInternal & data1,
+ CollisionInternal & data2,
+ CollisionInternalType type);
/**
* \brief Calculates the resolution vector for two BoxColliders.
@@ -147,7 +148,9 @@ private:
* \param position2 The position of the second BoxCollider.
* \return The resolution vector for the collision.
*/
- vec2 get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,const vec2& position1,const vec2& position2) const;
+ vec2 get_box_box_resolution(const BoxCollider & box_collider1,
+ const BoxCollider & box_collider2, const vec2 & position1,
+ const vec2 & position2) const;
/**
* \brief Calculates the resolution vector for two CircleCollider.
@@ -160,7 +163,10 @@ private:
* \param position2 The position of the second CircleCollider.
* \return The resolution vector for the collision.
*/
- vec2 get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, const vec2& final_position1, const vec2& final_position2) const;
+ vec2 get_circle_circle_resolution(const CircleCollider & circle_collider1,
+ const CircleCollider & circle_collider2,
+ const vec2 & final_position1,
+ const vec2 & final_position2) const;
/**
* \brief Calculates the resolution vector for two CircleCollider.
@@ -173,7 +179,10 @@ private:
* \param box_position The position of the BocCollider.
* \return The resolution vector for the collision.
*/
- vec2 get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, const vec2& circle_position, const vec2& box_position) const;
+ vec2 get_circle_box_resolution(const CircleCollider & circle_collider,
+ const BoxCollider & box_collider,
+ const vec2 & circle_position,
+ const vec2 & box_position) const;
/**
* \brief Determines the appropriate collision handler for a collision.
@@ -182,7 +191,7 @@ private:
*
* \param info Collision information containing data about both colliders.
*/
- void determine_collision_handler(CollisionInfo& info);
+ void determine_collision_handler(CollisionInfo & info);
/**
* \brief Handles collisions involving static objects.
@@ -191,9 +200,9 @@ private:
*
* \param info Collision information containing data about both colliders.
*/
- void static_collision_handler(CollisionInfo& info);
+ void static_collision_handler(CollisionInfo & info);
+
private:
-
/**
* \brief Checks for collisions between colliders.
*
@@ -202,7 +211,8 @@ private:
* \param colliders A collection of all active colliders.
* \return A list of collision pairs with their associated data.
*/
- std::vector<std::pair<CollisionInternal,CollisionInternal>> gather_collisions(std::vector<CollisionInternal> & colliders);
+ std::vector<std::pair<CollisionInternal, CollisionInternal>>
+ gather_collisions(std::vector<CollisionInternal> & colliders);
/**
* \brief Checks for collision between two colliders.
@@ -214,8 +224,10 @@ private:
* \param type The type of collider pair.
* \return True if a collision is detected, otherwise false.
*/
- bool get_collision(const CollisionInternal& first_info,const CollisionInternal& second_info, CollisionInternalType type) const;
-
+ bool get_collision(const CollisionInternal & first_info,
+ const CollisionInternal & second_info,
+ CollisionInternalType type) const;
+
/**
* \brief Detects collisions between two BoxColliders.
*
@@ -227,8 +239,11 @@ private:
* \param rigidbody2 Rigidbody of the second object.
* \return True if a collision is detected, otherwise false.
*/
- bool get_box_box_collision(const BoxCollider& box1, const BoxCollider& box2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2) const;
-
+ bool get_box_box_collision(const BoxCollider & box1, const BoxCollider & box2,
+ const Transform & transform1, const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const;
+
/**
* \brief Check collision for box on circle collider
*
@@ -240,7 +255,10 @@ private:
* \param rigidbody2 Rigidbody of the second object.
* \return True if a collision is detected, otherwise false.
*/
- bool get_box_circle_collision(const BoxCollider& box1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2) const;
+ bool get_box_circle_collision(const BoxCollider & box1, const CircleCollider & circle2,
+ const Transform & transform1, const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const;
/**
* \brief Check collision for circle on circle collider
@@ -255,7 +273,12 @@ private:
*
* \return status of collision
*/
- bool get_circle_circle_collision(const CircleCollider& circle1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2) const;
+ bool get_circle_circle_collision(const CircleCollider & circle1,
+ const CircleCollider & circle2,
+ const Transform & transform1,
+ const Transform & transform2,
+ const Rigidbody & rigidbody1,
+ const Rigidbody & rigidbody2) const;
};
} // namespace crepe