From c7c137752c787461fb6fe3e8f05701582ef99ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 28 Apr 2012 13:24:07 +0200 Subject: [PATCH] Physics: Capsule % Sphere collision. --- src/Physics/Capsule.cpp | 7 +++++++ src/Physics/Capsule.h | 5 +++++ src/Physics/Test/CapsuleTest.cpp | 16 ++++++++++++++++ src/Physics/Test/CapsuleTest.h | 1 + 4 files changed, 29 insertions(+) diff --git a/src/Physics/Capsule.cpp b/src/Physics/Capsule.cpp index 85d8203c2..f6262222b 100644 --- a/src/Physics/Capsule.cpp +++ b/src/Physics/Capsule.cpp @@ -31,6 +31,8 @@ void Capsule::applyTransformation(const Matrix4& transformation) { bool Capsule::collides(const AbstractShape* other) const { if(other->type() == Type::Point) return *this % *static_cast(other); + if(other->type() == Type::Sphere) + return *this % *static_cast(other); return AbstractShape::collides(other); } @@ -40,4 +42,9 @@ bool Capsule::operator%(const Point& other) const { Math::pow<2>(transformedRadius()); } +bool Capsule::operator%(const Sphere& other) const { + return Distance::lineSegmentPointSquared(transformedA(), transformedB(), other.transformedPosition()) < + Math::pow<2>(transformedRadius()+other.transformedRadius()); +} + }} diff --git a/src/Physics/Capsule.h b/src/Physics/Capsule.h index 0cc953fad..e1e2a552b 100644 --- a/src/Physics/Capsule.h +++ b/src/Physics/Capsule.h @@ -21,6 +21,7 @@ #include "AbstractShape.h" #include "Point.h" +#include "Sphere.h" namespace Magnum { namespace Physics { @@ -65,6 +66,9 @@ class PHYSICS_EXPORT Capsule: public AbstractShape { /** @brief Collision with point */ bool operator%(const Point& other) const; + /** @brief Collision with sphere */ + bool operator%(const Sphere& other) const; + protected: inline Type type() const { return Type::Capsule; } @@ -76,6 +80,7 @@ class PHYSICS_EXPORT Capsule: public AbstractShape { #ifndef DOXYGEN_GENERATING_OUTPUT inline bool operator%(const Point& a, const Capsule& b) { return b % a; } +inline bool operator%(const Sphere& a, const Capsule& b) { return b % a; } #endif }} diff --git a/src/Physics/Test/CapsuleTest.cpp b/src/Physics/Test/CapsuleTest.cpp index cae3675e0..b1a39ac59 100644 --- a/src/Physics/Test/CapsuleTest.cpp +++ b/src/Physics/Test/CapsuleTest.cpp @@ -52,4 +52,20 @@ void CapsuleTest::collisionPoint() { VERIFY_NOT_COLLIDES(capsule, point2); } +void CapsuleTest::collisionSphere() { + Physics::Capsule capsule({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f); + Physics::Sphere sphere({3.0f, 0.0f, 0.0f}, 0.9f); + Physics::Sphere sphere1({3.5f, 1.0f, 0.0f}, 0.6f); + Physics::Sphere sphere2({1.0f, 4.1f, 0.0f}, 1.0f); + + randomTransformation(capsule); + randomTransformation(sphere); + randomTransformation(sphere1); + randomTransformation(sphere2); + + VERIFY_COLLIDES(capsule, sphere); + VERIFY_COLLIDES(capsule, sphere1); + VERIFY_NOT_COLLIDES(capsule, sphere2); +} + }}} diff --git a/src/Physics/Test/CapsuleTest.h b/src/Physics/Test/CapsuleTest.h index 69e50de47..2b5e85e65 100644 --- a/src/Physics/Test/CapsuleTest.h +++ b/src/Physics/Test/CapsuleTest.h @@ -25,6 +25,7 @@ class CapsuleTest: public AbstractShapeTest { private slots: void applyTransformation(); void collisionPoint(); + void collisionSphere(); }; }}}