Advertisement
thecplusplusguy

sidescroller collision 2

Jun 9th, 2013
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. //better collision?
  2.     int dirX=(xvel>0 ? 1 : (xvel<0 ? -1 : 0));  //move pixel by pixel through multiple iteration
  3.     int dirY=(yvel>0 ? 1 : (yvel<0 ? -1 : 0));
  4.     int a=10;   //a margin of the pixel, basically the border-width.
  5.     int speedX=xvel;
  6.     int speedY=yvel;
  7.     for(int j=0;j<abs(yvel) || j<abs(xvel);j++) //this is done, so when the character goes fast, it doesn't move into the wall (move pixel by picel)
  8.     {
  9.         if(speedX)      //if we still need to move on the x, move
  10.         {
  11.             box.x+=dirX;
  12.             speedX-=dirX;
  13.         }
  14.         if(speedY)  //same with y
  15.         {
  16.             box.y+=dirY;
  17.             speedY-=dirY;
  18.     }
  19.           for(int i = 0; i < map.size(); i++){
  20.               for(int j = start; j < end; j++){
  21.                   if(map[i][j] == 0){
  22.                       continue;
  23.                   }
  24.                   SDL_Rect destrect = {j * 50, i * 50, 50, 50};
  25.                   int err=0;
  26.                   if(collision(&box,&destrect)){
  27.                       noCollision = false;
  28.                      
  29.                      
  30.                       //create the 4 inside rect
  31.                       SDL_Rect top={box.x,box.y,box.w,a};
  32.                       SDL_Rect bottom={box.x,box.y+box.h-a,box.w,a};
  33.                       SDL_Rect left={box.x,box.y+a,a,box.h-2*a};
  34.                       SDL_Rect right={box.x+box.w-a,box.y+a,a,box.h-2*a};
  35.                       SDL_Rect center={box.x+a,box.y+a,box.w-2*a,box.h-2*a};
  36.                      
  37.                       //do the right back movement, if we have collision on the left
  38.                       if(collision(&destrect,&left))
  39.                       {
  40.                         box.x++;
  41.                         setDirection();
  42.                       }else if(collision(&destrect,&right)) //right...
  43.                       {
  44.                         box.x--;
  45.                         setDirection();
  46.                       }
  47.                       else if(collision(&destrect,&top))
  48.                       {
  49.                         box.y++;
  50.                       }
  51.                       else if(collision(&destrect,&bottom))
  52.                       {
  53.                         box.y--;
  54.                         ground=true;
  55.                       }
  56.                       if(collision(&destrect,&center))  //we shouldn't get here, there is a block inside the player, try to move out
  57.                       {
  58.                         box.x--;
  59.                         box.y--;
  60.                       }
  61.                }
  62.             }
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement