0
\$\begingroup\$

I have been getting into game development and I have made some progress. I have come across an obstacle which is jumping I managed to get the Jumping to semi work but it doesn't look like a jump but instead it Jolts up or teleports up. This is how it looks:enter image description here

It's a bit slower thanks to the gif but as you can see it just jolts up. I tried implementing multiple ways to jump and I tried to stay with the velocity and acceleration method.

This is what I have right now:

sf::Vector2f velocity = sf::Vector2f(0,0);
float accelaration = 1.f;
void Player::updatePhysics(int deltaTime) {
    if (playerCount == 1) {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
            velocity.x -= accelaration * deltaTime;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
            velocity.x += accelaration * deltaTime;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !isJump) {
            isJump = true;
            jumpDownEnd = false;
            velocity.y -= 30 * deltaTime;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) & velocity.y > 800) {
            velocity.y += accelaration * deltaTime;
        }
    }


    if (velocity.y > 800) {
        velocity.y = 800 - player.getGlobalBounds().height / 2;
    }
    else {
        velocity.y += gravity ;
    }
    player.setPosition(velocity.x, velocity.y);
}

Notes
I have tried a couple of other things like storing player position and adding velocity and multiplying by deltaTime and it worked but I got rid of it because I got a few bugs.

Also I tried to change the velocity.y -= 30* deltaTime to velocity.y = 30* deltaTime and all it did was reduce height by a bit.

Also the reason I am not multiplying velocity by delta time when setting position is because it starts to twitch when it gets further away from the original position.

I have tried searching for answers but they didn't help me find any solutions.

Also deltaTime is as milliseconds instead of seconds. I constantly get a deltaTime of 15 or 16 if that helps.

Update

I have made the code to add velocity to player position but now the jump is just super fast and still jolts up.

void Player::updatePhysics(int deltaTime) {
    velocity.x = 0;
    if (playerCount == 1) {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
            velocity.x -= accelaration;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
            velocity.x += accelaration;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !isJump) {
            isJump = true;
            jumpDownEnd = false;
            velocity.y = -300;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) & velocity.y > 800) {
            velocity.y += accelaration;
        }
    }
    velocity.y += gravity * deltaTime;

    playerPos.x += velocity.x * deltaTime;
    playerPos.y += velocity.y * deltaTime;

    if (playerPos.y > 800) {
        playerPos.y = 800 - player.getGlobalBounds().height / 2;
        velocity.y = 0;
    }

    player.setPosition(playerPos);
}

enter image description here

\$\endgroup\$
9
  • \$\begingroup\$ why are you setting player position to velocity, instead of adding velocity to player position? \$\endgroup\$
    – Ocelot
    Nov 10, 2020 at 11:11
  • \$\begingroup\$ Like I said I did that but it didn’t change anything. I made a variable with the players initial x and y then continuously added the velocity but it didn’t change anything \$\endgroup\$ Nov 10, 2020 at 12:26
  • \$\begingroup\$ You said it fixed it but you got a few bugs so you got rid of it. Clearly you are contradicting yourself here. \$\endgroup\$
    – Ocelot
    Nov 10, 2020 at 12:42
  • \$\begingroup\$ No what I meant is that adding the velocity to player position worked. By that I meant it worked the exact same way as just setting the velocity to player position. \$\endgroup\$ Nov 10, 2020 at 12:48
  • \$\begingroup\$ Ok so right off the bat there are two problems. First, figure out your units of measure. If your measure is pixels and you are multiplying it by time in miliseconds, then presumably your velocity is measured in pixels per milisecond. That means your initial jump velocity is 30*1000 = 30000 pixels per second. Second, your gravity is frame dependent: you are adding constant value each frame no matter how much time has passed between the frames. Obviously that means your gravity will affect velocity more at higher framerates. \$\endgroup\$
    – Ocelot
    Nov 10, 2020 at 13:32

1 Answer 1

1
\$\begingroup\$

So I figured out my issue. Thanks to @Ocelot for telling me my deltaTime in milliseconds was too high. So I just divided my deltaTime by 100 and that fixed my issue.

void Player::updatePhysics(int deltaTime) {
    velocity.x = 0;
    if (playerCount == 1) {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
            velocity.x -= accelaration;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
            velocity.x += accelaration;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !isJump) {
            isJump = true;
            jumpDownEnd = false;
            velocity.y = -300;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) & velocity.y > 800) {
            velocity.y += accelaration;
        }
    }
    velocity.y += gravity * deltaTime/100;

    playerPos.x += velocity.x * deltaTime/100;
    playerPos.y += velocity.y * deltaTime/100;

    if (playerPos.y > 800) {
        playerPos.y = 800 - player.getGlobalBounds().height / 2;
        velocity.y = 0;
    }

    player.setPosition(playerPos);
}

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Don't divide delta time in each expression. Division is a relatively expensive operation, so try to do it once. \$\endgroup\$
    – Ocelot
    Nov 11, 2020 at 22:56
  • \$\begingroup\$ Alright I will change that in my code \$\endgroup\$ Nov 12, 2020 at 0:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .