From 6e778aa533145dde20da61c64bf8996fdc240f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 27 Apr 2012 02:36:55 +0200 Subject: [PATCH] Physics: Sphere % Sphere collision. --- src/Physics/Sphere.cpp | 7 +++++++ src/Physics/Sphere.h | 3 +++ src/Physics/Test/SphereTest.cpp | 13 +++++++++++++ src/Physics/Test/SphereTest.h | 1 + 4 files changed, 24 insertions(+) diff --git a/src/Physics/Sphere.cpp b/src/Physics/Sphere.cpp index ffbdf6aff..4d969c6af 100644 --- a/src/Physics/Sphere.cpp +++ b/src/Physics/Sphere.cpp @@ -26,6 +26,8 @@ void Sphere::applyTransformation(const Matrix4& transformation) { bool Sphere::collides(const AbstractShape* other) const { if(other->type() == Type::Point) return *this % *static_cast(other); + if(other->type() == Type::Sphere) + return *this % *static_cast(other); return AbstractShape::collides(other); } @@ -35,4 +37,9 @@ bool Sphere::operator%(const Point& other) const { Math::pow<2>(transformedRadius()); } +bool Sphere::operator%(const Sphere& other) const { + return (other.transformedPosition()-transformedPosition()).lengthSquared() < + Math::pow<2>(transformedRadius()+other.transformedRadius()); +} + }} diff --git a/src/Physics/Sphere.h b/src/Physics/Sphere.h index c898ba27e..b3ababd51 100644 --- a/src/Physics/Sphere.h +++ b/src/Physics/Sphere.h @@ -64,6 +64,9 @@ class PHYSICS_EXPORT Sphere: 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::Sphere; } diff --git a/src/Physics/Test/SphereTest.cpp b/src/Physics/Test/SphereTest.cpp index bb43c6f71..e118df0bc 100644 --- a/src/Physics/Test/SphereTest.cpp +++ b/src/Physics/Test/SphereTest.cpp @@ -54,4 +54,17 @@ void SphereTest::collisionPoint() { 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); +} + }}} diff --git a/src/Physics/Test/SphereTest.h b/src/Physics/Test/SphereTest.h index fe61390da..439a54560 100644 --- a/src/Physics/Test/SphereTest.h +++ b/src/Physics/Test/SphereTest.h @@ -25,6 +25,7 @@ class SphereTest: public AbstractShapeTest { private slots: void applyTransformation(); void collisionPoint(); + void collisionSphere(); }; }}}