# Simple 2-D Collision Detection

• 10-14-2006
Tonto
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.
• 10-14-2006
bumfluff
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
• 10-14-2006
Tonto
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.
• 10-14-2006
Snip
This tutorial might be useful. It shows how to do a collision test for some common shapes.
• 10-14-2006
Tonto
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.