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

Author Topic: Movement related question  (Read 1236 times)

0 Members and 1 Guest are viewing this topic.

Akasata

  • Newbie
  • *
  • Posts: 6
    • View Profile
Movement related question
« 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Movement related question
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Movement related question
« Reply #2 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.

 

anything