Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //whether a circle and a rectangle collide
- bool circleCollision(const Location ¢er, double radius, const SDL_Rect &rect){
- // test 1 && (test 2 || test 3)
- double
- left = rect.x,
- right = left + rect.w,
- top = rect.y,
- bottom = top + rect.h,
- centerX = center.x,
- centerY = center.y;
- //test 1: the rectangles must collide, and
- if (centerX - radius > right ||
- centerX + radius < left ||
- centerY - radius > bottom ||
- centerY + radius < top)
- return false;
- //test 2: the center must be between the left-right or top-bottom edges of the rectangle, or
- if (centerX > left && centerX < right) ||
- centerY > top && centerY < bottom)
- return true;
- //test 3: at least one corner must be within circle
- if (distance(top, left, centerX, centerY) < radius ||
- distance(top, right, centerX, centerY) < radius ||
- distance(bottom, left, centerX, centerY) < radius ||
- distance(bottom, right, centerX, centerY) < radius)
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement