diff --git a/src/Physics/Sphere.cpp b/src/Physics/Sphere.cpp index 4d969c6af..a96adf662 100644 --- a/src/Physics/Sphere.cpp +++ b/src/Physics/Sphere.cpp @@ -15,6 +15,10 @@ #include "Sphere.h" +#include "Math/Geometry/Distance.h" + +using namespace Magnum::Math::Geometry; + namespace Magnum { namespace Physics { void Sphere::applyTransformation(const Matrix4& transformation) { @@ -26,6 +30,8 @@ void Sphere::applyTransformation(const Matrix4& transformation) { bool Sphere::collides(const AbstractShape* other) const { if(other->type() == Type::Point) return *this % *static_cast(other); + if(other->type() == Type::Line) + return *this % *static_cast(other); if(other->type() == Type::Sphere) return *this % *static_cast(other); @@ -37,6 +43,11 @@ bool Sphere::operator%(const Point& other) const { Math::pow<2>(transformedRadius()); } +bool Sphere::operator%(const Line& other) const { + return Distance::linePointSquared(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 b3ababd51..11d8a0f0b 100644 --- a/src/Physics/Sphere.h +++ b/src/Physics/Sphere.h @@ -21,6 +21,7 @@ #include "AbstractShape.h" #include "Point.h" +#include "Line.h" namespace Magnum { namespace Physics { @@ -64,6 +65,9 @@ class PHYSICS_EXPORT Sphere: public AbstractShape { /** @brief Collision with point */ bool operator%(const Point& other) const; + /** @brief Collision with line */ + bool operator%(const Line& other) const; + /** @brief Collision with sphere */ bool operator%(const Sphere& other) const; @@ -77,6 +81,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; } #endif }} diff --git a/src/Physics/Test/SphereTest.cpp b/src/Physics/Test/SphereTest.cpp index e118df0bc..710c41b83 100644 --- a/src/Physics/Test/SphereTest.cpp +++ b/src/Physics/Test/SphereTest.cpp @@ -54,6 +54,19 @@ void SphereTest::collisionPoint() { VERIFY_NOT_COLLIDES(sphere, point2); } +void SphereTest::collisionLine() { + Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f); + Physics::Line line({1.0f, 1.5f, 3.5f}, {1.0f, 2.5f, 2.5f}); + Physics::Line line2({1.0f, 2.0f, 5.1f}, {1.0f, 3.0f, 5.1f}); + + 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 439a54560..d503ede2d 100644 --- a/src/Physics/Test/SphereTest.h +++ b/src/Physics/Test/SphereTest.h @@ -25,6 +25,7 @@ class SphereTest: public AbstractShapeTest { private slots: void applyTransformation(); void collisionPoint(); + void collisionLine(); void collisionSphere(); };