Advertisement

SFML smoother jumping?

Started by April 24, 2015 07:59 PM
7 comments, last by Elit3d 9 years, 8 months ago

The gravity is smooth going down however, when I press space it kind of jolts up. GIF: http://i.gyazo.com/a876e13925dde99c3bbbe8b14a1fa164.gif


#include "Player.h"

Player::Player()
{
	if (!playerTexture.loadFromFile("images/wizardT.png"))
	{
		std::cout << "Can't load the player image";
	}


	playerImage.setTexture(playerTexture);
	playerImage.setPosition(200, 200);
	playerImage.setTextureRect(sf::IntRect(48, 14, 73, 73));

	m_playerSpeed = 5.0f;

	gravity = 2.0;
	groundHeight = 525;
	flyTime = 0;

	isGrounded = true;
	keyPressed = false;

	moveSpeed = 2.0f;
	jumpSpeed = 150.0f;
}


Player::~Player()
{ 
}

void Player::OnKeyDown(){
	sf::Vector2f velocity(sf::Vector2f(0, 0));

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && keyPressed == false && playerImage.getPosition().y >= 0){
		keyPressed = true;
		velocity.y = -jumpSpeed;
		std::cout << "jump";
	}
	else if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
		keyPressed = false;
	}

	if (playerImage.getPosition().y + playerImage.getScale().y < groundHeight || velocity.y < 0)
	{
		velocity.y += gravity;
	}
	else{
		playerImage.setPosition(playerImage.getPosition().x, groundHeight - playerImage.getScale().y);
		velocity.y = 0;
	}

	playerImage.move(velocity.x, velocity.y);

}

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

Are you just moving your character by the jump speed value once and then letting gravity kick in? My characters have a vertical speed which essentially is at 0 when they're at rest on a platform. When the player presses the jump button, that speed is set to some amount (for example 18) and then is slowly decreased with each process frame (for example by 1). The result is a sort of acceleration and deceleration when you jump.

However I'm not currently using a fixed time step which would might change things a bit (I think mostly the rate at which the vertical speed changes). Maybe someone else could verify that.

Advertisement

Are you just moving your character by the jump speed value once and then letting gravity kick in? My characters have a vertical speed which essentially is at 0 when they're at rest on a platform. When the player presses the jump button, that speed is set to some amount (for example 18) and then is slowly decreased with each process frame (for example by 1). The result is a sort of acceleration and deceleration when you jump.

However I'm not currently using a fixed time step which would might change things a bit (I think mostly the rate at which the vertical speed changes). Maybe someone else could verify that.

Yes that is what it is currently doing. Im still learning this stuff about jumping so not sure of a better way to do it.

It looks like the y velocity instantly becomes -150 as soon as the space is pressed. You're not gradually bringing the value to -150.

Also, I'm not seeing anything that shows the character's position being affected by the velocity. So it's looking to me (between the code and the gif you provided) as though you're actually using your velocity values like position values.

You have "velocity" declared as a local variable in OnKeyDown which makes players velocity (0, 0) each time OnKeyDown is called. You need to store velocity variable in a player (make it a object variable). And decrease values of jumpSpeed and gravity.

Basically, what you're doing now:

if space was pressed you move player up to a high value (-jumpSpeed)

then each frame you move player down with a constant speed (gravity)

Alex explained the problem...

Also, i recommend step debugging. If you watch it line by line in a debugger it will often illustrate the problem well.
Advertisement

You have "velocity" declared as a local variable in OnKeyDown which makes players velocity (0, 0) each time OnKeyDown is called. You need to store velocity variable in a player (make it a object variable). And decrease values of jumpSpeed and gravity.

Basically, what you're doing now:

if space was pressed you move player up to a high value (-jumpSpeed)

then each frame you move player down with a constant speed (gravity)

Can I have an example of creating an object variable?

Terminology I have chosen probably wasnt the best. It is just member variable:


class Player
{
    sf::Vector2f velocity;
    ...
};

Dont forget to initialize it in constructor and remove declaration of local variable "velocity" in OnKeyDown method.

Terminology I have chosen probably wasnt the best. It is just member variable:


class Player
{
    sf::Vector2f velocity;
    ...
};

Dont forget to initialize it in constructor and remove declaration of local variable "velocity" in OnKeyDown method.

Wow cant believe it was that simple to fix my issue. I feel like a noob now hahahah. Many thanks sir! Fixed :) +1

This topic is closed to new replies.

Advertisement