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

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

    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
    Posts
    43
    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

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    One (of many) recent discussions
    Collision Test - 2D
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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

  6. #6
    Registered User CreatedByShadow's Avatar
    Join Date
    Jan 2006
    Posts
    24
    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, 08: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, 12:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21