Browse Source

Physics: Plane % LineSegment collision.

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

8
src/Physics/Plane.cpp

@ -32,6 +32,8 @@ void Plane::applyTransformation(const Matrix4& transformation) {
bool Plane::collides(const AbstractShape* other) const {
if(other->type() == Type::Line)
return *this % *static_cast<const Line*>(other);
if(other->type() == Type::LineSegment)
return *this % *static_cast<const LineSegment*>(other);
return AbstractShape::collides(other);
}
@ -41,4 +43,10 @@ bool Plane::operator%(const Line& other) const {
return t != t || (t != numeric_limits<float>::infinity() && t != -numeric_limits<float>::infinity());
}
bool Plane::operator%(const LineSegment& other) const {
float t = Intersection::planeLine(transformedPosition(), transformedNormal(), other.transformedA(), other.transformedB());
Corrade::Utility::Debug() << transformedPosition() << transformedNormal() << other.transformedA() << other.transformedB() << t;
return t > 0.0f && t < 1.0f;
}
}}

5
src/Physics/Plane.h

@ -22,6 +22,7 @@
#include "AbstractShape.h"
#include "Line.h"
#include "LineSegment.h"
namespace Magnum { namespace Physics {
@ -64,6 +65,9 @@ class PHYSICS_EXPORT Plane: public AbstractShape {
/** @brief Collision with line */
bool operator%(const Line& other) const;
/** @brief Collision with line segment */
bool operator%(const LineSegment& other) const;
protected:
inline Type type() const { return Type::Plane; }
@ -74,6 +78,7 @@ class PHYSICS_EXPORT Plane: public AbstractShape {
#ifndef DOXYGEN_GENERATING_OUTPUT
inline bool operator%(const Line& a, const Plane& b) { return b % a; }
inline bool operator%(const LineSegment& a, const Plane& b) { return b % a; }
#endif
}}

16
src/Physics/Test/PlaneTest.cpp

@ -52,4 +52,20 @@ void PlaneTest::collisionLine() {
VERIFY_NOT_COLLIDES(plane, line3);
}
void PlaneTest::collisionLineSegment() {
Physics::Plane plane(Vector3(), Vector3::yAxis());
Physics::LineSegment line({0.0f, -0.1f, 0.0f}, {0.0f, 7.0f, 0.0f});
Physics::LineSegment line2({0.0f, 0.1f, 0.0f}, {0.0f, 7.0f, 0.0f});
Physics::LineSegment line3({0.0f, -7.0f, 0.0f}, {0.0f, -0.1f, 0.0f});
randomTransformation(plane);
randomTransformation(line);
randomTransformation(line2);
randomTransformation(line3);
VERIFY_COLLIDES(plane, line);
VERIFY_NOT_COLLIDES(plane, line2);
VERIFY_NOT_COLLIDES(plane, line3);
}
}}}

1
src/Physics/Test/PlaneTest.h

@ -25,6 +25,7 @@ class PlaneTest: public AbstractShapeTest {
private slots:
void applyTransformation();
void collisionLine();
void collisionLineSegment();
};
}}}

Loading…
Cancel
Save