   PacMan AI Collision Detection
  I am making a pacman clone in SDL. I have got my PacMan able to go around the maze not coliding with walls and eating the dots and all the normal stuff. the problem i am having is with the ghost collision detection, which i have inherited from the pacman class so they should be the same. however the ghosts have problems with only certain types of corners, they turn in the direction of the corner, but then it looks like its detecting a collision incorrectly and it turns around again. What im confused about is, if it works correctly for the PacMan then why doesnt it work right for the ghost? Any help is appreciated. 
  If PacMan and the ghosts are different dimensions then the code might be specific to PacMan''s dimensions.Just a thought.You will have to post the collision detection function to get any real help. ;  << What im confused about is, if it works correctly for the PacMan then why doesnt it work right for the ghost? Any help is appreciated. >>If pacman and ghost are the same size, you can use one function to detect collision. For example, all my graphics are 30 x 30 pixels, and I use the following functions to detect whether a move is possible (which prevents going through walls). Works with either pacman or ghost since both are the same size (BLOCK_SIZE = 30). bool CanMove(int x, int y){   int c1,r1,c2,r2,c3,r3,c4,r4;  // maze object row and column corners c1 = XScreenToXMaze(x);   r1 = YScreenToYMaze(y);   c2 = XScreenToXMaze(x + BLOCK_SIZE - 1); r2 = YScreenToYMaze(y);   c3 = XScreenToXMaze(x);   r3 = YScreenToYMaze(y + BLOCK_SIZE - 1);   c4 = XScreenToXMaze(x + BLOCK_SIZE - 1); r4 = YScreenToYMaze(y + BLOCK_SIZE - 1);   if ((Maze[r1][c1] > 2) || (Maze[r2][c2] > 2) || (Maze[r3][c3] > 2) || (Maze[r4][c4] > 2)) return FALSE; // collision with wall, do not allow   else return TRUE;  // no collision, proceed } // END OF CanMove// convert X pixel position to maze columnint XScreenToXMaze(int x){  return (x - XTOP_LEFT) / BLOCK_SIZE;  }// convert Y pixel position to maze rowint YScreenToYMaze(int y){  return (y - YTOP_LEFT) / BLOCK_SIZE;  }x and y are screen coordinates, c and r are columns and rows for my maze array. I check any potential move with the four corners of the graphic against the maze (by converting from screen to maze array) before allowing a move. If Maze[r][c] > 2 means that is a piece of a wall, and each corner needs to be checked. If CanMove returns TRUE then the move is allowed. There''s probably easier ways to do this....but this works no matter what direction the graphic is moving (up/down/left/right).Also I decided on 30 x 30 pixels so I can use a couple varying speeds: 1, 2, 3, 5, 6, 10, 15 are all divisible by 30, so any of these can be used as increments. 32 x 32 would give you 1, 2, 4, 8, 16 which gives less increments. 3 pixels per frame is a good speed I found for pacman so I went with 30 x 30.NOTE: this was all figured out on paper (or NotePad) before I did a single line of C code. Important to get your design done and complete before coding.VazPac.cpp | EXEPhil P ;  PhilVaz: Very nicely done Pacman clone. Just a piece of advice: You should give OOP a try, you wont regret it.   [edited by - CProgrammer on February 12, 2004 8:20:13 AM];  Well, the way I''m doing the enemy AI, I don''t need collision detection at all.  I''m using an intersection based idea, which is working rather well.~Justin ;  please xplain your intersection idea, kinda sounds interesting. ;  Sure, then.  I don''t have any code with me right now (I''m on my home station, I work at school), but can pseudo it up.  *WARNING! IN JAVA* (but the concept is the same).What I do is set each intersection (a dead end or a turn counts) as a bounding rectangle (derived from the generic rectangle class to posess a ''type'' definition).  When the rectangle contains the enemy (as the enemy is 1 pixel smaller in both directions), the enemy responds based on what type of intersection it is.  For example, take this bit of pseudocode. public void randAI(Enemy en){for(x=0; x<intersectionArray.length; x++){if(intersectionArray[x].contains(en)){hitsInter=true;arrayLocation = x;break;}}if(hitsInter){switch(intersectionArray[arrayLocation].getType()){case 0: en.flipDirection();break;//Dead endcase 1: tempRand = en.pickOfThree();//4 way   switch(tempRand)   {   case 1: en.setDirLeft();break;   case 3: en.setDirRight();break;   default:break;   } {ETCETERA...}If you want the whole monty, I can post it up tomorrow.  Hope this helps!~Justin 