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

Author Topic: Checking if keys aren't being pressed  (Read 1877 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Checking if keys aren't being pressed
« on: February 06, 2013, 11:56:59 pm »
Hi there, I'm trying to implement inertia in my game (character speeds up after a certain amount of time). I'm trying to make a if statement that checks to see if any of the following keys are being pressed: W, A, S, D. If none are being pressed, restart the clock that measures the amount of time the player has been moving.

    if (!sf::Keyboard::isKeyPressed(sf::Keyboard::W) &&
        !sf::Keyboard::isKeyPressed(sf::Keyboard::A) &&
        !sf::Keyboard::isKeyPressed(sf::Keyboard::S) &&
        !sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        std::cout << "The user is pressing a key." << std::endl;
        inertiaClock.restart();
    }

Whenever a move function that houses this if statement gets called, the if statement isn't even executed. It automatically goes that fastest it can in this function, which would be an inertia multiplier of 5

void Player::addInertia()
{
    if (inertiaClock.getElapsedTime().asSeconds() <= 1.0f)
        inertiaValue = 1.0f;
    else if (inertiaClock.getElapsedTime().asSeconds() >= 1.0f)
        inertiaValue = 3;
    else
        inertiaValue = 5; // Speed automatically goes here
}
Any help is appreciated, thanks
Current Projects:
Technoport

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Checking if keys aren't being pressed
« Reply #1 on: February 07, 2013, 08:15:12 am »
if (time <= 1)
    ...
else if (time >= 1)
   ...
else
   ...

With which time values is the last else supposed to be triggered? I'm not sure that I understand your problem, but one thing is sure, the code inertiaValue = 5 is impossible to reach.
Laurent Gomila - SFML developer

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: Checking if keys aren't being pressed
« Reply #2 on: February 15, 2013, 06:14:29 am »
basic idea behind inertia is to store a Vector2 of your 'Velocity' and a Vector2 of your current direction, every game loop, edit only your current direction vector, then add it to your velocity vector (Velocity += CurDirection) then, move your character by your velocity value(make sure to set a terminal velocity limit to avoid over speeding), where you got confused, is that when you apply your velocity to your character, you multiply the temporary value by the time

if you like looking at code more than reading (wrote this in the comment box, might not compile, but the idea is still there)
sf::Clock Timer;
Character Chary;
{// main loop

   // get the time between a frame for use later on
   float ElapsedTime = Timer.restart().asSeconds();

   sf::Vector2<float> CurDirection;// a new CurDirection every time
   // get wasd movements, and add them to CurDirection (which is now 0,0)

   //SetVelocity and GetVelocity edit Character's velocity value
   Chary.SetVelocity(Chary.GetVelocity() + (CurDirection*ElapsedTime) );
   Chary.Move(Chary.GetVelocty());
}
 


Oh, and almost forgot, don't forget to make a drag value(float), and scale the vector by a negative of it's unit multiplied by your drag, and if the magnitude of the vector is lower than your drag, set it to 0,0 instead of applying the drag (otherwise will be jittery on stopping)
« Last Edit: February 15, 2013, 06:18:10 am by Weeve »
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion