This is my code for handling movement:
void Movement(const Uint8 *keyboardHandle)
{
if (keyboardHandle[SDL_SCANCODE_SPACE] && yes == 0)
{
velY = -20;
yes = 100;
}
player.posY += velY;
player.posY += g;
if (player.posY >= SCREEN_HEIGHT - player.width / 2)
{
player.posY = SCREEN_HEIGHT - player.width / 2;
}
velY = 0;
// Timer
yes--;
if(yes <= 0)
{
yes = 0;
}
}
It is basic, but all works besides the jump, it just teleport the player upwards.
I tried handling the gravity before the jump, not working. I tried making a for
loop to increment the Y velocity, still not working. Any ideas?
Solved it.
void Movement(const Uint8 *keyboardHandle)
{
if (keyboardHandle[SDL_SCANCODE_SPACE] && yes == 0)
{
g = -10;
yes = 50;
}
player.posX += velX;
player.posY += g;
if (player.posY >= SCREEN_HEIGHT - player.width / 2)
{
player.posY = SCREEN_HEIGHT - player.width / 2;
}
// Jump force
g++;
if(g >= 5)
{
g = 5;
}
// Jump timer
yes--;
if(yes <= 0)
{
yes = 0;
}
}
g
is by default 5
which would be the gravity, and we just smoothly invert it for a jump.