Browse Source

Physics: Capsule % Sphere collision.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
c7c137752c
  1. 7
      src/Physics/Capsule.cpp
  2. 5
      src/Physics/Capsule.h
  3. 16
      src/Physics/Test/CapsuleTest.cpp
  4. 1
      src/Physics/Test/CapsuleTest.h

7
src/Physics/Capsule.cpp

@ -31,6 +31,8 @@ void Capsule::applyTransformation(const Matrix4& transformation) {
bool Capsule::collides(const AbstractShape* other) const {
if(other->type() == Type::Point)
return *this % *static_cast<const Point*>(other);
if(other->type() == Type::Sphere)
return *this % *static_cast<const Sphere*>(other);
return AbstractShape::collides(other);
}
@ -40,4 +42,9 @@ bool Capsule::operator%(const Point& other) const {
Math::pow<2>(transformedRadius());
}
bool Capsule::operator%(const Sphere& other) const {
return Distance::lineSegmentPointSquared(transformedA(), transformedB(), other.transformedPosition()) <
Math::pow<2>(transformedRadius()+other.transformedRadius());
}
}}

5
src/Physics/Capsule.h

@ -21,6 +21,7 @@
#include "AbstractShape.h"
#include "Point.h"
#include "Sphere.h"
namespace Magnum { namespace Physics {
@ -65,6 +66,9 @@ class PHYSICS_EXPORT Capsule: public AbstractShape {
/** @brief Collision with point */
bool operator%(const Point& other) const;
/** @brief Collision with sphere */
bool operator%(const Sphere& other) const;
protected:
inline Type type() const { return Type::Capsule; }
@ -76,6 +80,7 @@ class PHYSICS_EXPORT Capsule: public AbstractShape {
#ifndef DOXYGEN_GENERATING_OUTPUT
inline bool operator%(const Point& a, const Capsule& b) { return b % a; }
inline bool operator%(const Sphere& a, const Capsule& b) { return b % a; }
#endif
}}

16
src/Physics/Test/CapsuleTest.cpp

@ -52,4 +52,20 @@ void CapsuleTest::collisionPoint() {
VERIFY_NOT_COLLIDES(capsule, point2);
}
void CapsuleTest::collisionSphere() {
Physics::Capsule capsule({-1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, 2.0f);
Physics::Sphere sphere({3.0f, 0.0f, 0.0f}, 0.9f);
Physics::Sphere sphere1({3.5f, 1.0f, 0.0f}, 0.6f);
Physics::Sphere sphere2({1.0f, 4.1f, 0.0f}, 1.0f);
randomTransformation(capsule);
randomTransformation(sphere);
randomTransformation(sphere1);
randomTransformation(sphere2);
VERIFY_COLLIDES(capsule, sphere);
VERIFY_COLLIDES(capsule, sphere1);
VERIFY_NOT_COLLIDES(capsule, sphere2);
}
}}}

1
src/Physics/Test/CapsuleTest.h

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

Loading…
Cancel
Save