1
General / trying to get movement and collision working smoothly
« on: February 26, 2015, 05:34:07 am »
for the last while I have been trying to get my program to slide along walls. so if there is a vertical wall and I am pressing up and right to slide up(which I can get for one direction but when there is a horizontal wall and I am pressing up and right I go right through not slide to the right.
is there a easy way to get the other direction to detect and allow me to slide left or right, cause as it stands now I will slide up or down
the player.update()
is there a easy way to get the other direction
sf::Vector2f tempLocation(player.getPosition().x, player.getPosition().y);
player.update(elapsedTime);
sf::Vector2f movedLocation(player.getPosition().x, player.getPosition().y);
for(unsigned int i = 0; i < colMap.size(); i++)
{
for(unsigned int j = 0; j < colMap[i].size(); j++)
{
if (colMap[i][j] == 1)
{
float Bottom, Top, Left, Right;
Bottom = i * tileSize + tileSize;
Top = i * tileSize;
Right = j * tileSize + tileSize;
Left = j * tileSize;
float pBottom, pTop, pRight, pLeft;
pBottom = player.getPosition().y + (player.getRadius() * 2);
pTop = player.getPosition().y;
pLeft = player.getPosition().x;
pRight = player.getPosition().x + (player.getRadius() * 2);
if(pLeft < Right && pRight > Left &&
pTop < Bottom && pBottom > Top)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
player.setPosition(tempLocation.x, movedLocation.y);
}
}
}
}
}
player.update(elapsedTime);
sf::Vector2f movedLocation(player.getPosition().x, player.getPosition().y);
for(unsigned int i = 0; i < colMap.size(); i++)
{
for(unsigned int j = 0; j < colMap[i].size(); j++)
{
if (colMap[i][j] == 1)
{
float Bottom, Top, Left, Right;
Bottom = i * tileSize + tileSize;
Top = i * tileSize;
Right = j * tileSize + tileSize;
Left = j * tileSize;
float pBottom, pTop, pRight, pLeft;
pBottom = player.getPosition().y + (player.getRadius() * 2);
pTop = player.getPosition().y;
pLeft = player.getPosition().x;
pRight = player.getPosition().x + (player.getRadius() * 2);
if(pLeft < Right && pRight > Left &&
pTop < Bottom && pBottom > Top)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
player.setPosition(tempLocation.x, movedLocation.y);
}
}
}
}
}
is there a easy way to get the other direction to detect and allow me to slide left or right, cause as it stands now I will slide up or down
the player.update()
void Player::update(const sf::Time& elapsedTime)
{
// Move the player
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
circle.move(-moveSpeed * elapsedTime.asSeconds(), 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
circle.move(moveSpeed * elapsedTime.asSeconds(), 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
circle.move(0, -moveSpeed * elapsedTime.asSeconds());
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
circle.move(0, moveSpeed * elapsedTime.asSeconds());
}
{
// Move the player
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
circle.move(-moveSpeed * elapsedTime.asSeconds(), 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
circle.move(moveSpeed * elapsedTime.asSeconds(), 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
circle.move(0, -moveSpeed * elapsedTime.asSeconds());
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
circle.move(0, moveSpeed * elapsedTime.asSeconds());
}
is there a easy way to get the other direction