diff --git a/src/Physics/AxisAlignedBox.cpp b/src/Physics/AxisAlignedBox.cpp index 026dd62c5..abce0aa3e 100644 --- a/src/Physics/AxisAlignedBox.cpp +++ b/src/Physics/AxisAlignedBox.cpp @@ -17,6 +17,7 @@ #include "Math/Matrix3.h" #include "Math/Matrix4.h" +#include "Physics/Point.h" namespace Magnum { namespace Physics { @@ -25,6 +26,18 @@ template void AxisAlignedBox::applyTransfor _transformedMax = (matrix*typename DimensionTraits::PointType(_max)).vector(); } +template bool AxisAlignedBox::collides(const AbstractShape* other) const { + if(other->type() == AbstractShape::Type::Point) + return *this % *static_cast*>(other); + + return AbstractShape::collides(other); +} + +template bool AxisAlignedBox::operator%(const Point& other) const { + return (other.transformedPosition() >= _transformedMin).all() && + (other.transformedPosition() < _transformedMax).all(); +} + template class AxisAlignedBox<2>; template class AxisAlignedBox<3>; diff --git a/src/Physics/AxisAlignedBox.h b/src/Physics/AxisAlignedBox.h index 1f92ecf36..2b6eb7f4a 100644 --- a/src/Physics/AxisAlignedBox.h +++ b/src/Physics/AxisAlignedBox.h @@ -20,7 +20,8 @@ */ #include "Math/Vector3.h" -#include "AbstractShape.h" +#include "Physics/AbstractShape.h" +#include "Physics/Physics.h" #include "corradeCompatibility.h" @@ -42,6 +43,8 @@ template class MAGNUM_PHYSICS_EXPORT AxisAlignedBox: pu void applyTransformationMatrix(const typename DimensionTraits::MatrixType& matrix) override; + bool collides(const AbstractShape* other) const override; + /** @brief Minimal coordinates */ inline typename DimensionTraits::VectorType min() const { return _min; @@ -70,6 +73,9 @@ template class MAGNUM_PHYSICS_EXPORT AxisAlignedBox: pu return _transformedMax; } + /** @brief Collision with point */ + bool operator%(const Point& other) const; + private: typename DimensionTraits::VectorType _min, _max, _transformedMin, _transformedMax; @@ -81,6 +87,9 @@ typedef AxisAlignedBox<2> AxisAlignedBox2D; /** @brief Three-dimensional axis-aligned box */ typedef AxisAlignedBox<3> AxisAlignedBox3D; +/** @collisionoperator{Point,AxisAlignedBox} */ +template inline bool operator%(const Point& a, const AxisAlignedBox& b) { return b % a; } + }} #endif diff --git a/src/Physics/Test/AxisAlignedBoxTest.cpp b/src/Physics/Test/AxisAlignedBoxTest.cpp index 3ef7f3827..700822895 100644 --- a/src/Physics/Test/AxisAlignedBoxTest.cpp +++ b/src/Physics/Test/AxisAlignedBoxTest.cpp @@ -18,18 +18,23 @@ #include "Math/Constants.h" #include "Math/Matrix4.h" #include "Physics/AxisAlignedBox.h" +#include "Physics/Point.h" + +#include "ShapeTestBase.h" namespace Magnum { namespace Physics { namespace Test { -class AxisAlignedBoxTest: public Corrade::TestSuite::Tester { +class AxisAlignedBoxTest: public Corrade::TestSuite::Tester, ShapeTestBase { public: AxisAlignedBoxTest(); void applyTransformation(); + void collisionPoint(); }; AxisAlignedBoxTest::AxisAlignedBoxTest() { - addTests(&AxisAlignedBoxTest::applyTransformation); + addTests(&AxisAlignedBoxTest::applyTransformation, + &AxisAlignedBoxTest::collisionPoint); } void AxisAlignedBoxTest::applyTransformation() { @@ -40,6 +45,19 @@ void AxisAlignedBoxTest::applyTransformation() { CORRADE_COMPARE(box.transformedMax(), Vector3(3.0f, -1.0f, 5.5f)); } +void AxisAlignedBoxTest::collisionPoint() { + Physics::AxisAlignedBox3D box({-1.0f, -2.0f, -3.0f}, {1.0f, 2.0f, 3.0f}); + Physics::Point3D point1({-1.5f, -1.0f, 2.0f}); + Physics::Point3D point2({0.5f, 1.0f, -2.5f}); + + randomTransformation(box); + randomTransformation(point1); + randomTransformation(point2); + + VERIFY_NOT_COLLIDES(box, point1); + VERIFY_COLLIDES(box, point2); +} + }}} CORRADE_TEST_MAIN(Magnum::Physics::Test::AxisAlignedBoxTest)