Thread: Simple 2-D Collision Detection

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Simple 2-D Collision Detection

    I'm trying to implement some simple collision detection in a 2-D system, but I am having trouble. Right now I'm making pong, just squares and stuff, but I suppose that I may want to make asteroids or something, so I guess I'm looking for something like, how to detect collisions between convex polygons on a 2-D system. I have read things, and I am having trouble applying stuff like sphere-plane collision logic to this sort of thing because

    - Square can hit top of paddle
    - Ball not really a sphere

    Right now, I have the objects on this 2-D system just defined as a set of points (float: -1.0 - 1.0) that get moved around by some distance (object's v * delta t) every time the update procedure for the game is called in the game loop. So, the strategy for just looking whether it occupies some place seems a bit inappropriate.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    OK, you know a lot more about this than me, but the book book I have been reading uses IntersectRect(RECT rect1, RECT rect2) and that returns true if the two rects that hold the position of your two sprites are colliding.

    Code:
    // This code is lifted from the book, hope it is not infringing copyrights
    
    int Collision(SPRITE sprite1, SPRITE sprite2)
    {
        RECT rect1;
        rect1.left = sprite1.x+1;
        rect1.top = sprite1.y+1;
        rect1.right = sprite1.x + sprite1.width-1;
        rect1.bottom = sprite1.y + sprite1.height-1;
    
        RECT rect2;
        rect2.left = sprite2.x+1;
        rect2.top = sprite2.y+1;
        rect2.right = sprite2.x + sprite2.width-1;
        rect2.bottom = sprite2.y + sprite2.height-1;
    
        RECT dest;
        return IntersectRect(&dest, &rect1, &rect2);
    }
    The &dest is a dummy rect

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Thanks, I wasn't aware of that API. Though I did understand the logic for seeing if unrotated rectangles intersected.

    I want to be able to test any sort of convex polygon shape now. The only feasable way I could think of doing it is generating linear equations from all of the polygon segments (from both) and bruteforce testing to see if any intersect within the domain they occupy.

    This seems a really inconvenient, though feasable. I would really like an alternative though.

  4. #4
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    This tutorial might be useful. It shows how to do a collision test for some common shapes.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I think this seperating of axis thing / geometrix test intersection query stuff is very relevant. This article cited in the bibliography: http://www.geometrictools.com/Docume...ratingAxes.pdf seems very useful. It's kind of thick reading for me, but I may be able to take small steps through it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quad equation for collision detection?
    By joeprogrammer in forum Game Programming
    Replies: 1
    Last Post: 06-09-2006, 10:59 PM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  3. simple collision detection question
    By godhand in forum Game Programming
    Replies: 2
    Last Post: 05-25-2004, 11:27 PM
  4. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  5. Pacman-style Maze Collision Detection
    By SMurf in forum Game Programming
    Replies: 4
    Last Post: 02-24-2004, 05:47 PM