This is a simple collision detection function I came up with. Rather than checking if two objects overlap, it tests if they do not. This goes against what we would normally do, but it is faster. It checks if the left edge of one box (it's minimum X co-ordinate) is greater than the right edge of another (it's maximum X co-ordinate), than the two cannot be colliding and returns false right away. It does the same for the vertical Y co-ordinates, if the top of one box (minimum Y co-ordinate) is greater than the bottom of another box (maximum Y co-ordinate) than it is impossible for the two to be colliding.
If either of those two tests are true, you can return immediately from this function with a false, or no collision which makes it much faster as most of the time, your objects will not be colliding and you don't want your program hanging around in a function for more time than it needs to.
If BOTH of those tests fail, than you absolutely have a collision and can return true, or process it even further to figure out where they are colliding if needed.
In this example code, I send it the information from a C++ Pong game I made, with the position of the ball and the paddle.
This is of course, simple bounding box collision.
1int collision(Ball &ball, Paddle &paddle)
3 int left1, left2, over_left;
4 int right1, right2, over_right;
5 int top1, top2, over_top;
6 int bottom1, bottom2, over_bottom;
7 int over_width, over_height;
8 int cx, cy;
9 int pixel1, pixel2;
11 left1 = (int)ball.get_x(); // get ball's left edge
12 left2 = paddle.get_x(); // get paddle's left edge
13 right1 = (int)ball.get_x() + ball.get_width(); // get ball's right edge
14 right2 = paddle.get_x() + paddle.get_width(); // get paddle's right edge
15 top1 = (int)ball.get_y(); // get ball's top edge
16 top2 = paddle.get_y(); // get paddle's top edge
17 bottom1 = (int)ball.get_y() + ball.get_height(); // get ball's bottom edge
18 bottom2 = paddle.get_y() + paddle.get_height(); // get paddle's bottom edge
21 // Test if the bounding boxes overlap.
22 // We can safely return false on any of these tests = fast and efficient
23 if(bottom1 < top2) return 0;
24 if(top1 > bottom2) return 0;
25 if(right1 < left2) return 0;
26 if(left1 > right2) return 0;
29 // Where on the paddle did the ball hit?
30 // This is for a pong game, so I return 1, 2, or 3 to indicate where the ball hit
31 // This could be easily improved with a little trigonometry
32 if(top1 < top2) return 1; // ball hit near top of paddle
33 if(bottom1 > bottom2) return 2; // ball hit near bottom of paddle
35 return 3; // ball hit centre area of paddle
For circular collision, this is code I use in my Deluxe Pacman 2 game (link below). Ignore the hack detection code, I put that in there so that if someone hacked my game to cheat (and someone did), than my game would alter itself and make it more difficult to play without making it immediately apparent that it detected the hack. This is originally intended as pure C code, not C++. Not much of a difference, but just so you understand why I use pointers * in this and references & in the bounding box above. You can't use references like the above in C functions... (just to be clear)...
1// Circular collision detection
p, GHOST *
4 // If the ghost is already dead, there is no collision
g->dead) return false;
gx = (double)
g->x + 16
.0; // ghost.x is based on tile size (32), so half that is 16.
gy = (double)
g->y + 16
.0; // ghost.y is based on tile size (32), so half that is 16.
px = (double)
py = (double)
15 // Hack detection, increase collision radius if a hack has been detected :)
gr += 5
19 printf("*** Hack Detected: %d ***\n"
20#endif // DEBUG
rt = abs(
rt) return false;
26 else if(abs(
rt) return false;
dist = sqrt((
dist_x) + (
dist_y)); // The distance of the vector between the two
30 // is the distance less than or equal to the absolute sum of the two radiuses?