SFML community forums

Help => General => Topic started by: Regeta on January 17, 2013, 06:05:57 pm

Title: Diagonal Player Movement - Stop movement facing diagonal direction - 2 keys down
Post by: Regeta on January 17, 2013, 06:05:57 pm
I have googled and search these forums for a solution for 30 minutes and can't seem to find anything. It's not a simple google search given the lack of definitions for the problem.

The player moves in 8 directions, using either the NumPad or ArrowKeys/WASD. The NumPad has no problems given its 8 directions are all separate keys. However, arrow keys and WASD require 2 keys down to move diagonally.

if( (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) && (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) )
    {
~walk walk walk~
    }


Everything works fine, until I want to STOP MOVEMENT while facing a diagonal direction. Input is detected so quickly, that 9 times out of 10 you will end up facing N, E, S, W- because even a faster user cannot release both keys at the exact same time. One key is always released after the other.

I've read a little bit about ONE possible solution: Taking out the logic in the keyboard if's, and replace it with a variable.

I am new to SFML, so I am not sure this is the best way to handle this. I would prefer not to slow down detection of input either, or make overcomplicated code if there is an easier way to fix this.


Thank you- I am sure any help to solve this will help me save hours of time trying to solve it or research the deepest depths of google to find the logic behind this instead of diagonal speed (sin / cos).  The engineering is pretty simple if I remember correctly, which is why I ask for a solution. I actually remember long ago kind of how I handled it in a different framework, but I do not remember enough to get it done quickly.
Title: Re: Diagonal Player Movement - Stop movement facing diagonal direction - 2 keys down
Post by: eXpl0it3r on January 17, 2013, 06:40:08 pm
Well a straight forward solution that came to mind is, that you can introduce a small delay in processing inputs again. So whenever a key gets released make sure to wait for a few milliseconds before you again allow the processing of key inputs.
Title: Re: Diagonal Player Movement - Stop movement facing diagonal direction - 2 keys down
Post by: Nexus on January 17, 2013, 08:44:35 pm
Keep also in mind, that you still have to allow the character to walk diagonally, then to continue horizontally -- without a break in between. So it's more complicated than just stopping at the first key release.
Title: Re: Diagonal Player Movement - Stop movement facing diagonal direction - 2 keys down
Post by: thePyro_13 on January 18, 2013, 05:14:02 am
Try separating the animation choice from the actual input. A possible way to do this is by choosing the animation based on the delta movement from the previous frame(or maybe a fractional second ago).

This way you should still trigger the correct animation even if you move perfectly horizontal or vertical for a single frame.
Title: Re: Diagonal Player Movement - Stop movement facing diagonal direction - 2 keys down
Post by: Regeta on May 25, 2013, 12:49:23 pm
Try separating the animation choice from the actual input. A possible way to do this is by choosing the animation based on the delta movement from the previous frame(or maybe a fractional second ago).

This way you should still trigger the correct animation even if you move perfectly horizontal or vertical for a single frame.

Thank you! I am pretty sure this is exactly how I did it before.

It doesn't lag universal input, which is very important to me. The last thing I want is putting a delay in processing inputs.

For how my brain works, the delta works as a medium between the input and the actual movement.
I believe it was a long time ago in a game engine, where I solved the same problem using this solution. I had forgot how, until your post refreshed my memory.

Thank you again :)