Browse Source

Physics: Capsule % Point collision.

pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
42bd89a76e
  1. 16
      src/Physics/Capsule.cpp
  2. 10
      src/Physics/Capsule.h
  3. 16
      src/Physics/Test/CapsuleTest.cpp
  4. 5
      src/Physics/Test/CapsuleTest.h

16
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<const Point*>(other);
return AbstractShape::collides(other);
}
bool Capsule::operator%(const Point& other) const {
return Distance::lineSegmentPointSquared(transformedA(), transformedB(), other.transformedPosition()) <
Math::pow<2>(transformedRadius());
}
}}

10
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

16
src/Physics/Test/CapsuleTest.cpp

@ -36,4 +36,20 @@ void CapsuleTest::applyTransformation() {
QCOMPARE(capsule.transformedRadius(), Math::Constants<GLfloat>::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);
}
}}}

5
src/Physics/Test/CapsuleTest.h

@ -15,15 +15,16 @@
GNU Lesser General Public License version 3 for more details.
*/
#include <QtCore/QObject>
#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();
};
}}}

Loading…
Cancel
Save