From ac692aa8207fb2dcc059e7761098cd98749154a0 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 19 Dec 2024 18:37:21 +0100 Subject: added some comments --- src/crepe/system/CollisionSystem.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/crepe') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index a8e6181..c9c5c58 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -156,6 +156,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self,CollisionInterna vec2 resolution; switch (type) { case CollisionInternalType::BOX_BOX: { + // Box-Box collision detection const BoxColliderInternal BOX1 = { .collider = std::get>(self.collider), .transform = self.info.transform, @@ -166,12 +167,15 @@ bool CollisionSystem::detect_collision(CollisionInternal & self,CollisionInterna .transform = other.info.transform, .rigidbody = other.info.rigidbody }; + // Get resolution vector from box-box collision detection resolution = this->get_box_box_detection(BOX1, BOX2); + // If no collision (NaN values), return false if(std::isnan(resolution.x) && std::isnan(resolution.y)) return false; break; } case CollisionInternalType::BOX_CIRCLE: { + // Box-Circle collision detection const BoxColliderInternal BOX1 = { .collider = std::get>(self.collider), .transform = self.info.transform, @@ -182,12 +186,16 @@ bool CollisionSystem::detect_collision(CollisionInternal & self,CollisionInterna .transform = other.info.transform, .rigidbody = other.info.rigidbody }; + // Get resolution vector from box-circle collision detection resolution = this->get_box_circle_detection(BOX1, CIRCLE2); + // If no collision (NaN values), return false if(std::isnan(resolution.x) && std::isnan(resolution.y)) return false; + // Invert the resolution vector for proper collision response resolution = -resolution; break; } case CollisionInternalType::CIRCLE_CIRCLE: { + // Circle-Circle collision detection const CircleColliderInternal CIRCLE1 = { .collider = std::get>(self.collider), .transform = self.info.transform, @@ -198,11 +206,14 @@ bool CollisionSystem::detect_collision(CollisionInternal & self,CollisionInterna .transform = other.info.transform, .rigidbody = other.info.rigidbody }; + // Get resolution vector from circle-circle collision detection resolution = this->get_circle_circle_detection(CIRCLE1,CIRCLE2); + // If no collision (NaN values), return false if(std::isnan(resolution.x) && std::isnan(resolution.y)) return false; break; } case CollisionInternalType::CIRCLE_BOX: { + // Circle-Box collision detection const CircleColliderInternal CIRCLE1 = { .collider = std::get>(self.collider), .transform = self.info.transform, @@ -213,17 +224,26 @@ bool CollisionSystem::detect_collision(CollisionInternal & self,CollisionInterna .transform = other.info.transform, .rigidbody = other.info.rigidbody }; + // Get resolution vector from box-circle collision detection (order swapped) resolution = this->get_box_circle_detection(BOX2, CIRCLE1); + // If no collision (NaN values), return false if(std::isnan(resolution.x) && std::isnan(resolution.y)) return false; break; } case CollisionInternalType::NONE: - break; + // No collision detection needed if the type is NONE + return false; + break; } + // Store the calculated resolution vector for the 'self' collider self.resolution = resolution; + // Calculate the resolution direction based on the rigidbody data self.resolution_direction = this->resolution_correction(self.resolution, self.info.rigidbody.data); + // For the 'other' collider, the resolution is the opposite direction of 'self' other.resolution = -self.resolution; other.resolution_direction = self.resolution_direction; + + // Return true if a collision was detected and resolution was calculated return true; } -- cgit v1.2.3