SFML community forums

Help => General => Topic started by: Akasata on July 09, 2018, 10:14:55 pm

Title: Movement related question
Post by: Akasata on July 09, 2018, 10:14:55 pm
Hi. I have a simple question related to moving player.

Casual move function looks like this:

if(sf::Keyboard::isKeyPressed(left)) player.move(-100, 0);
if(sf::Keyboard::isKeyPressed(right)) player.move(100, 0);

When you press LEFT and RIGHT key in the same time player doesn't move at all and it's good. But my problem is that I want it to stay stopped when I release keys in the same time, but it's not. I know that it's related to the fact the it's hard to release to keys in the same time, but I want to know the method to ease it.
Title: Re: Movement related question
Post by: Hapax on July 09, 2018, 11:06:43 pm
You could consider adding a delay to the initial movement to allow both keys to be removed within that time period but that would just give a feeling of lag.

Another option is to implement a system of acceleration instead of instant movement. This would mean that it would gradually increase in speed until it reaches its maximum. This could be fast or slow acceleration. Using this approach means that when you release the first direction, it will start accelerating in the direction still held but won't be as noticeable or jerky as the original version so you have a bit of time to release the second key without it being too bad.
Title: Re: Movement related question
Post by: NGM88 on July 10, 2018, 01:46:58 pm
Could be done with a state machine. Releasing the left and right keys could check whether the other key is pressed and block movement using another boolean. It's a bit of a logic puzzle.