Player jumping doesnt always land on the bottom

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
http://www.mediafire.com/?yn9fz6h6nei3hg5 http://www.mediafire.com/?yn9fz6h6nei3hg5
The game is linked to: SDL, SDL_main, SDL_image, SDL_ttf
look at player.cpp
<pre class="prettyprint #include "Player.h"
#include "Constants.h"
#include "Globals.h"
#include <iostream>
#include <cstdlib>

Player::Player()
{
//Initialize the offsets
x = 0; y = SCREEN_HEIGHT - PLAYER_HEIGHT;
//Initialize the velocity
xVel = 0; yVel = 0;

//Initialize other movement variables
climax = 8;
airtime = 0;

//Initialize animation variables
frame = 0;
frame2 = 0;
status = PLAYER_RIGHT;
ani_time = 0;
ani_climax = 2;

//Initializes gravity status
in_air = false;
on_ground = false;
falling = false;

}

void Player::handle_events()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the velocity
switch( event.key.keysym.sym )
{
//moving right
case SDLK_RIGHT: xVel += PLAYER_SPEED; break;
//moving left
case SDLK_LEFT: xVel -= PLAYER_SPEED; break;
//jumping (when applicable)
case SDLK_UP:
if ( in_air == false && falling == false && on_ground == true )
{
//yVel -= PLAYER_GRAV; // - means it goes up, + means it goes down
in_air = true;
on_ground = false;
}
break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Set the velocity
switch( event.key.keysym.sym )
{
case SDLK_RIGHT: xVel -= PLAYER_SPEED; break;
case SDLK_LEFT: xVel += PLAYER_SPEED; break;
case SDLK_UP:
if ( in_air == true )
{
airtime = (climax - 2);
}
break;
}
}
}

void Player::move()
{
//Move
x += xVel;

y += yVel;

//Make sure the player does not walk out of the dimmensions
if( ( x < 0 ) || ( x + PLAYER_WIDTH > SCREEN_WIDTH ) )
{
x -= xVel;
}

//Handle the players gravity
if( y + PLAYER_HEIGHT > SCREEN_HEIGHT ) //If the player is on the ground
{
//only if falling is true (landing on the ground)
if( falling == true && ( in_air == false && on_ground == false) )
{
y -= yVel;
yVel = 0;
on_ground = true;
falling = false;
}
}
else //If you are on the ground
{
if( on_ground == false )
{
if( in_air == false )
{
falling = true;
}
}
}

}

void Player::jump()
{
//while you are in air, the airtime will reach its climax
if( in_air == true )
{
if( airtime < climax ) //if the airtime has not climaxed, then it shall increase
{
airtime++;
if( yVel > -PLAYER_GRAV )
{
yVel--;
//Print the yVel to console
cout << "yVel: " << yVel << endl;
}
}
}
if( airtime == climax ) //one airtime reaches it climax, it shall start falling
{
in_air = false;
falling = true;
airtime = 0;
}

if( falling == true )
{
if( yVel < PLAYER_GRAV ) //this way the jump does not speed up
{
yVel++;
//Print the yVel to console
cout << "yVel: " << yVel << endl;
}
}

}

void Player::show()
{
//If Player is moving left
if( xVel < 0 )
{
//Set the animation to left
status = PLAYER_LEFT;

//Move to the next frame in the animation
ani_time++;

if( ani_time > ani_climax )
{
ani_time = 0;
frame++;
}

//Other frames will also work even if not shown
frame2 = 1;
}
//If Player is moving right
else if( xVel > 0 )
{
//Set the animation to right
status = PLAYER_RIGHT;

//Move to the next frame in the animation
ani_time++;

if( ani_time > ani_climax )
{
ani_time = 0;
frame++;
}

//Other frames will also work even if not shown
frame2 = 0;
}
//If Player standing
else
{
//Restart the animation
frame = 0;
ani_time = 0;
}

//Loop the animation
if( frame >= 5 )
{
frame = 1;
}

//Show the animation
apply_surface( x, y, player, screen, &clipsPlayer );
//The following is what decides clipsPlayer is
if( status == PLAYER_RIGHT )
{
if( on_ground == true )
{
clipsPlayer = clipsRight[ frame ];
}
}
if( status == PLAYER_LEFT )
{
if( on_ground == true )
{
clipsPlayer = clipsLeft[ frame ];
}
}
if( on_ground == false && ( in_air == true || falling == true ) )
{
clipsPlayer = clipsJump[ frame2 ];
}
}

void Player::text()
{
apply_surface( 8, 8, message, screen, NULL );

//Render the text
message = TTF_RenderText_Solid( font, "Text", textColor );

//If there was an error in rendering the text
if( message == NULL )
{
return;
}
}[/code]
The problem I am having is that sometimes when I land, I may be a few pixels above the ground. What can I do so that I always land on the surface pixel perfect? This should be an easy fix for someone who knows far more c++ than I do. Just tell me what code
goes where and all will be well :). Thanks




View the full article
 
Back
Top