public bool CollidesWith(CollisionUnit other)
{
Vector2 otherCircleCenter;
int otherCircleRadius;
float dX;
float dY;
float dist;
Vector2 otherUpperLeft;
Vector2 otherLowerRight;
mCheckedUnits.Add(other);
if (mCollisionType == CollisionType.COLLISION_CIRCLE && other.GetCollisionType() == CollisionType.COLLISION_CIRCLE)
{
otherCircleCenter = other.GetCircleCenter();
otherCircleRadius = other.GetCircleRadius();
dX = mCircleCenter.X - otherCircleCenter.X;
dY = mCircleCenter.Y - otherCircleCenter.Y;
dist = (dX * dX) + (dY * dY);
// (radius + radius)^2 instead of more expensive squareRoot(dist)
return (dist <= (mCircleRadius + otherCircleRadius) * (mCircleRadius + otherCircleRadius));
}
else if (mCollisionType == CollisionType.COLLISION_CIRCLE && other.GetCollisionType() == CollisionType.COLLISION_BOX)
{
return CircleBoxCollision(mCircleCenter, mCircleRadius, other.GetUpperLeft(), other.GetLowerRight());
}
else if (mCollisionType == CollisionType.COLLISION_BOX && other.GetCollisionType() == CollisionType.COLLISION_CIRCLE)
{
return CircleBoxCollision(other.GetCircleCenter(), other.GetCircleRadius(), mPoint1, mPoint2);
}
else if (mCollisionType == CollisionType.COLLISION_BOX && other.GetCollisionType() == CollisionType.COLLISION_BOX)
{
otherUpperLeft = other.GetUpperLeft();
otherLowerRight = other.GetLowerRight();
return (mPoint1.X <= otherLowerRight.X && mPoint1.Y <= otherLowerRight.Y && otherUpperLeft.X <= mPoint2.X && otherUpperLeft.Y <= mPoint2.Y);
}
// TODO: other collision types - line, pixel
return false;
}