Sign in to follow this  
Avah Don

SDL simple jump?

Recommended Posts

Avah Don    105

Hi, I'm following this sdl tutorial by lazyfoo and i want to add a simple jump, like jumping 160px. Here the code for movement;

void Player::handleEvent(SDL_Event& e)
{
	if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
	{
		switch (e.key.keysym.sym)
		{
		case SDLK_UP: _velY -= _playerVel;	break;
		case SDLK_DOWN:	_velY += _playerVel; break;
		case SDLK_LEFT:	_velX -= _playerVel; break;
		case SDLK_RIGHT: _velX += _playerVel; break;
		}
	}

	else if (e.type == SDL_KEYUP && e.key.repeat == 0)
	{
		switch (e.key.keysym.sym)
		{
		case SDLK_UP: _velY += _playerVel; break;
		case SDLK_DOWN:	_velY -= _playerVel; break;
		case SDLK_LEFT: _velX += _playerVel; break;
		case SDLK_RIGHT: _velX -= _playerVel; break;
		}
	}
}

void Player::move(std::vector<vec3Struct>& _levelInfo)
{
	_posX += _velX;
	if( ( _posX < 0 ) || ( _posX + _playerBox > 1000 /*levelWidth*/ ) || touchesWall(_levelInfo))
	{
		_posX -= _velX;
	}

	_posY += _velY;
	if( touchesWall(_levelInfo))
	{
		_posY -= _velY;
	}
}

bool Player::checkCollision( SDL_Rect a, SDL_Rect b )
{
	//The sides of the rectangles
	int leftA, leftB;
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB;

	//Calculate the sides of rect A
	leftA = a.x;
	rightA = a.x + a.w;
	topA = a.y;
	bottomA = a.y + a.h;

	//Calculate the sides of rect B
	leftB = b.x;
	rightB = b.x + b.w;
	topB = b.y;
	bottomB = b.y + b.h;

	//If any of the sides from A are outside of B
	if( bottomA <= topB ) {	return false; }
	if( topA >= bottomB ) {	return false; }
	if( rightA <= leftB ) {	return false; }
	if( leftA >= rightB ) {	return false; }
	//If none of the sides from A are outside B
	return true;
}

bool Player::touchesWall(std::vector<vec3Struct>& _levelInfo)
{
	//Go through the tiles
	for( int i = 0; i < _levelInfo.size(); ++i )
	{
		SDL_Rect tmp;
		tmp.x = _levelInfo[i].x;
		tmp.y = _levelInfo[i].y;
		tmp.w = 80;
		tmp.h = 80;

		SDL_Rect _playerRec;
		_playerRec.x = _posX;
		_playerRec.y = _posY;
		_playerRec.w = _playerBox;
		_playerRec.h = _playerBox;

		//If the collision box touches the wall tile
		if( checkCollision( _playerRec, tmp) )
		{
			return true;
		}
	}
	//If no wall tiles were touched
	return false;
}

but everytime a try too add gravity to game, like if player is not toches wall, it y axis will decrease, but i always failed :(

Edited by avahdon

Share this post


Link to post
Share on other sites
Avah Don    105

When you say that your attempts to add gravity always fail, in what sense is it failing and how are you adding gravity?

as i said above, if player is not toches wall, it y axis will increases, i add this to the move function

if( !touchesWall(_levelInfo))
	{
		_posY ++;
	}

but, after this, i can no longer move. this fall slowly till it hit the level.

Share this post


Link to post
Share on other sites
Finalspace    1147

I would recomment you to read a platformer guide - here are some good ones:

 

http://info.sonicretro.org/SPG:Solid_Tiles

http://www.hobbygamedev.com/int/platformer-game-source-in-processing/

http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

http://www.wildbunny.co.uk/blog/2011/12/11/how-to-make-a-2d-platform-game-part-1/

 

In your case, i would focus on using a tile-based approach, simple to implement and works for most cases.

 

Also you can use an existing physics engine to make a platformer as well - for example box2d which exists for nearly every platform.

Edited by Finalspace

Share this post


Link to post
Share on other sites
CC Ricers    1491

Tile-based collision makes a lot of things easier. But you don't seem to be taking advantage of all these conveniences.

 

It is not necessary to brute-force check all the tiles of the level. You need to take the X and Y extents of the character's bounding box, reduce the position down to the tile coordinates, and check the tiles within that range.

 

You can keep applying gravity on each frame, but reset downward velocity to 0 whenever the character is touching ground. That prevents it from getting stuck in the ground.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this