joker546645

SFML - Collision Deep

Mar 4th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. if (body.getGlobalBounds ( ).intersects ( other.sprite.getGlobalBounds() ))
  2.     {
  3.         sf::Vector2f otherPosition = other.pos;
  4.         sf::Vector2f otherHalfSize = other.sprite.getSize ( ) / 2.0f;
  5.         sf::Vector2f thisPosition = GetPosition ( );
  6.         sf::Vector2f thisHalfSize = GetHalfSize ( );
  7.        
  8.         float deltaX = thisPosition.x - (otherPosition.x + otherHalfSize.x);
  9.         float deltaY = thisPosition.y - (otherPosition.y + otherHalfSize.y);
  10.         float differenceX = otherHalfSize.x + thisHalfSize.x;
  11.         float differenceY = otherHalfSize.y + thisHalfSize.y;
  12.        
  13.         float deepX = differenceX - abs ( deltaX );
  14.         float deepY = differenceY - abs ( deltaY );
  15.  
  16.         if (deepX < deepY && deltaX > 0)  // Prawa Kolizja
  17.         {
  18.             other.pos = sf::Vector2f ( thisPosition.x - thisHalfSize.x - otherHalfSize.x - otherHalfSize.x, other.pos.y );
  19.             std::cout << "Prawa!" << std::endl;
  20.         }
  21.         else if (deepX < deepY && deltaX < 0)  // Lewa Kolizja
  22.         {
  23.             other.pos = sf::Vector2f ( thisPosition.x + thisHalfSize.x, other.pos.y );
  24.             std::cout << "Left!" << std::endl;
  25.         }
  26.         else if (deepX > deepY && deltaY > 0)  // Dolna Kolizja
  27.         {
  28.             other.pos = sf::Vector2f ( other.pos.x, thisPosition.y - thisHalfSize.y - otherHalfSize.y - otherHalfSize.y );
  29.             std::cout << "Low!" << std::endl;
  30.         }
  31.         else if (deepX > deepY && deltaY < 0)  // Górna Kolizja
  32.         {
  33.             other.pos = sf::Vector2f ( other.pos.x, thisPosition.y + thisHalfSize.y );
  34.             other.dir.y = 1.0f;
  35.             std::cout << "Up!" << std::endl;
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment