diff options
-rw-r--r-- | src/crepe/api/Vector2.h | 3 | ||||
-rw-r--r-- | src/crepe/api/Vector2.hpp | 5 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 28 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 |
4 files changed, 20 insertions, 17 deletions
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index bf9d124..52e1bb6 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -90,6 +90,9 @@ struct Vector2 { //! Returns the perpendicular vector to this vector. Vector2 perpendicular() const; + + //! Checks if both components of the vector are NaN. + bool is_nan() const; }; } // namespace crepe diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index ff53cb0..a470595 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -163,4 +163,9 @@ Vector2<T> Vector2<T>::perpendicular() const { return {-y, x}; } +template <class T> +bool Vector2<T>::is_nan() const { + return std::isnan(x) && std::isnan(y); +} + } // namespace crepe diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index f0f1fa8..0b9ea61 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -168,7 +168,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // 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; + if(resolution.is_nan()) return false; break; } case CollisionInternalType::BOX_CIRCLE: { @@ -184,7 +184,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // 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; + if(resolution.is_nan()) return false; // Invert the resolution vector for proper collision response resolution = -resolution; break; @@ -202,7 +202,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // 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; + if(resolution.is_nan()) return false; break; } case CollisionInternalType::CIRCLE_BOX: { @@ -218,7 +218,7 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern // 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; + if(resolution.is_nan()) return false; break; } case CollisionInternalType::NONE: @@ -239,9 +239,8 @@ bool CollisionSystem::detect_collision(CollisionInternal & self, CollisionIntern return true; } -vec2 CollisionSystem::get_box_box_detection(const BoxColliderInternal & box1, - const BoxColliderInternal & box2) const { - vec2 resolution{std::nanf(""), std::nanf("")}; +vec2 CollisionSystem::get_box_box_detection(const BoxColliderInternal & box1, const BoxColliderInternal & box2) const { + vec2 resolution{NAN, NAN}; // Get current positions of colliders vec2 pos1 = AbsolutePosition::get_position(box1.transform, box1.collider.offset); vec2 pos2 = AbsolutePosition::get_position(box2.transform, box2.collider.offset); @@ -320,14 +319,11 @@ vec2 CollisionSystem::get_box_circle_detection(const BoxColliderInternal & box, = std::sqrt(closest_delta.x * closest_delta.x + closest_delta.y * closest_delta.y); vec2 collision_normal = closest_delta / distance; - // Compute penetration depth - float penetration_depth = scaled_circle_radius - distance; - - // Compute the resolution vector - return vec2{collision_normal * penetration_depth}; - } - // No collision - return vec2{std::nanf(""), std::nanf("")}; + // Compute the resolution vector + return vec2{collision_normal * penetration_depth}; + } + // No collision + return vec2{NAN, NAN};; } vec2 CollisionSystem::get_circle_circle_detection( @@ -371,7 +367,7 @@ vec2 CollisionSystem::get_circle_circle_detection( return resolution; } // No collision - return vec2{std::nanf(""), std::nanf("")}; + return vec2{NAN, NAN};; } CollisionSystem::Direction diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 2ee3051..f62414e 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -22,4 +22,3 @@ add_example(button) add_example(loadfont) add_example(FontExample) add_example(AITest) -add_example(collisiontest) |