1

I have a problem with collision detection of a circle and a rectangle. I have tried to solve the problem with the Pythagorean Theorem. But none of the queries works. The rectangle collides with the rectangular bounding box of the circle.

 if (CGRectIntersectsRect(player.frame, visibleEnemy.frame)) {
   if (([visibleEnemy spriteTyp] == jumper || [visibleEnemy spriteTyp] == wobble )) {
     if ((visibleEnemy.center.x - player.frame.origin.x) * (visibleEnemy.center.x - player.frame.origin.x) +
         (visibleEnemy.center.y - player.frame.origin.y) * (visibleEnemy.center.y - player.frame.origin.y) <=
         (visibleEnemy.bounds.size.width/2 * visibleEnemy.bounds.size.width/2)) {
       NSLog(@"Check  1");
       normalAction = NO;
     }

     if ((visibleEnemy.center.x - (player.frame.origin.x + player.bounds.size.width)) *
         (visibleEnemy.center.x - (player.frame.origin.x + player.bounds.size.width)) +
         (visibleEnemy.center.y - player.frame.origin.y) * (visibleEnemy.center.y - player.frame.origin.y) <=
         (visibleEnemy.bounds.size.width/2 * visibleEnemy.bounds.size.width/2)) {
       NSLog(@"Check  2"); 
       normalAction = NO;
     }
     else {
       NSLog(@"Check  3");
       normalAction = NO;
     }
   }         
 }
Hidde
  • 11,493
  • 8
  • 43
  • 68
  • So are you saying that currently the rectangle collides with the circles bounding box? Or that's what you are wanting? What you wrote is slightly confusing – Smittey Mar 02 '14 at 22:24
  • possible duplicate of [Circle-Rectangle collision detection (intersection)](http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection) – FogleBird Mar 02 '14 at 22:25
  • Thanks for the reply, I want that the rectangle collides with the outline of the circle and not with the rectangular bounding box. – user3371865 Mar 02 '14 at 22:34

1 Answers1

0

Here is how I did it in one of my small gaming projects. It gave me best results and it's simple. My code detects if there is a collision between circle and the line. So you can easily adopt it to circle - rectangle collision detection by checking all 4 edges of the rectangle.

Let's say that a ball has a ballRadius, and location (xBall, yBall). The line is defined with two points (xStart, yStart) and (xEnd, yEnd).

Implementation of a simple collision detection:

float ballRadius = ...;
float x1 = xStart - xBall;
float y1 = yStart - yBall;

float x2 = xEnd - xBall;
float y2 = yEnd - yBall;

float dx = x2 - x1;
float dy = y2 - y1;
float dr = sqrtf(powf(dx, 2) + powf(dy, 2));
float D = x1*y2 - x2*y1;

float delta = powf(ballRadius*0.9,2)*powf(dr,2) - powf(D,2);

if (delta >= 0) 
{ 
    // Collision detected 
}

If delta is greater than zero there are two intersections between ball (circle) and line. If delta is equal to zero there is one intersection – perfect collision.

I hope it will help you.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • Thanks, but this is very complicated. Maybe someone has a suggestion based on my approach. – user3371865 Mar 02 '14 at 23:00
  • Nice, but can you please describe a bit more on the math behind this? Would like to know why. Thanks in Advanced. – Unheilig Mar 02 '14 at 23:33
  • This will work if the ball hits the "edge" if the line too? like >>>O ------ –  Mar 22 '14 at 14:30