2
\$\begingroup\$

Example scenario:

Assume 2 dimensional top down movement system.

Given that I have 4 directional keys ( UP / DOWN / LEFT / RIGHT ), I am able to move in 8 different directions 4 perpendicular and 4 diagonal.

As long as a directional key is pressed there exists an assumed movement, if all directional keys are released then movement is assumed to have stopped.

If I have an event driven input system in place (instead of polling for key states every frame there is an observer which receives and acts upon events based on which keys are pressed and released) is it possible to stop movement in a diagonal direction given that the events received will be sequential?

In a polling driven input system, as long as I would be able to release both keys fast enough for them both to register as released at the same time on the next frame there would be no need to change the direction and the movement would conclude in a diagonal state.

\$\endgroup\$
3
  • \$\begingroup\$ You don't want to allow diagonal movement? It's not clear to me what's stopping your from doing that. \$\endgroup\$
    – House
    Commented Aug 16, 2016 at 19:31
  • \$\begingroup\$ @Byte56 Given that I have 8 directions an entity can face, I want to be also able to have the entity face diagonally when it stops moving. If I have event driven input, one of the key releases will come before the other effectively setting the direction of the entity to a perpendicular state (i.e. north/east/south/west) before receiving the second release event which will trigger the halt of movement. In effect the entity will always face either up, left, right or down but never in any of the diagonal directions. And I was wondering if its possible for it to do so in some way. \$\endgroup\$ Commented Aug 16, 2016 at 19:57
  • \$\begingroup\$ Polling event systems can also suffer from this problem, if the polling rate is so high that it's very hard for humans to release both keys within the polling period. \$\endgroup\$ Commented Sep 16, 2016 at 1:19

1 Answer 1

3
\$\begingroup\$

If you want this kind of interaction, you'll have to introduce some sort of delay into your controls because it is very unlikely that the player will be able to release both keys in the same frame. That's most likely 16-33ms depending on your frame rate.

If you add the delay (maybe just on release and on diagonals as a special case), your direction won't change when releasing a key off a diagonal movement for a small margin of time. If both keys are released in that margin, your movement would stop in the original diagonal direction.

A lot of games will have some key press/release queue with a generous delay for diagonal controls. A good example is dashing on the diagonal by double-pressing two arrow keys.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .