# Collision between circles and squares

• 07-19-2007
Collision between circles and squares
I just recently starting doing collision detection. I understand collision between two squares in SDL just fine. However, I am a little lost as how to program collision detection between a circle and a square...

How would I go about doing it? The way I figure it, test every side of the square to see if the distance between the side of the square and the radius of the circle is larger or smaller. But the thing is I don't know to code that thought.

Any help is greatly appreciated. Thanks in advance.
• 07-19-2007
fractality
What should the result be?
Just TRUE or FALSE or do you need penetration depth, contact normals, point/time of impact?

Collision detection is a generally quite messy thing to do, lots of special cases and also floating point precision errors that can give rise to all kind of bugs.

My best tip is to google for 2d or 3d collision, there are lots of code and tutorials floating around if you only need the basics to work. The ones on gamasutra are usually easy to read...

If you need more complicated (and still correct) collision handling, consider using a toolkit like Solid or OPCODE, they are pretty straightforward to use, and will most likely be much more optimized than you'll ever manage by yourself.

/f
• 07-19-2007
Snip
This tutorial explains collision detection between various 2D shapes, including a square and a circle (Section 3). It's a nice tutorial, the Flash animations make it easier to understand.
• 07-19-2007
Salem
• 07-19-2007
foxman
Well, there's diffrent way to do this. Usually, the less complex the solution, the more CPU time will it take (except if it's a really great solution :D ).

I did a game recently who involved collision detection (see this post: http://cboard.cprogramming.com/showp...0&postcount=84). First of all, i was using 2 surfaces for every "object" i had. One surface was for displaying, while the other surface was for doing collision detection and/or action detection (basically, one surface had some kind of information on it while the other was just for display). Since i had only one object who had to detect collision, this was fairly simple.

First of all, while loading the game, i was creating and array of coordinate for all the "peripherical" pixel of the object, relative to the (0,0) point of its surface. Then, for every frame, i was checking if any of those peripherical points had a specific color on the static object (that's it, object that weren't moving: terrain) and also doing some calculation to see if they were in the range of the dynamic object. If they do were (surfaces were overlapping), then, i had to check if it were really on the object or not by doing some simple math to define the coordinate of the pixel on the other surface and testing if the color of that pixel was the one corresponding to "a collision".

Well, it may not be totally clear. You may want to check my game for further investigation hah. But it can be quite a pain to understand, since, first of all, my first language isn't english, and it's written in C, and it's a bit voluminous (that's it, more or less 3 000 lines). And has some hard-to-understand-at-first-glance instruction, like this one:

Code:

```// Le point se trouve sur l'image de l'auto ordi numero j pixel = (Uint32 *) voituresOrdi[j].patron->masque[voituresOrdi[j].orientationCourante]->pixels         + (ptCourant.y - ((int) voituresOrdi[j].posCentre.y - voituresOrdi[j].patron->centre.y))         * voituresOrdi[j].patron->masque[voituresOrdi[j].orientationCourante]->w         + (ptCourant.x - ((int) voituresOrdi[j].posCentre.x - voituresOrdi[j].patron->centre.x));```
Anyway. This was fairly simple. And was the first game i made. And also the first thing i tought about how to detect 2D collision of object of any shape.
• 07-19-2007