Collision between circles and squares

This is a discussion on Collision between circles and squares within the Game Programming forums, part of the General Programming Boards category; I just recently starting doing collision detection. I understand collision between two squares in SDL just fine. However, I am ...

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.

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.

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 ).

I did a game recently who involved collision detection (see this post: Post your games here...). 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: