Hello, been a while since I've been able to use SFML for stuff, feels good to use it again, anyway:
The program I started working on yesterday basically makes 100 random rectangles that act as "rooms", creating random passages between the rooms each time the program is run. The player can navigate through these paths.
I have basic object collision set up, but my problem is determining the best way to go about letting the player move after hitting a vertical or horizontal wall.
The important code is here:
...
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::W)
up = true;
else if (event.key.code == sf::Keyboard::A)
left = true;
else if (event.key.code == sf::Keyboard::S)
down = true;
else if (event.key.code == sf::Keyboard::D)
right = true;
break;
case sf::Event::KeyReleased:
if (event.key.code == sf::Keyboard::W)
up = false;
else if (event.key.code == sf::Keyboard::A)
left = false;
else if (event.key.code == sf::Keyboard::S)
down = false;
else if (event.key.code == sf::Keyboard::D)
right = false;
break;
}
}
//////////////////
/// Movement ///
//////////////////
last_x = player.getPosition().x;
last_y = player.getPosition().y;
if (right == true && left == false) // D
player.move(SPEED, 0);
else if (right == false && left == true) // A
player.move(-SPEED, 0);
if (up == true && down == false) // W
player.move(0, -SPEED);
else if (up == false && down == true) // S
player.move(0, SPEED);
for (int i = 0; i < NUM_ROOMS; i++)
{
if (player.getGlobalBounds().intersects(rooms[i].rectangle.getGlobalBounds()))
{
player.setPosition(last_x, last_y);
break;
}
}
...
The problem I have right now is that if the player uses keys to go diagonal (ex:, the W+D keys, WASD keys) while against a vertical wall, it prevents the player from moving up/down. Same situation with horizontal walls, it prevents the player from moving left/right while pressing (S+D) or (A+S). The method I'm currently using it keeps track of the last position of the player before hitting the wall, and if it collides, the player goes back to the position it was at before.
Most other basic games I've played allow the player to still move even if they are technically going diagonal while against a wall; it simply makes the player go down instead. I hope that makes sense, please tell me if it doesn't.
Here is a link to the program I made:
http://www.sendspace.com/file/od07v5If you don't trust opening that file, please feel free to compile the code yourself:
http://pastebin.com/fK0QePp7You are the red square. Uses WASD keys by default to move.
If you play it and try to move diagonally inward to a wall, you'll see you can't move at all. I would appreiciate it very much if someone could guide me on how to fix this.
EDIT:I got it to work as I wanted it to by splitting it into two For loops
between x and y movement, although I doubt this is the most efficient method, probably quite inefficient, but "it works".
last_x = player.getPosition().x;
last_y = player.getPosition().y;
if (right == true && left == false) // D
player.move(SPEED, 0); //make it an elapsed time "time-step"?
else if (right == false && left == true) // A
player.move(-SPEED, 0);
for (int i = 0; i < NUM_ROOMS; i++)
{
if (player.getGlobalBounds().intersects(rooms[i].rectangle.getGlobalBounds()))
{
player.setPosition(last_x, player.getPosition().y);
break;
}
}
if (up == true && down == false) // W
player.move(0, -SPEED);
else if (up == false && down == true) // S
player.move(0, SPEED);
for (int i = 0; i < NUM_ROOMS; i++)
{
if (player.getGlobalBounds().intersects(rooms[i].rectangle.getGlobalBounds()))
{
player.setPosition(player.getPosition().x, last_y);
break;
}
}
So, I guess this is solved as far as I can tell, but if you have a suggestion as to a better way to let you "slide" against the wall while going diagonal (or movement in general), please reply!