Sign in to follow this  
mobeen

2D tile based game collision problem

Recommended Posts

Hello everyone, Intro: I m currently doing a pacman clone (2d tile based game) in c# using gdi plus . All sprites are 24x24. the whole level is 22 x 35. All sprites are positioned such that their positions are in the center. Problem: Collision of pacman with the walls. The pacman stops at different positions that are offseted everytime by 1-5 pixels from the wall.This causes it to move out from the movement channel. How I m doing it? I have a sprite base class which has an update method as follows.
void Sprite::Update(float elapsedTime){...}
I load up my level from an external file, fill Sprites into an array. During render of the level, every sprite's update method is first called thereafter the sprite's render method is called. The update method determines the current direction of sprite and then calls Move as follows
void Sprite::Update(float elapsedtime)
{
   float dx=0, dy=0;
   switch(Direction)
   {
      case enDirections.NORTH:
         dy=-speed;
            break;

      case enDirections.SOUTH:
         dy=speed;
            break;

      case enDirections.WEST:
         dx=-speed;
            break;

      case enDirections.EAST:
         dx=speed;
            break;
   }

   Move(dx*elapsedTime, dy*elapsedTime);
}
Now in move, I simply create a new temp x and y value and check it into the level If there is a wall, i push the pacman by the amount it has intruded into the wall as follows.
bool Sprite::Move(float dx, float dy)
{
   float nx = currentPos.X + dx;
   float ny = currentPos.Y + dy;
   float deltaX=0, deltaY=0;
   switch(Direction)
   {
      case enDirections.NORTH:
         deltaY=-HALF_SPRITE_SIZE;
            break;

      case enDirections.SOUTH:
         deltaY=HALF_SPRITE_SIZE;
            break;

      case enDirections.WEST:
         deltaX=-HALF_SPRITE_SIZE;
            break;

      case enDirections.EAST:
         deltaX=HALF_SPRITE_SIZE;
            break;
   }

   if(IsValidPosition(nx+deltaX, ny+deltaY))
   {
      //some code to remove sprite
      
      currentPos.X=nx;
      currentPos.Y=ny;
      return true;
   }
   else //We have bumped into a wall
   { 
      int colIndex = (int)(nx/SPRITE_SIZE);
      int rowIndex = (int)(ny/SPRITE_SIZE);
      
      int index = (rowIndex*MAZE_WIDTH+colIndex)-1;
       
      float wallposX =Level.CurrentLevel.Sprite[index].pos.X - deltaX;
      float wallposY =Level.CurrentLevel.Sprite[index].pos.Y - deltaY;
      
      float offsetX = wallposX - nx;
      float offsetY = wallposY - ny;

      currentPos.X+=offsetX;
      currentPos.Y+=offsetY;
      
      //Prevent it to move any further
      Direction = enDirections.STATIC;
   }
   return false;
}
What is the wrong thing in my code or is there anything that is wrong or any other better way of doing it. Any website link tutorial is welcomed. Waiting for your comments be leanient please ;-) If anyone is interested in helping me out let me know i will upload the source. Regards Mobeen

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sounds like the fidelity of your character motion is too accurate for it's own good.

Consider making the game logic handle characters as being always in Discrete tile-aligned locations; but simply have an Animation that shows the moving from one to the next as a smooth motion.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Sounds like the fidelity of your character motion is too accurate for it's own good.

Consider making the game logic handle characters as being always in Discrete tile-aligned locations; but simply have an Animation that shows the moving from one to the next as a smooth motion.

That might not be a good idea for a Pac Man game.. with a ghost on your tail, you could be "killed" a whole tile away.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by oscinis
That might not be a good idea for a Pac Man game.. with a ghost on your tail, you could be "killed" a whole tile away.


Depends how well you code it.

If you limit ghost motion to be on the same quantized timer as player motion; then the positions of everything would be pretty solid.

You'd time the transition animation to synchronize with this stuff of course, so running into a ghost vs near misses doesn't look ambigous.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Sounds like the fidelity of your character motion is too accurate for it's own good.

Consider making the game logic handle characters as being always in Discrete tile-aligned locations; but simply have an Animation that shows the moving from one to the next as a smooth motion.


You were spot on Anonymous Poster. As soon as i changed the movement logic from speed to tile based location, it worked.

With collision part done now the ai part is what i am currently doing.

Thanks for the help dude.
Regards
Mobeen

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this