From bceef6b8ade2ab8341153e4bf18a5e01d27c90b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 28 Apr 2012 01:28:20 +0200 Subject: [PATCH] Physics: Plane % Line collision. --- src/Physics/Plane.cpp | 19 +++++++++++++++++++ src/Physics/Plane.h | 11 +++++++++++ src/Physics/Test/PlaneTest.cpp | 16 ++++++++++++++++ src/Physics/Test/PlaneTest.h | 5 +++-- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Physics/Plane.cpp b/src/Physics/Plane.cpp index 7301a79d3..a192fbe00 100644 --- a/src/Physics/Plane.cpp +++ b/src/Physics/Plane.cpp @@ -15,6 +15,13 @@ #include "Plane.h" +#include + +#include "Math/Geometry/Intersection.h" + +using namespace std; +using namespace Magnum::Math::Geometry; + namespace Magnum { namespace Physics { void Plane::applyTransformation(const Matrix4& transformation) { @@ -22,4 +29,16 @@ void Plane::applyTransformation(const Matrix4& transformation) { _transformedNormal = transformation.rotation()*_normal; } +bool Plane::collides(const AbstractShape* other) const { + if(other->type() == Type::Line) + return *this % *static_cast(other); + + return AbstractShape::collides(other); +} + +bool Plane::operator%(const Line& other) const { + float t = Intersection::planeLine(transformedPosition(), transformedNormal(), other.transformedA(), other.transformedB()); + return t != t || (t != numeric_limits::infinity() && t != -numeric_limits::infinity()); +} + }} diff --git a/src/Physics/Plane.h b/src/Physics/Plane.h index e7daa7b93..40c061be6 100644 --- a/src/Physics/Plane.h +++ b/src/Physics/Plane.h @@ -21,6 +21,8 @@ #include "AbstractShape.h" +#include "Line.h" + namespace Magnum { namespace Physics { /** @brief Infinite plane, defined by position and normal */ @@ -31,6 +33,8 @@ class PHYSICS_EXPORT Plane: public AbstractShape { void applyTransformation(const Matrix4& transformation); + bool collides(const AbstractShape* other) const; + /** @brief Position */ inline Vector3 position() const { return _position; } @@ -57,6 +61,9 @@ class PHYSICS_EXPORT Plane: public AbstractShape { return _transformedNormal; } + /** @brief Collision with line */ + bool operator%(const Line& other) const; + protected: inline Type type() const { return Type::Plane; } @@ -65,6 +72,10 @@ class PHYSICS_EXPORT Plane: public AbstractShape { _normal, _transformedNormal; }; +#ifndef DOXYGEN_GENERATING_OUTPUT +inline bool operator%(const Line& a, const Plane& b) { return b % a; } +#endif + }} #endif diff --git a/src/Physics/Test/PlaneTest.cpp b/src/Physics/Test/PlaneTest.cpp index 75907c2c1..1a65f72b2 100644 --- a/src/Physics/Test/PlaneTest.cpp +++ b/src/Physics/Test/PlaneTest.cpp @@ -36,4 +36,20 @@ void PlaneTest::applyTransformation() { QVERIFY(plane.transformedNormal() == Vector3(Math::Constants::Sqrt2, -Math::Constants::Sqrt2, 0)); } +void PlaneTest::collisionLine() { + Physics::Plane plane(Vector3(), Vector3::yAxis()); + Physics::Line line({0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}); + Physics::Line line2({0.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}); + Physics::Line line3({0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}); + + randomTransformation(plane); + randomTransformation(line); + randomTransformation(line2); + randomTransformation(line3); + + VERIFY_COLLIDES(plane, line); + VERIFY_COLLIDES(plane, line2); + VERIFY_NOT_COLLIDES(plane, line3); +} + }}} diff --git a/src/Physics/Test/PlaneTest.h b/src/Physics/Test/PlaneTest.h index 51d89b8d0..6deff3649 100644 --- a/src/Physics/Test/PlaneTest.h +++ b/src/Physics/Test/PlaneTest.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 PlaneTest: public QObject { +class PlaneTest: public AbstractShapeTest { Q_OBJECT private slots: void applyTransformation(); + void collisionLine(); }; }}}