Browse Source

Physics: Sphere % Sphere collision.

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

7
src/Physics/Sphere.cpp

@ -26,6 +26,8 @@ void Sphere::applyTransformation(const Matrix4& transformation) {
bool Sphere::collides(const AbstractShape* other) const { bool Sphere::collides(const AbstractShape* other) const {
if(other->type() == Type::Point) if(other->type() == Type::Point)
return *this % *static_cast<const Point*>(other); return *this % *static_cast<const Point*>(other);
if(other->type() == Type::Sphere)
return *this % *static_cast<const Sphere*>(other);
return AbstractShape::collides(other); return AbstractShape::collides(other);
} }
@ -35,4 +37,9 @@ bool Sphere::operator%(const Point& other) const {
Math::pow<2>(transformedRadius()); Math::pow<2>(transformedRadius());
} }
bool Sphere::operator%(const Sphere& other) const {
return (other.transformedPosition()-transformedPosition()).lengthSquared() <
Math::pow<2>(transformedRadius()+other.transformedRadius());
}
}} }}

3
src/Physics/Sphere.h

@ -64,6 +64,9 @@ class PHYSICS_EXPORT Sphere: public AbstractShape {
/** @brief Collision with point */ /** @brief Collision with point */
bool operator%(const Point& other) const; bool operator%(const Point& other) const;
/** @brief Collision with sphere */
bool operator%(const Sphere& other) const;
protected: protected:
inline Type type() const { return Type::Sphere; } inline Type type() const { return Type::Sphere; }

13
src/Physics/Test/SphereTest.cpp

@ -54,4 +54,17 @@ void SphereTest::collisionPoint() {
VERIFY_NOT_COLLIDES(sphere, point2); VERIFY_NOT_COLLIDES(sphere, point2);
} }
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);
Physics::Sphere sphere2({1.0f, 3.0f, 0.0f}, 1.0f);
randomTransformation(sphere);
randomTransformation(sphere1);
randomTransformation(sphere2);
VERIFY_COLLIDES(sphere, sphere1);
VERIFY_NOT_COLLIDES(sphere, sphere2);
}
}}} }}}

1
src/Physics/Test/SphereTest.h

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

Loading…
Cancel
Save