I am trying to make a platform game in C++ and SDL2.
I tried to add a jump in the update() function of the main code below , but it does not work as expected, as you can see in the video:
Note: I did not set any fixed position after the jump so the player will keep falling when jumping unless I press Left/Right/Up/Down keys to stop it.
1)Jumping is not responsive all times: sometimes the player just walks instead of jumping and I have no idea why
2)It jumps to an inconsistent height each time, depending on when I press the left/right keys
3)The left/right keys should not work after they have been pressed once (when it comes to jumping) but they work, allowing the player to turn and steer themselves in mid-air, and I have no idea how to fix it.
//test
if(Player.State == PLAYER_JUMPING && keystates[SDL_SCANCODE_LEFT])
Player.XPosition -= 2.5f; //jump on the left
if(Player.State == PLAYER_JUMPING && keystates[SDL_SCANCODE_RIGHT])
Player.XPosition += 2.5f; //jump on the right
/////////////////////// //Actual code that works to jump up//////////////////////////
enum EPlayerState
{
PLAYER_STILL = 0, //0
PLAYER_MOVING_LEFT,
PLAYER_MOVING_RIGHT,
PLAYER_MOVING_UP,
PLAYER_MOVING_DOWN,
PLAYER_JUMPING,
NUM_PLAYER_STATES //6
};
/////////////////////////////////////////////////////////////////
struct SPlayer
{
.......
float XPosition = 0.f;
float YPosition = 408.0f;
float Velocity = 160.0f;
float JumpVelocity = -200.0f;
float Gravity = 250.0f;
EPlayerState State = PLAYER_STILL;
.....
};
//////////////////////////////////////////
SPlayer Player;
////////////////////////////////////////
//POLLING EVENTS
// The user is holding down a key on the keyboard
case SDL_KEYDOWN:
{
switch(Event.key.keysym.sym)
{
case SDLK_LEFT:
{
isLeftPressed = true;
Player.State = PLAYER_MOVING_LEFT;
break;
}
case SDLK_RIGHT:
{
isRightPressed = true;
Player.State = PLAYER_MOVING_RIGHT;
break;
}
case SDLK_UP:
{ isUpPressed = true;
Player.State = PLAYER_MOVING_UP;
break;
}
case SDLK_DOWN:
{ isDownPressed = true;
Player.State = PLAYER_MOVING_DOWN;
break;
}
case SDLK_s:
{ isSPressed = true;
Player.State = PLAYER_JUMPING;
break;
}
default:
break;
}
break;
}
// The user is releasing a key on the keyboard
case SDL_KEYUP:
{
switch(Event.key.keysym.sym)
{
case SDLK_LEFT:
{
isLeftPressed = false;
if(Player.State == PLAYER_MOVING_LEFT)
Player.State = PLAYER_STILL;
break;
}
case SDLK_RIGHT:
{
isRightPressed = false;
if(Player.State == PLAYER_MOVING_RIGHT)
Player.State = PLAYER_STILL;
break;
}
case SDLK_UP:
{
isUpPressed = false;
if(Player.State == PLAYER_MOVING_UP)
Player.State = PLAYER_STILL;
break;
}
case SDLK_DOWN:
{
isDownPressed = false;
if(Player.State == PLAYER_MOVING_DOWN)
Player.State = PLAYER_STILL;
break;
}
case SDLK_s:
{
//nothing here so we keep jumping pressing S once
break;
}
default:
break;
}
}
default:
break;
}
}
}
//////////////////////////////////////////////////////
// in Update() function I have:
//Jumping UP
if(Player.State == PLAYER_JUMPING)
{
Player.YPosition += Player.JumpVelocity * (float)DeltaTime; //Yposition + (-200.0f * Deltatime)
Player.JumpVelocity += Player.Gravity * (float)DeltaTime; //-200.0f + 250.0f * DeltaTime
}
else //reset JumpVelocity to original value
{
Player.JumpVelocity = -200.0f;
}