Browse Source

Physics: Plane % Line collision.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
bceef6b8ad
  1. 19
      src/Physics/Plane.cpp
  2. 11
      src/Physics/Plane.h
  3. 16
      src/Physics/Test/PlaneTest.cpp
  4. 5
      src/Physics/Test/PlaneTest.h

19
src/Physics/Plane.cpp

@ -15,6 +15,13 @@
#include "Plane.h"
#include <limits>
#include "Math/Geometry/Intersection.h"
using namespace std;
using namespace Magnum::Math::Geometry;
namespace Magnum { namespace Physics {
void Plane::applyTransformation(const Matrix4& transformation) {
@ -22,4 +29,16 @@ void Plane::applyTransformation(const Matrix4& transformation) {
_transformedNormal = transformation.rotation()*_normal;
}
bool Plane::collides(const AbstractShape* other) const {
if(other->type() == Type::Line)
return *this % *static_cast<const Line*>(other);
return AbstractShape::collides(other);
}
bool Plane::operator%(const Line& other) const {
float t = Intersection::planeLine(transformedPosition(), transformedNormal(), other.transformedA(), other.transformedB());
return t != t || (t != numeric_limits<float>::infinity() && t != -numeric_limits<float>::infinity());
}
}}

11
src/Physics/Plane.h

@ -21,6 +21,8 @@
#include "AbstractShape.h"
#include "Line.h"
namespace Magnum { namespace Physics {
/** @brief Infinite plane, defined by position and normal */
@ -31,6 +33,8 @@ class PHYSICS_EXPORT Plane: public AbstractShape {
void applyTransformation(const Matrix4& transformation);
bool collides(const AbstractShape* other) const;
/** @brief Position */
inline Vector3 position() const { return _position; }
@ -57,6 +61,9 @@ class PHYSICS_EXPORT Plane: public AbstractShape {
return _transformedNormal;
}
/** @brief Collision with line */
bool operator%(const Line& other) const;
protected:
inline Type type() const { return Type::Plane; }
@ -65,6 +72,10 @@ class PHYSICS_EXPORT Plane: public AbstractShape {
_normal, _transformedNormal;
};
#ifndef DOXYGEN_GENERATING_OUTPUT
inline bool operator%(const Line& a, const Plane& b) { return b % a; }
#endif
}}
#endif

16
src/Physics/Test/PlaneTest.cpp

@ -36,4 +36,20 @@ void PlaneTest::applyTransformation() {
QVERIFY(plane.transformedNormal() == Vector3(Math::Constants<float>::Sqrt2, -Math::Constants<float>::Sqrt2, 0));
}
void PlaneTest::collisionLine() {
Physics::Plane plane(Vector3(), Vector3::yAxis());
Physics::Line line({0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f});
Physics::Line line2({0.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f});
Physics::Line line3({0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f});
randomTransformation(plane);
randomTransformation(line);
randomTransformation(line2);
randomTransformation(line3);
VERIFY_COLLIDES(plane, line);
VERIFY_COLLIDES(plane, line2);
VERIFY_NOT_COLLIDES(plane, line3);
}
}}}

5
src/Physics/Test/PlaneTest.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 PlaneTest: public QObject {
class PlaneTest: public AbstractShapeTest {
Q_OBJECT
private slots:
void applyTransformation();
void collisionLine();
};
}}}

Loading…
Cancel
Save