From 42bd89a76e7e320c086be11a6aab10248a3d7c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 28 Apr 2012 12:15:30 +0200 Subject: [PATCH] Physics: Capsule % Point collision. --- src/Physics/Capsule.cpp | 16 ++++++++++++++++ src/Physics/Capsule.h | 10 ++++++++++ src/Physics/Test/CapsuleTest.cpp | 16 ++++++++++++++++ src/Physics/Test/CapsuleTest.h | 5 +++-- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Physics/Capsule.cpp b/src/Physics/Capsule.cpp index ab09f3bc3..85d8203c2 100644 --- a/src/Physics/Capsule.cpp +++ b/src/Physics/Capsule.cpp @@ -15,6 +15,10 @@ #include "Capsule.h" +#include "Math/Geometry/Distance.h" + +using namespace Magnum::Math::Geometry; + namespace Magnum { namespace Physics { void Capsule::applyTransformation(const Matrix4& transformation) { @@ -24,4 +28,16 @@ void Capsule::applyTransformation(const Matrix4& transformation) { _transformedRadius = scaling*_radius; } +bool Capsule::collides(const AbstractShape* other) const { + if(other->type() == Type::Point) + return *this % *static_cast(other); + + return AbstractShape::collides(other); +} + +bool Capsule::operator%(const Point& other) const { + return Distance::lineSegmentPointSquared(transformedA(), transformedB(), other.transformedPosition()) < + Math::pow<2>(transformedRadius()); +} + }} diff --git a/src/Physics/Capsule.h b/src/Physics/Capsule.h index 9578edc65..0cc953fad 100644 --- a/src/Physics/Capsule.h +++ b/src/Physics/Capsule.h @@ -20,6 +20,7 @@ */ #include "AbstractShape.h" +#include "Point.h" namespace Magnum { namespace Physics { @@ -36,6 +37,8 @@ class PHYSICS_EXPORT Capsule: public AbstractShape { void applyTransformation(const Matrix4& transformation); + bool collides(const AbstractShape* other) const; + inline Vector3 a() const { return _a; } /**< @brief Start point */ inline Vector3 b() const { return _a; } /**< @brief End point */ @@ -59,6 +62,9 @@ class PHYSICS_EXPORT Capsule: public AbstractShape { return _transformedRadius; } + /** @brief Collision with point */ + bool operator%(const Point& other) const; + protected: inline Type type() const { return Type::Capsule; } @@ -68,6 +74,10 @@ class PHYSICS_EXPORT Capsule: public AbstractShape { float _radius, _transformedRadius; }; +#ifndef DOXYGEN_GENERATING_OUTPUT +inline bool operator%(const Point& a, const Capsule& b) { return b % a; } +#endif + }} #endif diff --git a/src/Physics/Test/CapsuleTest.cpp b/src/Physics/Test/CapsuleTest.cpp index 6f15f3bd3..cae3675e0 100644 --- a/src/Physics/Test/CapsuleTest.cpp +++ b/src/Physics/Test/CapsuleTest.cpp @@ -36,4 +36,20 @@ void CapsuleTest::applyTransformation() { QCOMPARE(capsule.transformedRadius(), Math::Constants::Sqrt3*7.0f); } +void CapsuleTest::collisionPoint() { + Physics::Capsule capsule({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f); + Physics::Point point({2.0f, 0.0f, 0.0f}); + Physics::Point point1({2.9f, 1.0f, 0.0f}); + Physics::Point point2({1.0f, 3.1f, 0.0f}); + + randomTransformation(capsule); + randomTransformation(point); + randomTransformation(point1); + randomTransformation(point2); + + VERIFY_COLLIDES(capsule, point); + VERIFY_COLLIDES(capsule, point1); + VERIFY_NOT_COLLIDES(capsule, point2); +} + }}} diff --git a/src/Physics/Test/CapsuleTest.h b/src/Physics/Test/CapsuleTest.h index adbd04134..69e50de47 100644 --- a/src/Physics/Test/CapsuleTest.h +++ b/src/Physics/Test/CapsuleTest.h @@ -15,15 +15,16 @@ GNU Lesser General Public License version 3 for more details. */ -#include +#include "AbstractShapeTest.h" namespace Magnum { namespace Physics { namespace Test { -class CapsuleTest: public QObject { +class CapsuleTest: public AbstractShapeTest { Q_OBJECT private slots: void applyTransformation(); + void collisionPoint(); }; }}}