From: Patrik Gornicz Date: Sat, 6 Dec 2008 14:49:12 +0000 (-0500) Subject: collision test cleaning X-Git-Tag: physics-premerge~81 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=39643454e15a57174037f04c7bb4f96520d36ad6;p=libbear.git collision test cleaning --- diff --git a/src/CollisionInfo.cpp b/src/CollisionInfo.cpp index 09e9083..e98bdb4 100644 --- a/src/CollisionInfo.cpp +++ b/src/CollisionInfo.cpp @@ -19,6 +19,11 @@ /// ***** Constructors/Destructors ***** +CollisionInfo::CollisionInfo() +{ + +} + CollisionInfo::CollisionInfo(const Vector2& point, const Vector2& normal) : point(point), normal(normal) { diff --git a/src/CollisionInfo.h b/src/CollisionInfo.h index 1c0cb19..379373b 100644 --- a/src/CollisionInfo.h +++ b/src/CollisionInfo.h @@ -26,6 +26,7 @@ class CollisionInfo { public: + CollisionInfo(); CollisionInfo(const Vector2& point, const Vector2& normal); ~CollisionInfo(); diff --git a/src/collisionManager.cpp b/src/collisionManager.cpp index 39f6396..e03135a 100644 --- a/src/collisionManager.cpp +++ b/src/collisionManager.cpp @@ -32,7 +32,7 @@ void applyCollisionAt(PhysicsEntity* p1, PhysicsEntity* p2); void applyCollisionAt(Ball* b1, Ball* b2); -CollisionInfo* getInfoAt(Ball* b1, Ball* b2); +bool getInfoAt(Ball* b1, Ball* b2, CollisionInfo* cInfo); /// ***** Private Variables ***** @@ -86,17 +86,14 @@ void applyCollisionAt(PhysicsEntity* p1, PhysicsEntity* p2) void applyCollisionAt(Ball* b1, Ball* b2) { - // /* - CollisionInfo* info = getInfoAt(b1, b2); + CollisionInfo cInfo; - if(info == NULL) + if(!getInfoAt(b1, b2, &cInfo)) return; // a few values to simplify the equations - Vector2 normal = info->normal; - Vector2 point = info->point; - - delete info; + const Vector2& normal = cInfo.normal; + const Vector2& point = cInfo.point; float m1 = b1->mass; float m2 = b2->mass; @@ -116,10 +113,9 @@ void applyCollisionAt(Ball* b1, Ball* b2) b1->applyImpulse(impulse / m1 * normal, point); b2->applyImpulse(-impulse / m2 * normal, point); - // */ } -CollisionInfo* getInfoAt(Ball* b1, Ball* b2) +bool getInfoAt(Ball* b1, Ball* b2, CollisionInfo* pcInfo) { // a few values to simplify the equations float r1 = b1->radius; @@ -135,17 +131,20 @@ CollisionInfo* getInfoAt(Ball* b1, Ball* b2) || p1.x + r1 < p2.x - r2 || p1.y - r1 > p2.y + r2 || p1.y + r1 < p2.y - r2) - return NULL; + return false; // test if not touching if ((p1 - p2).sqrLength() >= (r1 + r2)*(r1 + r2)) - return NULL; + return false; // test if they are moving apart in some way if they aren't it's likely // that they collided last frame and are still overlapping if ((v1 - v2).dot(p1 - p2) >= 0) - return NULL; + return false; + + pcInfo->point = p1 - (p1 - p2) * r1 / (r1 + r2); + pcInfo->normal = p1 - p2; - return new CollisionInfo(p1 - (p1 - p2) * r1 / (r1 + r2), p1 - p2); + return true; }