Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // collision detection
- // we first check the x-Axis
- // determining the side which faces the moving direction
- if(movement.x > 0)
- {
- int coordFwdFEdgeX;
- if (movement.x < 0)
- {
- coordFwdFEdgeX = player.getPosition().x;
- } else
- {
- coordFwdFEdgeX = player.getPosition().x + playerSize.x;
- }
- if(coordFwdFEdgeX >= 0)
- {
- // with which line(s) of tiles does the player collide?
- std::vector<int> collidingLinesX;
- if(movement.y < 0)
- {
- for(int i = player.getPosition().y; i >= static_cast<int>(player.getPosition().y+movement.y); --i)
- {
- collidingLinesX.push_back(i/5);
- }
- } else if (movement.y == 0)
- {
- collidingLinesX.push_back(player.getPosition().y);
- } else
- {
- for(int i = player.getPosition().y; i <= static_cast<int>(player.getPosition().y+movement.y); ++i)
- {
- collidingLinesX.push_back(i/5);
- }
- }
- // we scan along these lines for obstacles
- int closestObstacleX = 30111995;
- if(movement.x < 0)
- {
- for(uint i = 0; i < collidingLinesX.size(); ++i)
- {
- for(int x =(coordFwdFEdgeX+movement.x)/5; x < (coordFwdFEdgeX/5); ++x)
- {
- if(Play::getInstance()->getPhysicsMap()[collidingLinesX[i]][x] != 0)
- {
- closestObstacleX = x*5;
- }
- }
- }
- if(closestObstacleX != 30111995)
- {
- movement.x = -(coordFwdFEdgeX - closestObstacleX)*deltaTime.asSeconds();
- }
- } else
- {
- for(uint i = 0; i < collidingLinesX.size(); ++i)
- {
- for(int x = (coordFwdFEdgeX+movement.x)/5; x > (coordFwdFEdgeX/5); --x)
- {
- if(Play::getInstance()->getPhysicsMap()[collidingLinesX[i]][x] != 0)
- {
- closestObstacleX = x*5;
- }
- }
- }
- if(closestObstacleX != 30111995)
- {
- movement.x = (closestObstacleX - coordFwdFEdgeX)*deltaTime.asSeconds();
- }
- }
- }
- }
- // now the y-axis
- // determining the side which faces the moving direction
- if(movement.y > 0)
- {
- int coordFwdFEdgeY;
- if(movement.y < 0)
- {
- coordFwdFEdgeY = player.getPosition().y;
- } else
- {
- coordFwdFEdgeY = player.getPosition().y + playerSize.y;
- }
- // we don't want to try to access a negative value by index
- if(coordFwdFEdgeY >= 0)
- {
- // with which line(s) of tiles does the player collide?
- std::vector<int> collidingLinesY;
- if(movement.x < 0)
- {
- for(int i = player.getPosition().x; i >= static_cast<int>(player.getPosition().x+movement.x); --i)
- {
- collidingLinesY.push_back(i);
- }
- } else if(movement.x == 0)
- {
- collidingLinesY.push_back(player.getPosition().x);
- } else
- {
- for(int i = player.getPosition().x; i <= static_cast<int>(player.getPosition().x+movement.x); ++i)
- {
- collidingLinesY.push_back(i);
- }
- }
- // we scan along these lines for obstacles
- int closestObstacleY = 30111995;
- if(movement.y < 0)
- {
- for(int i = 0; i < collidingLinesY.size(); ++i)
- {
- for(int y = static_cast<float>(coordFwdFEdgeY+movement.y)/5; y < static_cast<float>(coordFwdFEdgeY)/5; ++y)
- {
- if(Play::getInstance()->getPhysicsMap()[y][collidingLinesY[i]] != 0)
- {
- closestObstacleY = y*5;
- }
- }
- }
- if(closestObstacleY != 30111995)
- {
- movement.y = (closestObstacleY - coordFwdFEdgeY)*deltaTime.asSeconds();
- }
- } else
- {
- for(int i = 0; i < collidingLinesY.size(); ++i)
- {
- for(int y = static_cast<float>(coordFwdFEdgeY+movement.y)/5; y > static_cast<float>(coordFwdFEdgeY)/5; --y)
- {
- if(Play::getInstance()->getPhysicsMap()[y][collidingLinesY[i]] != 0)
- {
- closestObstacleY = y*5;
- }
- }
- }
- if(closestObstacleY != 30111995)
- {
- movement.y = -(coordFwdFEdgeY - closestObstacleY)*deltaTime.asSeconds();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement