1
I have a collision map with 0 for no collision and 1 for collision I go through the file find 1 and get its position such as
0 1 0
top = 0
bottom = 32
left = 32
right = 64
for ( int i = 0; i < mapCollision.size(); i++ )
{
for ( int j = 0; j < mapCollision.size(); j++ )
{
if ( mapCollision[j] == 1)
{
sf::Vector2i findSquarePosition = sf::Vector2i ( 1 * j, 1 * i );
int topOfTile, bottomOfTile, leftOfTile, rightOfTile;
topOfTile = findSquarePosition.y * 32;
bottomOfTile = findSquarePosition.y * 32 + 32;
leftOfTile = findSquarePosition.x * 32;
rightOfTile = findSquarePosition.x * 32 + 32;
int playerTopOfBox, playerBottomOfBox, playerLeftOfBox, playerRightOfBox;
playerTopOfBox = player.getPosition().y;
playerBottomOfBox = player.getPosition().y + player.getSize().y;
playerLeftOfBox = player.getPosition().x;
playerRightOfBox = player.getPosition().x + player.getSize().x;
if ( playerRightOfBox < leftOfTile || playerLeftOfBox > rightOfTile || playerTopOfBox > bottomOfTile || playerBottomOfBox < topOfTile )
{
}
else
{
if ( playerTopOfBox < bottomOfTile )
{
cout << "Collision!" << endl;
}
}
}
}
}
issue I am having is that I hit the block however if I move over to go around it I get blocked also.
I am trying to make a collision detection that knows if I hit the left side of the block or top or bottom or right and pushing away accordingly to that detection.
Say my left side of my player hits the right wall I want to push him right
Say my right side of my player hits the left wall I want to push him left
Can anyone help thank you!
0 1 0
top = 0
bottom = 32
left = 32
right = 64
for ( int i = 0; i < mapCollision.size(); i++ )
{
for ( int j = 0; j < mapCollision.size(); j++ )
{
if ( mapCollision[j] == 1)
{
sf::Vector2i findSquarePosition = sf::Vector2i ( 1 * j, 1 * i );
int topOfTile, bottomOfTile, leftOfTile, rightOfTile;
topOfTile = findSquarePosition.y * 32;
bottomOfTile = findSquarePosition.y * 32 + 32;
leftOfTile = findSquarePosition.x * 32;
rightOfTile = findSquarePosition.x * 32 + 32;
int playerTopOfBox, playerBottomOfBox, playerLeftOfBox, playerRightOfBox;
playerTopOfBox = player.getPosition().y;
playerBottomOfBox = player.getPosition().y + player.getSize().y;
playerLeftOfBox = player.getPosition().x;
playerRightOfBox = player.getPosition().x + player.getSize().x;
if ( playerRightOfBox < leftOfTile || playerLeftOfBox > rightOfTile || playerTopOfBox > bottomOfTile || playerBottomOfBox < topOfTile )
{
}
else
{
if ( playerTopOfBox < bottomOfTile )
{
cout << "Collision!" << endl;
}
}
}
}
}
issue I am having is that I hit the block however if I move over to go around it I get blocked also.
I am trying to make a collision detection that knows if I hit the left side of the block or top or bottom or right and pushing away accordingly to that detection.
Say my left side of my player hits the right wall I want to push him right
Say my right side of my player hits the left wall I want to push him left
Can anyone help thank you!