Thread: Collision between circles and squares

  1. #1
    Registered User CreatedByShadow's Avatar
    Join Date
    Jan 2006

    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.

  2. #2
    Registered User
    Join Date
    Apr 2006
    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.


  3. #3
    return 0;
    Join Date
    Jan 2005
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    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: 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:

    // 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.
    Last edited by foxman; 07-19-2007 at 11:24 AM.

  6. #6
    Registered User CreatedByShadow's Avatar
    Join Date
    Jan 2006
    Awesome, thanks for all the replies!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circles and Computers
    By G4B3 in forum Tech Board
    Replies: 4
    Last Post: 11-29-2008, 09:23 AM
  2. basic collision detection
    By revelation437 in forum Game Programming
    Replies: 2
    Last Post: 09-09-2003, 01:15 PM
  3. Collision detection algorithm
    By Hannwaas in forum Game Programming
    Replies: 5
    Last Post: 11-30-2001, 01:27 PM