So, I ask question about my 2D game, actually it is not game, but it is something like poor game. Well i made moving system and now is time to make jump. I made some kind of gravity with tutorial but I don't like it. It is simple and nice but there is problems in that gravity example: 1. You can hold up arrow and "fly" with jump. I tried to fix that and make some kind of limit to jump but it was bad try. Second problem is that when you jump and you release up arrow (jumping key) then my sprite fall very fast and I can't really control that sprite in air.
This is my first game and I want spend time to do that, but now i have problem that i can't solve with my self so if you can help, please do it. I use C++ with CodeBlocks and SFML multimedia library.
So there is little parts of my code.
First there is velocity and gravity configuration:
//gravity & velocity
const float gravity = 1;
int ground = 600; //height of ground
Vector2f velocity(Vector2f(0,0));
float movingSpeed = 0.5f, jumpingSpeed = 2.0f;
Then there is simple when you press button you jump:
if(Keyboard::isKeyPressed(Keyboard::Up)){
skeleton.move(0,velocity.y = -jumpingSpeed);
source.y = Up;
isWalking = true;
}
And there is system what recognize if you jump and this make you fall:
if(skeleton.getPosition().y + skeleton.getScale().y < ground || velocity.y < 0){
velocity.y += gravity;
}else{
skeleton.setPosition(skeleton.getPosition().x, ground - skeleton.getScale().y);
}
So my sprite name is "skeleton". And yeah this is first time with game physics. I'm not very good game programmer so these code can be poor and yeah... if you invent a solution share it.