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 *****
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;
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;
|| 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;
}