Hello,
I have a collision question, the following code works, but when the player come the wall closer, the wall attracts the player like a magnet, can someone tell me how to fix it?
for (auto& wall : walls){
FloatRect wallBounds = wall.getGlobalBounds();
FloatRect playerBounds = player.getGlobalBounds();
nextPos = playerBounds;
nextPos.left += Vec.x ;
nextPos.top += Vec.y;
if(wallBounds.intersects(nextPos)){
//Bottom Collision
if (Vec.y > 0 && playerBounds.left < wallBounds.left + wallBounds.width && playerBounds.left + playerBounds.width > wallBounds.left )
{
Vec.y = 0;
player.setPosition(playerBounds.left, wallBounds.top - playerBounds.height);
}
//Top Collision
if(Vec.y < 0 && playerBounds.left < wallBounds.left + wallBounds.width && playerBounds.left + playerBounds.width > wallBounds.left)
{
Vec.y = 0;
player.setPosition(playerBounds.left, wallBounds.top + wallBounds.height);
}
//Right Collision
if(Vec.x > 0 && playerBounds.top < wallBounds.top + wallBounds.height && playerBounds.top + playerBounds.height > wallBounds.top)
{
Vec.x = 0;
player.setPosition(wallBounds.left - playerBounds.width, playerBounds.top);
}
//Left Collision
if (Vec.x < 0 && playerBounds.top < wallBounds.top + wallBounds.height && playerBounds.top + playerBounds.height > wallBounds.top)
{
Vec.x = 0;
player.setPosition(wallBounds.left + wallBounds.width , playerBounds.top);
}
}