mirror of https://github.com/mosra/magnum.git
Browse Source
Seems like an essential part of the toolbox -- we have Point, LineSegment and Line and now we have also all "swept sphere" alternatives to them -- Sphere (swept "along" the point), Capsule (swept along line segment) and Cylinder (swept along infinite line). "Implemented" collision detection with point and sphere, which is just simplified version of collision detection implemented for Capsule (i.e. no need to check for cap cases).pull/23/head
12 changed files with 295 additions and 10 deletions
@ -0,0 +1,58 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "Cylinder.h" |
||||
|
||||
#include "Math/Functions.h" |
||||
#include "Math/Matrix3.h" |
||||
#include "Math/Matrix4.h" |
||||
#include "Math/Geometry/Distance.h" |
||||
#include "Magnum.h" |
||||
#include "Shapes/Point.h" |
||||
#include "Shapes/Sphere.h" |
||||
|
||||
using namespace Magnum::Math::Geometry; |
||||
|
||||
namespace Magnum { namespace Shapes { |
||||
|
||||
template<UnsignedInt dimensions> Cylinder<dimensions> Cylinder<dimensions>::transformed(const typename DimensionTraits<dimensions, Float>::MatrixType& matrix) const { |
||||
return Cylinder<dimensions>(matrix.transformPoint(_a), matrix.transformPoint(_b), matrix.uniformScaling()*_radius); |
||||
} |
||||
|
||||
template<UnsignedInt dimensions> bool Cylinder<dimensions>::operator%(const Point<dimensions>& other) const { |
||||
return Distance::linePointSquared(_a, _b, other.position()) < |
||||
Math::pow<2>(_radius); |
||||
} |
||||
|
||||
template<UnsignedInt dimensions> bool Cylinder<dimensions>::operator%(const Sphere<dimensions>& other) const { |
||||
return Distance::linePointSquared(_a, _b, other.position()) < |
||||
Math::pow<2>(_radius+other.radius()); |
||||
} |
||||
|
||||
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||
template class MAGNUM_SHAPES_EXPORT Cylinder<2>; |
||||
template class MAGNUM_SHAPES_EXPORT Cylinder<3>; |
||||
#endif |
||||
|
||||
}} |
||||
@ -0,0 +1,117 @@
|
||||
#ifndef Magnum_Shapes_Cylinder_h |
||||
#define Magnum_Shapes_Cylinder_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class Magnum::Shapes::Cylinder, typedef Magnum::Shapes::Cylinder2D, Magnum::Shapes::Cylinder3D |
||||
*/ |
||||
|
||||
#include "Math/Vector3.h" |
||||
#include "DimensionTraits.h" |
||||
#include "Shapes/Shapes.h" |
||||
#include "Shapes/magnumShapesVisibility.h" |
||||
|
||||
namespace Magnum { namespace Shapes { |
||||
|
||||
/**
|
||||
@brief Infinite cylinder defined by line and radius |
||||
|
||||
Unlike other elements the cylinder expects uniform scaling. See @ref shapes for |
||||
brief introduction. |
||||
@see @ref Cylinder2D, @ref Cylinder3D, @ref Capsule |
||||
@todo Store the radius as squared value to avoid sqrt/pow? Will complicate |
||||
collision detection with sphere. |
||||
*/ |
||||
template<UnsignedInt dimensions> class MAGNUM_SHAPES_EXPORT Cylinder { |
||||
public: |
||||
enum: UnsignedInt { |
||||
Dimensions = dimensions /**< Dimension count */ |
||||
}; |
||||
|
||||
/**
|
||||
* @brief Constructor |
||||
* |
||||
* Creates zero-sized cylinder at origin. |
||||
*/ |
||||
constexpr /*implicit*/ Cylinder(): _radius(0.0f) {} |
||||
|
||||
/** @brief Constructor */ |
||||
constexpr /*implicit*/ Cylinder(const typename DimensionTraits<dimensions, Float>::VectorType& a, const typename DimensionTraits<dimensions, Float>::VectorType& b, Float radius): _a(a), _b(b), _radius(radius) {} |
||||
|
||||
/** @brief Transformed shape */ |
||||
Cylinder<dimensions> transformed(const typename DimensionTraits<dimensions, Float>::MatrixType& matrix) const; |
||||
|
||||
/** @brief First point */ |
||||
constexpr typename DimensionTraits<dimensions, Float>::VectorType a() const { |
||||
return _a; |
||||
} |
||||
|
||||
/** @brief Set first point */ |
||||
void setA(const typename DimensionTraits<dimensions, Float>::VectorType& a) { |
||||
_a = a; |
||||
} |
||||
|
||||
/** @brief Second point */ |
||||
constexpr typename DimensionTraits<dimensions, Float>::VectorType b() const { |
||||
return _b; |
||||
} |
||||
|
||||
/** @brief Set second point */ |
||||
void setB(const typename DimensionTraits<dimensions, Float>::VectorType& b) { |
||||
_b = b; |
||||
} |
||||
|
||||
/** @brief Radius */ |
||||
constexpr Float radius() const { return _radius; } |
||||
|
||||
/** @brief Set radius */ |
||||
void setRadius(Float radius) { _radius = radius; } |
||||
|
||||
/** @brief Collision with point */ |
||||
bool operator%(const Point<dimensions>& other) const; |
||||
|
||||
/** @brief Collision with sphere */ |
||||
bool operator%(const Sphere<dimensions>& other) const; |
||||
|
||||
private: |
||||
typename DimensionTraits<dimensions, Float>::VectorType _a, _b; |
||||
Float _radius; |
||||
}; |
||||
|
||||
/** @brief Infinite two-dimensional cylinder */ |
||||
typedef Cylinder<2> Cylinder2D; |
||||
|
||||
/** @brief Infinite three-dimensional cylinder */ |
||||
typedef Cylinder<3> Cylinder3D; |
||||
|
||||
/** @collisionoperator{Point,Cylinder} */ |
||||
template<UnsignedInt dimensions> inline bool operator%(const Point<dimensions>& a, const Cylinder<dimensions>& b) { return b % a; } |
||||
|
||||
/** @collisionoperator{Sphere,Cylinder} */ |
||||
template<UnsignedInt dimensions> inline bool operator%(const Sphere<dimensions>& a, const Cylinder<dimensions>& b) { return b % a; } |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,85 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "Math/Matrix3.h" |
||||
#include "Math/Matrix4.h" |
||||
#include "Magnum.h" |
||||
#include "Shapes/Cylinder.h" |
||||
#include "Shapes/Point.h" |
||||
#include "Shapes/Sphere.h" |
||||
|
||||
#include "ShapeTestBase.h" |
||||
|
||||
namespace Magnum { namespace Shapes { namespace Test { |
||||
|
||||
class CylinderTest: public TestSuite::Tester { |
||||
public: |
||||
CylinderTest(); |
||||
|
||||
void transformed(); |
||||
void transformedAverageScaling(); |
||||
void collisionPoint(); |
||||
void collisionSphere(); |
||||
}; |
||||
|
||||
CylinderTest::CylinderTest() { |
||||
addTests({&CylinderTest::transformed, |
||||
&CylinderTest::collisionPoint, |
||||
&CylinderTest::collisionSphere}); |
||||
} |
||||
|
||||
void CylinderTest::transformed() { |
||||
const Shapes::Cylinder3D cylinder({1.0f, 2.0f, 3.0f}, {-1.0f, -2.0f, -3.0f}, 7.0f); |
||||
|
||||
const auto transformed = cylinder.transformed(Matrix4::scaling(Vector3(2.0f))*Matrix4::rotation(Deg(90.0f), Vector3::zAxis())); |
||||
CORRADE_COMPARE(transformed.a(), Vector3(-4.0f, 2.0f, 6.0f)); |
||||
CORRADE_COMPARE(transformed.b(), Vector3(4.0f, -2.0f, -6.0f)); |
||||
CORRADE_COMPARE(transformed.radius(), 14.0f); |
||||
} |
||||
|
||||
void CylinderTest::collisionPoint() { |
||||
Shapes::Cylinder3D cylinder({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f); |
||||
Shapes::Point3D point({2.0f, 0.0f, 0.0f}); |
||||
Shapes::Point3D point1({1.0f, 3.1f, 0.0f}); |
||||
Shapes::Point3D point2({2.9f, -1.0f, 0.0f}); |
||||
|
||||
VERIFY_COLLIDES(cylinder, point); |
||||
VERIFY_COLLIDES(cylinder, point1); |
||||
VERIFY_NOT_COLLIDES(cylinder, point2); |
||||
} |
||||
|
||||
void CylinderTest::collisionSphere() { |
||||
Shapes::Cylinder3D cylinder({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f); |
||||
Shapes::Sphere3D sphere({3.0f, 0.0f, 0.0f}, 0.9f); |
||||
Shapes::Sphere3D sphere1({1.0f, 4.1f, 0.0f}, 1.0f); |
||||
Shapes::Sphere3D sphere2({3.5f, -1.0f, 0.0f}, 0.6f); |
||||
|
||||
VERIFY_COLLIDES(cylinder, sphere); |
||||
VERIFY_COLLIDES(cylinder, sphere1); |
||||
VERIFY_NOT_COLLIDES(cylinder, sphere2); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Shapes::Test::CylinderTest) |
||||
Loading…
Reference in new issue