1
General / 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:
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:
(added dt and tried to multiply it by the acceleration and deceleration)
The Controller function:
I would love for any help, thank you very much
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);
}
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);
}
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;
}
}
}
}
{//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