1
General / Re: Tilemap - check collision from sides
« on: January 10, 2017, 10:38:59 pm »
Thanks for reply!
I made something like this. It works with left/right collision and bottom. Top works as well but , now when my player is on the block the yVelocity is 0(m_Tile = true) and that is ok but the player will stay in the same Y position even when i move out of blocks. How can I get it simple to work?
I made something like this. It works with left/right collision and bottom. Top works as well but , now when my player is on the block the yVelocity is 0(m_Tile = true) and that is ok but the player will stay in the same Y position even when i move out of blocks. How can I get it simple to work?
counter = 0;
for(wallit = wallGroup.begin(); wallit != wallGroup.end(); wallit++){
bottom = wallGroup[counter].getPosition().y + 64;
top = wallGroup[counter].getPosition().y;
right = wallGroup[counter].getPosition().x + 64;
left = wallGroup[counter].getPosition().x;
if(player.getBounds().intersects(wallGroup[counter].getGlobalBounds())){
if(!player.m_onTile){
if(player.m_velocity.x > 0 && player.getPosition().x < right){
player.setPosition(player.getPosition().x - 1, player.getPosition().y);
} else if(player.m_velocity.x < 0 && player.getPosition().x > right){
player.setPosition(player.getPosition().x + 1, player.getPosition().y);
}
//top collision
else if(player.m_velocity.y > 0 && player.getPosition().y < bottom){
player.m_onTile = true;
}
else if(player.m_velocity.y < 0 && player.getPosition().y > bottom){
player.setPosition(player.getPosition().x,player.getPosition().y + 2);
}
} else {
}
}else {
}
counter++;
}
for(wallit = wallGroup.begin(); wallit != wallGroup.end(); wallit++){
bottom = wallGroup[counter].getPosition().y + 64;
top = wallGroup[counter].getPosition().y;
right = wallGroup[counter].getPosition().x + 64;
left = wallGroup[counter].getPosition().x;
if(player.getBounds().intersects(wallGroup[counter].getGlobalBounds())){
if(!player.m_onTile){
if(player.m_velocity.x > 0 && player.getPosition().x < right){
player.setPosition(player.getPosition().x - 1, player.getPosition().y);
} else if(player.m_velocity.x < 0 && player.getPosition().x > right){
player.setPosition(player.getPosition().x + 1, player.getPosition().y);
}
//top collision
else if(player.m_velocity.y > 0 && player.getPosition().y < bottom){
player.m_onTile = true;
}
else if(player.m_velocity.y < 0 && player.getPosition().y > bottom){
player.setPosition(player.getPosition().x,player.getPosition().y + 2);
}
} else {
}
}else {
}
counter++;
}