From 04d020fcd67dfc81ee8d8293fc9acf488cf72504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 28 Apr 2012 00:52:57 +0200 Subject: [PATCH] Physics: Sphere % LineSegment collision. --- src/Physics/Sphere.cpp | 7 +++++++ src/Physics/Sphere.h | 5 +++++ src/Physics/Test/SphereTest.cpp | 13 +++++++++++++ src/Physics/Test/SphereTest.h | 1 + 4 files changed, 26 insertions(+) diff --git a/src/Physics/Sphere.cpp b/src/Physics/Sphere.cpp index a96adf662..106ff3c22 100644 --- a/src/Physics/Sphere.cpp +++ b/src/Physics/Sphere.cpp @@ -32,6 +32,8 @@ bool Sphere::collides(const AbstractShape* other) const { return *this % *static_cast(other); if(other->type() == Type::Line) return *this % *static_cast(other); + if(other->type() == Type::LineSegment) + return *this % *static_cast(other); if(other->type() == Type::Sphere) return *this % *static_cast(other); @@ -48,6 +50,11 @@ bool Sphere::operator%(const Line& other) const { Math::pow<2>(transformedRadius()); } +bool Sphere::operator%(const LineSegment& other) const { + return Distance::lineSegmentPointSquared(other.transformedA(), other.transformedB(), transformedPosition()) < + Math::pow<2>(transformedRadius()); +} + bool Sphere::operator%(const Sphere& other) const { return (other.transformedPosition()-transformedPosition()).lengthSquared() < Math::pow<2>(transformedRadius()+other.transformedRadius()); diff --git a/src/Physics/Sphere.h b/src/Physics/Sphere.h index 11d8a0f0b..0acd8f627 100644 --- a/src/Physics/Sphere.h +++ b/src/Physics/Sphere.h @@ -22,6 +22,7 @@ #include "AbstractShape.h" #include "Point.h" #include "Line.h" +#include "LineSegment.h" namespace Magnum { namespace Physics { @@ -68,6 +69,9 @@ class PHYSICS_EXPORT Sphere: public AbstractShape { /** @brief Collision with line */ bool operator%(const Line& other) const; + /** @brief Collision with line segment */ + bool operator%(const LineSegment& other) const; + /** @brief Collision with sphere */ bool operator%(const Sphere& other) const; @@ -82,6 +86,7 @@ class PHYSICS_EXPORT Sphere: public AbstractShape { #ifndef DOXYGEN_GENERATING_OUTPUT inline bool operator%(const Point& a, const Sphere& b) { return b % a; } inline bool operator%(const Line& a, const Sphere& b) { return b % a; } +inline bool operator%(const LineSegment& a, const Sphere& b) { return b % a; } #endif }} diff --git a/src/Physics/Test/SphereTest.cpp b/src/Physics/Test/SphereTest.cpp index 710c41b83..77bb70d6c 100644 --- a/src/Physics/Test/SphereTest.cpp +++ b/src/Physics/Test/SphereTest.cpp @@ -67,6 +67,19 @@ void SphereTest::collisionLine() { VERIFY_NOT_COLLIDES(sphere, line2); } +void SphereTest::collisionLineSegment() { + Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f); + Physics::LineSegment line({1.0f, 2.0f, 4.9f}, {1.0f, 2.0f, 7.0f}); + Physics::LineSegment line2({1.0f, 2.0f, 5.1f}, {1.0f, 2.0f, 7.0f}); + + randomTransformation(sphere); + randomTransformation(line); + randomTransformation(line2); + + VERIFY_COLLIDES(sphere, line); + VERIFY_NOT_COLLIDES(sphere, line2); +} + void SphereTest::collisionSphere() { Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f); Physics::Sphere sphere1({1.0f, 3.0f, 5.0f}, 1.0f); diff --git a/src/Physics/Test/SphereTest.h b/src/Physics/Test/SphereTest.h index d503ede2d..b71a08a09 100644 --- a/src/Physics/Test/SphereTest.h +++ b/src/Physics/Test/SphereTest.h @@ -26,6 +26,7 @@ class SphereTest: public AbstractShapeTest { void applyTransformation(); void collisionPoint(); void collisionLine(); + void collisionLineSegment(); void collisionSphere(); };