Advertisement
Guest User

manuel

a guest
Feb 1st, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // collision detection
  2.     // we first check the x-Axis
  3.     // determining the side which faces the moving direction
  4.     if(movement.x > 0)
  5.     {
  6.         int coordFwdFEdgeX;
  7.         if (movement.x < 0)
  8.         {
  9.             coordFwdFEdgeX = player.getPosition().x;
  10.         } else
  11.         {
  12.             coordFwdFEdgeX = player.getPosition().x + playerSize.x;
  13.         }
  14.         if(coordFwdFEdgeX >= 0)
  15.         {
  16.             // with which line(s) of tiles does the player collide?
  17.             std::vector<int> collidingLinesX;
  18.             if(movement.y < 0)
  19.             {
  20.                 for(int i = player.getPosition().y; i >= static_cast<int>(player.getPosition().y+movement.y); --i)
  21.                 {
  22.                     collidingLinesX.push_back(i/5);
  23.                 }
  24.             } else if (movement.y == 0)
  25.             {
  26.                 collidingLinesX.push_back(player.getPosition().y);
  27.             } else
  28.             {
  29.                 for(int i = player.getPosition().y; i <= static_cast<int>(player.getPosition().y+movement.y); ++i)
  30.                 {
  31.                     collidingLinesX.push_back(i/5);
  32.                 }
  33.             }
  34.             // we scan along these lines for obstacles
  35.             int closestObstacleX = 30111995;
  36.             if(movement.x < 0)
  37.             {
  38.                 for(uint i = 0; i < collidingLinesX.size(); ++i)
  39.                 {
  40.                     for(int x =(coordFwdFEdgeX+movement.x)/5; x < (coordFwdFEdgeX/5); ++x)
  41.                     {
  42.                         if(Play::getInstance()->getPhysicsMap()[collidingLinesX[i]][x] != 0)
  43.                         {
  44.                             closestObstacleX = x*5;
  45.                         }
  46.                     }
  47.                 }
  48.                 if(closestObstacleX != 30111995)
  49.                 {
  50.                     movement.x = -(coordFwdFEdgeX - closestObstacleX)*deltaTime.asSeconds();
  51.                 }
  52.             } else
  53.             {
  54.                 for(uint i = 0; i < collidingLinesX.size(); ++i)
  55.                 {
  56.                     for(int x = (coordFwdFEdgeX+movement.x)/5; x > (coordFwdFEdgeX/5); --x)
  57.                     {
  58.                         if(Play::getInstance()->getPhysicsMap()[collidingLinesX[i]][x] != 0)
  59.                         {
  60.                             closestObstacleX = x*5;
  61.                         }
  62.                     }
  63.                 }
  64.                 if(closestObstacleX != 30111995)
  65.                 {
  66.                     movement.x = (closestObstacleX - coordFwdFEdgeX)*deltaTime.asSeconds();
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     // now the y-axis
  72.     // determining the side which faces the moving direction
  73.     if(movement.y > 0)
  74.     {
  75.         int coordFwdFEdgeY;
  76.         if(movement.y < 0)
  77.         {
  78.             coordFwdFEdgeY = player.getPosition().y;
  79.         } else
  80.         {
  81.             coordFwdFEdgeY = player.getPosition().y + playerSize.y;
  82.         }
  83.         // we don't want to try to access a negative value by index
  84.         if(coordFwdFEdgeY >= 0)
  85.         {
  86.             // with which line(s) of tiles does the player collide?
  87.             std::vector<int> collidingLinesY;
  88.             if(movement.x < 0)
  89.             {
  90.                 for(int i = player.getPosition().x; i >= static_cast<int>(player.getPosition().x+movement.x); --i)
  91.                 {
  92.                     collidingLinesY.push_back(i);
  93.                 }
  94.             } else if(movement.x == 0)
  95.             {
  96.                 collidingLinesY.push_back(player.getPosition().x);
  97.             } else
  98.             {
  99.                 for(int i = player.getPosition().x; i <= static_cast<int>(player.getPosition().x+movement.x); ++i)
  100.                 {
  101.                     collidingLinesY.push_back(i);
  102.                 }
  103.             }
  104.             // we scan along these lines for obstacles
  105.             int closestObstacleY = 30111995;
  106.             if(movement.y < 0)
  107.             {
  108.                 for(int i = 0; i < collidingLinesY.size(); ++i)
  109.                 {
  110.                     for(int y = static_cast<float>(coordFwdFEdgeY+movement.y)/5; y < static_cast<float>(coordFwdFEdgeY)/5; ++y)
  111.                     {
  112.                         if(Play::getInstance()->getPhysicsMap()[y][collidingLinesY[i]] != 0)
  113.                         {
  114.                             closestObstacleY = y*5;
  115.                         }
  116.                     }
  117.                 }
  118.                 if(closestObstacleY != 30111995)
  119.                 {
  120.                     movement.y = (closestObstacleY - coordFwdFEdgeY)*deltaTime.asSeconds();
  121.                 }
  122.             } else
  123.             {
  124.                 for(int i = 0; i < collidingLinesY.size(); ++i)
  125.                 {
  126.                     for(int y = static_cast<float>(coordFwdFEdgeY+movement.y)/5; y > static_cast<float>(coordFwdFEdgeY)/5; --y)
  127.                     {
  128.                         if(Play::getInstance()->getPhysicsMap()[y][collidingLinesY[i]] != 0)
  129.                         {
  130.                             closestObstacleY = y*5;
  131.                         }
  132.                     }
  133.                 }
  134.                 if(closestObstacleY != 30111995)
  135.                 {
  136.                     movement.y = -(coordFwdFEdgeY - closestObstacleY)*deltaTime.asSeconds();
  137.                 }
  138.             }
  139.         }
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement