Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (body.getGlobalBounds ( ).intersects ( other.sprite.getGlobalBounds() ))
- {
- sf::Vector2f otherPosition = other.pos;
- sf::Vector2f otherHalfSize = other.sprite.getSize ( ) / 2.0f;
- sf::Vector2f thisPosition = GetPosition ( );
- sf::Vector2f thisHalfSize = GetHalfSize ( );
- float deltaX = thisPosition.x - (otherPosition.x + otherHalfSize.x);
- float deltaY = thisPosition.y - (otherPosition.y + otherHalfSize.y);
- float differenceX = otherHalfSize.x + thisHalfSize.x;
- float differenceY = otherHalfSize.y + thisHalfSize.y;
- float deepX = differenceX - abs ( deltaX );
- float deepY = differenceY - abs ( deltaY );
- if (deepX < deepY && deltaX > 0) // Prawa Kolizja
- {
- other.pos = sf::Vector2f ( thisPosition.x - thisHalfSize.x - otherHalfSize.x - otherHalfSize.x, other.pos.y );
- std::cout << "Prawa!" << std::endl;
- }
- else if (deepX < deepY && deltaX < 0) // Lewa Kolizja
- {
- other.pos = sf::Vector2f ( thisPosition.x + thisHalfSize.x, other.pos.y );
- std::cout << "Left!" << std::endl;
- }
- else if (deepX > deepY && deltaY > 0) // Dolna Kolizja
- {
- other.pos = sf::Vector2f ( other.pos.x, thisPosition.y - thisHalfSize.y - otherHalfSize.y - otherHalfSize.y );
- std::cout << "Low!" << std::endl;
- }
- else if (deepX > deepY && deltaY < 0) // Górna Kolizja
- {
- other.pos = sf::Vector2f ( other.pos.x, thisPosition.y + thisHalfSize.y );
- other.dir.y = 1.0f;
- std::cout << "Up!" << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment