Advertisement
Aslai

Collision Code

Jul 29th, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. bool player::resolveCollision( double x, double y, bool movedX, bool movedY, bool bumped )
  2. {
  3.     int X = floor(x), Y = floor(y);
  4.     int X2 = floor(_x), Y2 = floor(_y);
  5.     bool XE = (X == x || X2 == _x), YE = (Y == y);
  6.     bool hasChanged = false;
  7.  
  8.     bool cols[3][3];
  9.     bool cols2[3][3];
  10.     bool cols3[3][3];
  11.     bool cols4[3][3];
  12.    
  13.     for( int i = 0; i < 3; i ++ )for( int j = 0; j < 3; j ++ )cols[j][i] = ((map*)MyMap)->getBlockSolid( X - 1 + j, Y - 1 + i );
  14.     for( int i = 0; i < 3; i ++ )for( int j = 0; j < 3; j ++ )cols2[j][i] = ((map*)MyMap)->getBlockSolid( X2 - 1 + j, Y - 1 + i );
  15.     for( int i = 0; i < 3; i ++ )for( int j = 0; j < 3; j ++ )cols4[j][i] = ((map*)MyMap)->getBlockSolid( X - 1 + j, Y2 - 1 + i );
  16.     for( int i = 0; i < 3; i ++ )for( int j = 0; j < 3; j ++ )cols3[j][i] = ((map*)MyMap)->getBlockSolid( X2 - 1 + j, Y2 - 1 + i );
  17.    
  18.     if( !bumped ){
  19.         if( _vspeed > 0 ){
  20.             if( cols[0][2] && cols[2][2] && !cols[1][2] && cols2[1][2] && !XE && _hspeed > 0 ) { _x = X; _hspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true;  }
  21.             else if( cols2[0][2] && cols2[2][2] && !cols2[1][2] && cols[1][2] && !XE && _hspeed < 0 ) { _x = X2; _hspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true; }
  22.         }
  23.         else if( _vspeed < 0 ){
  24.             if( cols[0][1] && cols[2][1] && !cols[1][1] && cols2[1][1] && !XE && _hspeed > 0 )  { _x = X; _hspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true; }
  25.             else if( cols2[0][1] && cols2[2][1] && !cols2[1][1] && cols[1][1] && !XE && _hspeed < 0 ) { _x = X2; _hspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true; }
  26.         }
  27.         if( _hspeed < 0 ){
  28.             if( cols[1][0] && cols[1][2] && !cols[1][1] && cols4[1][1] && !YE && _vspeed > 0 ) { _y = Y; _vspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true; }
  29.             else if( cols3[0][0] && cols3[0][2] && !cols3[0][1] && cols[1][1] && !YE && _vspeed < 0 ) { _y = Y2; _vspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true;  }
  30.         }
  31.         if( _hspeed > 0 ){
  32.             if( cols[2][0] && cols[2][2] && !cols[2][1] && cols4[2][1] && !YE && _vspeed > 0 ) { _y = Y; _vspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true; }
  33.             else if( cols3[2][0] && cols3[2][2] && !cols3[2][1] && cols[2][1] && !YE && _vspeed < 0 ) { _y = Y2; _vspeed = 0; resolveCollision( x, _y, movedX, movedY, true ); return true;  }
  34.         }
  35.     }
  36.     if( !movedY ){
  37.         if( ( cols2[1][2] || ( cols2[2][2] && !XE ) ) && _vspeed >= 0 && _y <= Y ) { _y = Y; _vspeed = 0; hasChanged = true; if( gravdir == G_DOWN ) onGround = true; resolveCollision( x, _y, movedX, true ); return true; }
  38.         else if( gravdir == G_DOWN ) { onGround = false;}
  39.         if( ( cols2[1][1] || ( cols2[2][1] && !XE ) ) && _vspeed < 0 && _y >= Y ) { _y = Y2; _vspeed = 0; if( gravdir == G_UP ) onGround = true; if( _hspeed < 0 && !cols3[0][1] ){ _x += _hspeed/10.0; } hasChanged = true; resolveCollision( x, _y, movedX, true ); return true; }
  40.         else if( gravdir == G_UP ) { onGround = false;}
  41.     }
  42.     if( !movedX ){
  43.         if( ( cols[1][1] || ( cols[1][2] && !YE ) ) && _hspeed < 0 ) { _x = X + 1; _hspeed = 0; if( gravdir == G_LEFT ) onGround = true; resolveCollision( _x, _y, true, movedY ); return true; }
  44.         else if( gravdir == G_LEFT ) { onGround = false;}
  45.         if( ( cols[2][1] || ( cols[2][2] && !YE ) ) && _hspeed > 0 ) { _x = X; _hspeed = 0; if( gravdir == G_RIGHT ) onGround = true; resolveCollision( _x, _y, true, movedY ); return true; }
  46.         else if( gravdir == G_RIGHT ) { onGround = false;}
  47.     }
  48.     return false;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement