Hello! I am looking for a solution to have my sprite rotate correctly when I am going in a diagonal direction (holding up key and right key, down key and left key, etc.). I have a ship sprite the initially faces right, when I hit a single arrow key it will rotate appropriately to face that direction, but I can't find a way to get the diagonals to work.. I have tried an if statement with up key and right key then some logic to turn it, but it doesn't quite work. Here is my code for my ship's movement:
//Character movement
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
if(ship.getRotation() != 315){
if(ship.getRotation() > 135){
ship.rotate(2);
}
if(ship.getRotation() <= 135 || (ship.getRotation() >= 0 && ship.getRotation() < 315)){
ship.rotate(-2);
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
if(ship.getRotation() != 270){
if((ship.getRotation() > 270) || (ship.getRotation() <= 90)){
ship.rotate(-2);
}
if((ship.getRotation() < 270) && (ship.getRotation() > 90)){
ship.rotate(2);
}
}
ship.move(sf::Vector2f(0,-speed[2]));
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
if(ship.getRotation() != 90){
if((ship.getRotation() > 90) && (ship.getRotation() <= 180)){
ship.rotate(-2);
}
if((ship.getRotation() < 90) || (ship.getRotation() > 180)){
ship.rotate(2);
}
}
ship.move(sf::Vector2f(0,speed[2]));
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
if(ship.getRotation() != 180){
if(ship.getRotation() > 180){
ship.rotate(-2);
}
if(ship.getRotation() < 180){
ship.rotate(2);
}
}
ship.move(sf::Vector2f(-speed[2],0));
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
if(ship.getRotation() != 0){
if((ship.getRotation() > 0) && (ship.getRotation() < 180)){
ship.rotate(-2);
}
if(ship.getRotation() >= 180){
ship.rotate(2);
}
}
ship.move(sf::Vector2f(speed[2],0));
}
I have a feeling it is clashing with one of the other if statements, any suggestions on this?