From 34294541b95642bd9b317010171070ecbd2de4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 28 Apr 2012 02:18:13 +0200 Subject: [PATCH] Physics: Plane % LineSegment collision. --- src/Physics/Plane.cpp | 8 ++++++++ src/Physics/Plane.h | 5 +++++ src/Physics/Test/PlaneTest.cpp | 16 ++++++++++++++++ src/Physics/Test/PlaneTest.h | 1 + 4 files changed, 30 insertions(+) diff --git a/src/Physics/Plane.cpp b/src/Physics/Plane.cpp index a192fbe00..ba72c7c37 100644 --- a/src/Physics/Plane.cpp +++ b/src/Physics/Plane.cpp @@ -32,6 +32,8 @@ void Plane::applyTransformation(const Matrix4& transformation) { bool Plane::collides(const AbstractShape* other) const { if(other->type() == Type::Line) return *this % *static_cast(other); + if(other->type() == Type::LineSegment) + return *this % *static_cast(other); return AbstractShape::collides(other); } @@ -41,4 +43,10 @@ bool Plane::operator%(const Line& other) const { return t != t || (t != numeric_limits::infinity() && t != -numeric_limits::infinity()); } +bool Plane::operator%(const LineSegment& other) const { + float t = Intersection::planeLine(transformedPosition(), transformedNormal(), other.transformedA(), other.transformedB()); + Corrade::Utility::Debug() << transformedPosition() << transformedNormal() << other.transformedA() << other.transformedB() << t; + return t > 0.0f && t < 1.0f; +} + }} diff --git a/src/Physics/Plane.h b/src/Physics/Plane.h index 40c061be6..2b9cc417b 100644 --- a/src/Physics/Plane.h +++ b/src/Physics/Plane.h @@ -22,6 +22,7 @@ #include "AbstractShape.h" #include "Line.h" +#include "LineSegment.h" namespace Magnum { namespace Physics { @@ -64,6 +65,9 @@ class PHYSICS_EXPORT Plane: public AbstractShape { /** @brief Collision with line */ bool operator%(const Line& other) const; + /** @brief Collision with line segment */ + bool operator%(const LineSegment& other) const; + protected: inline Type type() const { return Type::Plane; } @@ -74,6 +78,7 @@ class PHYSICS_EXPORT Plane: public AbstractShape { #ifndef DOXYGEN_GENERATING_OUTPUT inline bool operator%(const Line& a, const Plane& b) { return b % a; } +inline bool operator%(const LineSegment& a, const Plane& b) { return b % a; } #endif }} diff --git a/src/Physics/Test/PlaneTest.cpp b/src/Physics/Test/PlaneTest.cpp index 1a65f72b2..4f3712427 100644 --- a/src/Physics/Test/PlaneTest.cpp +++ b/src/Physics/Test/PlaneTest.cpp @@ -52,4 +52,20 @@ void PlaneTest::collisionLine() { VERIFY_NOT_COLLIDES(plane, line3); } +void PlaneTest::collisionLineSegment() { + Physics::Plane plane(Vector3(), Vector3::yAxis()); + Physics::LineSegment line({0.0f, -0.1f, 0.0f}, {0.0f, 7.0f, 0.0f}); + Physics::LineSegment line2({0.0f, 0.1f, 0.0f}, {0.0f, 7.0f, 0.0f}); + Physics::LineSegment line3({0.0f, -7.0f, 0.0f}, {0.0f, -0.1f, 0.0f}); + + randomTransformation(plane); + randomTransformation(line); + randomTransformation(line2); + randomTransformation(line3); + + VERIFY_COLLIDES(plane, line); + VERIFY_NOT_COLLIDES(plane, line2); + VERIFY_NOT_COLLIDES(plane, line3); +} + }}} diff --git a/src/Physics/Test/PlaneTest.h b/src/Physics/Test/PlaneTest.h index 6deff3649..e8404692f 100644 --- a/src/Physics/Test/PlaneTest.h +++ b/src/Physics/Test/PlaneTest.h @@ -25,6 +25,7 @@ class PlaneTest: public AbstractShapeTest { private slots: void applyTransformation(); void collisionLine(); + void collisionLineSegment(); }; }}}