Browse Source

Physics: Sphere % LineSegment collision.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
04d020fcd6
  1. 7
      src/Physics/Sphere.cpp
  2. 5
      src/Physics/Sphere.h
  3. 13
      src/Physics/Test/SphereTest.cpp
  4. 1
      src/Physics/Test/SphereTest.h

7
src/Physics/Sphere.cpp

@ -32,6 +32,8 @@ bool Sphere::collides(const AbstractShape* other) const {
return *this % *static_cast<const Point*>(other);
if(other->type() == Type::Line)
return *this % *static_cast<const Line*>(other);
if(other->type() == Type::LineSegment)
return *this % *static_cast<const LineSegment*>(other);
if(other->type() == Type::Sphere)
return *this % *static_cast<const Sphere*>(other);
@ -48,6 +50,11 @@ bool Sphere::operator%(const Line& other) const {
Math::pow<2>(transformedRadius());
}
bool Sphere::operator%(const LineSegment& other) const {
return Distance::lineSegmentPointSquared(other.transformedA(), other.transformedB(), transformedPosition()) <
Math::pow<2>(transformedRadius());
}
bool Sphere::operator%(const Sphere& other) const {
return (other.transformedPosition()-transformedPosition()).lengthSquared() <
Math::pow<2>(transformedRadius()+other.transformedRadius());

5
src/Physics/Sphere.h

@ -22,6 +22,7 @@
#include "AbstractShape.h"
#include "Point.h"
#include "Line.h"
#include "LineSegment.h"
namespace Magnum { namespace Physics {
@ -68,6 +69,9 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
/** @brief Collision with line */
bool operator%(const Line& other) const;
/** @brief Collision with line segment */
bool operator%(const LineSegment& other) const;
/** @brief Collision with sphere */
bool operator%(const Sphere& other) const;
@ -82,6 +86,7 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
#ifndef DOXYGEN_GENERATING_OUTPUT
inline bool operator%(const Point& a, const Sphere& b) { return b % a; }
inline bool operator%(const Line& a, const Sphere& b) { return b % a; }
inline bool operator%(const LineSegment& a, const Sphere& b) { return b % a; }
#endif
}}

13
src/Physics/Test/SphereTest.cpp

@ -67,6 +67,19 @@ void SphereTest::collisionLine() {
VERIFY_NOT_COLLIDES(sphere, line2);
}
void SphereTest::collisionLineSegment() {
Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f);
Physics::LineSegment line({1.0f, 2.0f, 4.9f}, {1.0f, 2.0f, 7.0f});
Physics::LineSegment line2({1.0f, 2.0f, 5.1f}, {1.0f, 2.0f, 7.0f});
randomTransformation(sphere);
randomTransformation(line);
randomTransformation(line2);
VERIFY_COLLIDES(sphere, line);
VERIFY_NOT_COLLIDES(sphere, line2);
}
void SphereTest::collisionSphere() {
Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f);
Physics::Sphere sphere1({1.0f, 3.0f, 5.0f}, 1.0f);

1
src/Physics/Test/SphereTest.h

@ -26,6 +26,7 @@ class SphereTest: public AbstractShapeTest {
void applyTransformation();
void collisionPoint();
void collisionLine();
void collisionLineSegment();
void collisionSphere();
};

Loading…
Cancel
Save