Hi
My objects are not moving that fast, so I think that makes a little simpler. Anyways here is my attempt for a collision detection algorithm for a circle hitting a square, please comment on it, can it be made faster/better?
//cPx = x coordinate of circle
//cPy = y coordinate of circle
//cr = radius of circle
//sqW = with of square
//sqH = height of square
//sqX = x coordinate of square
//sqY = y coordinate of square
-(BOOL)gcCircleCenterRadiusCollidesSquare: (float) cPx : (float) cPy : (float) cr : (float) sqW : (float) sqH : (float) sqX : (float) sqY {
float sqRx = sqX + sqW;
float sqLx = sqX - sqW;
float sqTy = sqY + sqH;
float sqBy = sqY - sqH;
if(sqrt(pow(sqRx - cPx, 2) + pow(sqBy - cPy, 2)) < cr ||
sqrt(pow(sqRx - cPx, 2) + pow(sqTy - cPy, 2)) < cr ||
sqrt(pow(sqLx - cPx, 2) + pow(sqTy - cPy, 2)) < cr ||
sqrt(pow(sqLx - cPx, 2) + pow(sqBy - cPy, 2)) < cr ||
((cPx < sqRx && cPx > sqLx) && (cPy - cr <= sqTy && cPy - cr > sqBy)) ||
((cPx < sqRx && cPx > sqLx) && (cPy + cr <= sqTy && cPy + cr > sqBy))) {
return TRUE;
}
return FALSE;
}