Browse Source

Physics: Sphere % Line collision.

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

11
src/Physics/Sphere.cpp

@ -15,6 +15,10 @@
#include "Sphere.h"
#include "Math/Geometry/Distance.h"
using namespace Magnum::Math::Geometry;
namespace Magnum { namespace Physics {
void Sphere::applyTransformation(const Matrix4& transformation) {
@ -26,6 +30,8 @@ void Sphere::applyTransformation(const Matrix4& transformation) {
bool Sphere::collides(const AbstractShape* other) const {
if(other->type() == Type::Point)
return *this % *static_cast<const Point*>(other);
if(other->type() == Type::Line)
return *this % *static_cast<const Line*>(other);
if(other->type() == Type::Sphere)
return *this % *static_cast<const Sphere*>(other);
@ -37,6 +43,11 @@ bool Sphere::operator%(const Point& other) const {
Math::pow<2>(transformedRadius());
}
bool Sphere::operator%(const Line& other) const {
return Distance::linePointSquared(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

@ -21,6 +21,7 @@
#include "AbstractShape.h"
#include "Point.h"
#include "Line.h"
namespace Magnum { namespace Physics {
@ -64,6 +65,9 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
/** @brief Collision with point */
bool operator%(const Point& other) const;
/** @brief Collision with line */
bool operator%(const Line& other) const;
/** @brief Collision with sphere */
bool operator%(const Sphere& other) const;
@ -77,6 +81,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; }
#endif
}}

13
src/Physics/Test/SphereTest.cpp

@ -54,6 +54,19 @@ void SphereTest::collisionPoint() {
VERIFY_NOT_COLLIDES(sphere, point2);
}
void SphereTest::collisionLine() {
Physics::Sphere sphere({1.0f, 2.0f, 3.0f}, 2.0f);
Physics::Line line({1.0f, 1.5f, 3.5f}, {1.0f, 2.5f, 2.5f});
Physics::Line line2({1.0f, 2.0f, 5.1f}, {1.0f, 3.0f, 5.1f});
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

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

Loading…
Cancel
Save