Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Diagonal Player Movement - Stop movement facing diagonal direction - 2 keys down  (Read 4033 times)

0 Members and 1 Guest are viewing this topic.

Regeta

  • Newbie
  • *
  • Posts: 6
    • View Profile
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.
« Last Edit: January 17, 2013, 06:07:42 pm by Regeta »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
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.

Regeta

  • Newbie
  • *
  • Posts: 6
    • View Profile
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 :)