Browse Source

Physics: Sphere % Point collision.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
15dc8015b4
  1. 12
      src/Physics/Sphere.cpp
  2. 10
      src/Physics/Sphere.h
  3. 13
      src/Physics/Test/SphereTest.cpp
  4. 5
      src/Physics/Test/SphereTest.h

12
src/Physics/Sphere.cpp

@ -23,4 +23,16 @@ void Sphere::applyTransformation(const Matrix4& transformation) {
_transformedRadius = scaling*_radius; _transformedRadius = scaling*_radius;
} }
bool Sphere::collides(const AbstractShape* other) const {
if(other->type() == Type::Point)
return *this % *static_cast<const Point*>(other);
return AbstractShape::collides(other);
}
bool Sphere::operator%(const Point& other) const {
return (other.transformedPosition()-transformedPosition()).lengthSquared() <
Math::pow<2>(transformedRadius());
}
}} }}

10
src/Physics/Sphere.h

@ -20,6 +20,7 @@
*/ */
#include "AbstractShape.h" #include "AbstractShape.h"
#include "Point.h"
namespace Magnum { namespace Physics { namespace Magnum { namespace Physics {
@ -36,6 +37,8 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
void applyTransformation(const Matrix4& transformation); void applyTransformation(const Matrix4& transformation);
bool collides(const AbstractShape* other) const;
/** @brief Position */ /** @brief Position */
inline Vector3 position() const { return _position; } inline Vector3 position() const { return _position; }
@ -58,6 +61,9 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
return _transformedRadius; return _transformedRadius;
} }
/** @brief Collision with point */
bool operator%(const Point& other) const;
protected: protected:
inline Type type() const { return Type::Sphere; } inline Type type() const { return Type::Sphere; }
@ -66,6 +72,10 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
float _radius, _transformedRadius; float _radius, _transformedRadius;
}; };
#ifndef DOXYGEN_GENERATING_OUTPUT
inline bool operator%(const Point& a, const Sphere& b) { return b % a; }
#endif
}} }}
#endif #endif

13
src/Physics/Test/SphereTest.cpp

@ -41,4 +41,17 @@ void SphereTest::applyTransformation() {
QCOMPARE(sphere.transformedRadius(), Math::Constants<GLfloat>::Sqrt3*7.0f); QCOMPARE(sphere.transformedRadius(), Math::Constants<GLfloat>::Sqrt3*7.0f);
} }
void SphereTest::collisionPoint() {
Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f);
Physics::Point point({1.0f, 3.0f, 3.0f});
Physics::Point point2({1.0f, 3.0f, 1.0f});
randomTransformation(sphere);
randomTransformation(point);
randomTransformation(point2);
VERIFY_COLLIDES(sphere, point);
VERIFY_NOT_COLLIDES(sphere, point2);
}
}}} }}}

5
src/Physics/Test/SphereTest.h

@ -15,15 +15,16 @@
GNU Lesser General Public License version 3 for more details. GNU Lesser General Public License version 3 for more details.
*/ */
#include <QtCore/QObject> #include "AbstractShapeTest.h"
namespace Magnum { namespace Physics { namespace Test { namespace Magnum { namespace Physics { namespace Test {
class SphereTest: public QObject { class SphereTest: public AbstractShapeTest {
Q_OBJECT Q_OBJECT
private slots: private slots:
void applyTransformation(); void applyTransformation();
void collisionPoint();
}; };
}}} }}}

Loading…
Cancel
Save