tutaj link ---> film co sie dzieje
hej, mam taką funkcje do sprawdzania kolizji i generalnie dziala dobrze, ale jesli najezdzam postacia na platforme od gory albo od dolu i cala postac nie znajduje sie jakby w szerokości platformy, to przerzuca mi ja na bok zamiast zostawic na platformie. ktos wie jak to naprawić?
void Player::checkCollisions(std::vector<Obstacle> obstacles)
{
sf::Vector2f playerPos = hitbox.getPosition();
for (auto& obs : obstacles) {
sf::FloatRect playerBounds = hitbox.getGlobalBounds();
sf::FloatRect wallBounds = obs.getObstacle().getGlobalBounds();
nextPosition = playerBounds;
nextPosition.left += velocity.x;
nextPosition.top += velocity.y;
if (wallBounds.intersects(nextPosition)) {
//Bottom collision
if (playerBounds.top < wallBounds.top
&& playerBounds.top + playerBounds.height < wallBounds.top + wallBounds.height
&& playerBounds.left < wallBounds.left + wallBounds.width
&& playerBounds.left + playerBounds.width > wallBounds.left
)
{
this->isPressed = 0;
velocity.y = 0.f;
player.setPosition(playerBounds.left, wallBounds.top - playerBounds.height);
hitbox.setPosition(playerBounds.left, wallBounds.top - playerBounds.height);
}
//Top collision
else if (playerBounds.top > wallBounds.top
&& playerBounds.top + playerBounds.height > wallBounds.top + wallBounds.height
&& playerBounds.left < wallBounds.left + wallBounds.width
&& playerBounds.left + playerBounds.width > wallBounds.left
)
{
velocity.y = 0.f;
player.setPosition(playerBounds.left, wallBounds.top + wallBounds.height);
hitbox.setPosition(playerBounds.left, wallBounds.top + wallBounds.height);
}
//Right collision
if (playerBounds.left < wallBounds.left
&& playerBounds.left + playerBounds.width < wallBounds.left + wallBounds.width
&& playerBounds.top < wallBounds.top + wallBounds.height
&& playerBounds.top + playerBounds.height > wallBounds.top
)
{
velocity.x = 0.f;
player.setPosition(wallBounds.left - playerBounds.width, playerBounds.top);
hitbox.setPosition(wallBounds.left - playerBounds.width, playerBounds.top);
}
//Left collision
else if (playerBounds.left > wallBounds.left
&& playerBounds.left + playerBounds.width > wallBounds.left + wallBounds.width
&& playerBounds.top < wallBounds.top + wallBounds.height
&& playerBounds.top + playerBounds.height > wallBounds.top
)
{
velocity.x = 0.f;
player.setPosition(wallBounds.left + wallBounds.width, playerBounds.top);
hitbox.setPosition(wallBounds.left + wallBounds.width, playerBounds.top);
}
}
}
}