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

Author Topic: how to make player movement based on Duration of pressing  (Read 2770 times)

0 Members and 1 Guest are viewing this topic.

elhanan

  • Newbie
  • *
  • Posts: 2
    • View Profile
how to make player movement based on Duration of pressing
« on: May 29, 2021, 04:19:05 pm »
Hey all,

I'm trying to figure out how to make the player movement that will be affected by the duration of pressing on the Keys.

I used a guide from youtube and got to this result:

Player.cpp:
#include "Player.h"

Player::Player(const sf::Sprite& sprite) : Moveable(sprite)
{
        initPhysics();
}

void Player::update()
{
        updateMovement();
        updatePhysics();
}

void Player::updateMovement()
{
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                move(-1.f, 0.f);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                move(1.f, 0.f);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                move(0.f, -1.f);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                move(0.f, 1.f);
        }
}

void Player::draw(sf::RenderWindow& window)
{
        window.draw(getSprite());
}

void Player::move(float dirX, float dirY)
{
        m_velocity.x += m_acceleration * dirX;

        if (std::abs(m_velocity.x) > MAX_VELOCITY)
                m_velocity.x = MAX_VELOCITY * ((m_velocity.x < 0.f) ? -1.f : 1.f);

}

void Player::initPhysics()
{
        m_acceleration = 5.f;
        m_deccelaration = 0.90f;
        m_gravity = 4.f;
}

void Player::updatePhysics()
{
        m_velocity *= m_deccelaration;

        if (std::abs(m_velocity.x) < MIN_VELOCITY)
                m_velocity.x = 0.f;
        if (std::abs(m_velocity.y) < MIN_VELOCITY)
                m_velocity.y = 0.f;

        getSprite().move(m_velocity);
}
 

right now its working but I want it to consider the duration of pressing I have tried to to change my code but it didn't worked.
here is how I tried to change it:

#include "Player.h"

Player::Player(const sf::Sprite& sprite) : Moveable(sprite)
{
        initPhysics();
}

void Player::update(float dt)
{
        updateMovement(dt);
        updatePhysics(dt);
}

void Player::updateMovement(float dt)
{
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                move(-1.f, 0.f, dt);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                move(1.f, 0.f, dt);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                move(0.f, -1.f, dt);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                move(0.f, 1.f, dt);
        }
}

void Player::draw(sf::RenderWindow& window)
{
        window.draw(getSprite());
}

void Player::move(float dirX, float dirY, float dt)
{
        m_velocity.x += m_acceleration * dirX * dt;

        if (std::abs(m_velocity.x) > MAX_VELOCITY)
                m_velocity.x = MAX_VELOCITY * ((m_velocity.x < 0.f) ? -1.f : 1.f);

}

void Player::initPhysics()
{
        m_acceleration = 5.f;
        m_deccelaration = 0.90f;
        m_gravity = 4.f;
}

void Player::updatePhysics(float dt)
{
        m_velocity *= m_deccelaration * dt;

        if (std::abs(m_velocity.x) < MIN_VELOCITY)
                m_velocity.x = 0.f;
        if (std::abs(m_velocity.y) < MIN_VELOCITY)
                m_velocity.y = 0.f;

        getSprite().move(m_velocity);
}

 

(added dt and tried to multiply it by the acceleration and deceleration)

The Controller function:
void Controller::run()
{//main loop
    sf::Clock clock;
    while (m_window.isOpen())
    {
        m_window.clear();
        m_player.draw(m_window);
        m_window.display();

        m_player.update(clock.restart().asSeconds());

        if (auto event = sf::Event{}; m_window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                m_window.close();
                break;
            }

        }
    }
}
 

I would love for any help, thank you very much
« Last Edit: May 29, 2021, 04:37:16 pm by elhanan »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: how to make player movement based on Duration of pressing
« Reply #1 on: May 30, 2021, 01:21:07 pm »
what exactly didn't worked? what happened?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

elhanan

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: how to make player movement based on Duration of pressing
« Reply #2 on: May 30, 2021, 01:24:49 pm »
the player is not moving.

maybe I don't send the right time?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: how to make player movement based on Duration of pressing
« Reply #3 on: May 30, 2021, 01:33:18 pm »
that could be the problem. can you try to debug it and watch some variables? i suspect either m_acceleration or dirX or dt are equal to zero, hence m_velocity.x += m_acceleration * dirX * dt; is also zero, and if velocity is zero the player doesn't move  :P
Visit my game site (and hopefully help funding it? )
Website | IndieDB