Allegro.cc Forums » Programming Questions » Pong Collision

 Neil Roy Member #2,229 April 2002 I created a quick pong game a couple years ago, and my collision function (you really should separate your code into functions) basically went like this...```if(bottom1 < top2) return 0; if(top1 > bottom2) return 0; if(right1 < left2) return 0; if(left1 > right2) return 0; ``` It gets the bottom, top, left and right side (bounding box) of each object to test, then it does the above checks. if the bottom of one is higher up on the screen than the top of the other, than they are obviously not colliding. The same for the left and right sides, if the right side of one object is less than the left side of the other, than they cannot be colliding. This is better than checking to see if they overlap. If all four of the above checks fail, than you have a collision, you can then report a collision, or do further checks if you wish to have pixel perfect collision.Another method for collision detection which is easy, is circular. You basically assign a radius to each object for collision, you then check the distance between the two objects using some simple math, and if the distance is less than the sum of the two radiuses, than you have a collision. Otherwise you do not. It's probably one of the easiest to do.