   Does my Collision Detection function suck?
  Howdy again, I'm back with yet more ugly code. This time I don't really have a specific problem, just an ugly area of code that I'd like someone to take a look at and give me some feedback on. The collision detection works great and it runs at a decent speed. But I'd really like to know of other ways of doing this as I'm sure my way isn't the best.C++/Dev-C++/SDLOk, this guy is my collision detection function. You give it two "Recs" and it makes sure they don't overlap. A Rec is simply a rectangle that all my units have. It covers their entire sprite. It basically says "I'm *this* wide and *this* tall and *here* is where I am." - Yeah... it talks to me.bool Collision(Rec Rec1, Rec Rec2){  if(Rec1.Bottom<=Rec2.Top)  return false;  else if(Rec1.Top>=Rec2.Bottom)  return false;  else if(Rec1.Right<=Rec2.Left)  return false;  else if(Rec1.Left>=Rec2.Right)  return false;  else  return true;}I got the idea for the above code from a Collision Detection tutorial found in a link off the SDL website. In his code he did a bit of math first and used SDL_Rects. So I made my own rectangle class and cut out the math.Ok, now on to the fun part. This ugly little bastard function takes in one parameter- an integer, which corresponds to the Unit that we're running the check on. Then we check all tiles near the Unit for possible collision problems. That way I don't have to check all tiles in the map, just the ones currently near the Unit. Ok, ready for this? This'll probably make your head spin as it's pretty ugly code...void UnitCollide(int U){  int z=Units.MyZone;  int x=int(Units.MyPos.x/TileSize);  int y=int(Units.MyPos.y/TileSize);//these 3 lines are here so we can save on doing a bunch of calculations over and over again    Rec TileRec; //This creates a rec for the Tiles we're looking at.    for(int a=0;a<3;a++)  for(int b=0;b<3;b++)//these for loops run through the 9 tiles surrounding our unit and check only those tiles for a collision  if(ThisMap.Zones[z].ZoneTiles[(a+x-1)][(b+y-1)]==1)  { //Here's where we set the Rec for our current Tile   TileRec.Top=(y+(b-1))*TileSize; TileRec.Bottom=(y+(b-1))*TileSize+TileSize;   TileRec.Left=(x+(a-1))*TileSize;   TileRec.Right=(x+(a-1))*TileSize+TileSize;    //TileSize is 32, it's basically how big the tiles are in one dimension. So tiles are actually 32x32. Most units are 64x64, that's why i check 9 tiles- One unit could easily be in 9 tiles all at the same time. /* And here's where we check for a collision   If a collision is found, we move the unit's x coordinate back to what it was before   Then we check for a collision again-   If a collision is detected we reverse what we just did and instead move the unit's y coord back   then we check again-   If there is still a collision then we re-reverse the x coord. at this point we completely move the unit back   to where it was before it even had a chance to move- thereby ensuring there's no collision.   */   if(Collision(Units.MyRec, TileRec))   { double XMove=Units.MyVertex.x/Units.MyVertex.z;Units.MyPos.x-=(XMove); Units.UpdateRec(); //UpdateRec() is used to update the unit's Rec to it's new position.if(Collision(Units.MyRec, TileRec)){    double YMove=Units.MyVertex.y/Units.MyVertex.z; Units.MyPos.x+=(XMove); Units.MyPos.y-=(YMove); Units.UpdateRec();  if(Collision(Units.MyRec, TileRec))  {Units.MyPos.x-=(XMove);Units.UpdateRec();    } } }}}I have it setup like this so that if a player or enemy is moving in two directions at once (ie: northwest, northeast, southeast, southwest) then they'll stop moving in the direction that is causing the collision but continue moving in the direction that has no problem. This way the player doesn't come to a complete stop if he hits an obstacle, now he can keep on hugging a wall while still moving.So, now you've seen my ugly code and I'm terribly embarassed. But hopefully someone can suggest a better way of doing all this and my code will be better for it. Can anyone suggest a better way to do this? I know collision detection is a huge area and a large source of problems for beginners. I tried doing it on my own, but now I'd like to hear some advice on how others check for collisions. Thanks for your time and future responses.Thanks,-ArtP.S.- If you want/need to see more code I have plenty of it- just ask. I think I've included enough code to get you a general idea of what's going on, but if you need more speak up. I'm here to learn and get feedback.P.P.S.- Oh yeah, ThisMap is the name of the current map the units are on. Each map has 25 zones that the units can move in and out of. And each zone is comprised of Tiles declared as a 2D array, int ZoneTiles[100][100]. If the value of ZoneTiles[x][y] is 1, then that tile is an obstacle.   