Small gap when landing in platform game

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Small gap when landing in platform game

Post by PaperDuckyFTW »

Good day to evarywun!!!!!!!

As the title says, I've come across a cedundrum. In the platofrm game I am making, after implimenting tile based collision I implimented gravity and jumping. All three work fine-however when the player lands there is a small gap maybe 2-5 pixels in height, that is under the player. I dont really mind it, but to make the game look completely awesome I was hoping to eliminate this gap. If this is confusing i can post a link to a pic to show what I mean.

If anyone knows why there is this gap, I would be in your online/virtual debt :mrgreen:
Thanks for your help and take care
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Small gap when landing in platform game

Post by short »

If this is confusing i can post a link to a pic to show what I mean.
This and related code.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

Re: Small gap when landing in platform game

Post by Randi »

have you checked to make sure the image of the player is towards the bottom of it? or it could be there is extra space on the tile, or I could be wrong altogether.
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Small gap when landing in platform game

Post by PaperDuckyFTW »

Ok, here is the picture of the problem:
Image

Here is some code...

Code: Select all

//TILE COLLISION
bool tile_collision()
{
	for( int x = 0; x < 20; x++ )
	{
		for( int y = 0; y < 20; y++ )
		{
			if( map1[y][x] == 2 )
			{
				if( ( bounds.x+bounds.w > x*32 ) && ( bounds.x < x*32+32 ) &&
				    ( bounds.y+bounds.h > y*32-1 ) && ( bounds.y < y*32+32)    )
				{
					return true;
				}
			}
		}
	}

	return false;
}

Code: Select all

//exerts from the player class

case SDLK_UP: if( can_jump == true ){ jump_power = -20; can_jump = false; }; break;

void Player::update_player()
{
	playerYVel = 5;

	if( jump_power < 0 )
	{
		jump_power++;
	}

	bounds.x += playerXVel;

	if( ( bounds.x < 0 ) || ( bounds.x + PLAYER_WIDTH > LEVEL_W ) || tile_collision() )
	{
		bounds.x -= playerXVel;
	}

	bounds.y += playerYVel;
	bounds.y += jump_power;

	if( ( bounds.y < 0 ) || ( bounds.y + PLAYER_HEIGHT > LEVEL_H ) || tile_collision() )
	{
		bounds.y -= playerYVel;
		can_jump = true;

		if(jump_power < 0)
        {
			jump_power = 0;
			bounds.y = bounds.y/32*32+32;
        }
	}

}
I hope this *might* clear things up. The tiles are 32x32 and the player is 19x34
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Small gap when landing in platform game

Post by PaperDuckyFTW »

With the tile collision things, ive played around with the numbers, an example is the: bounds.y+bounds.h > y*32-1
Of btw, the bounds rect is the player rect.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Small gap when landing in platform game

Post by XianForce »

Uhh, why do you have a yVelocity and jump power, both adding to bounds.y?

I'd say what's happening is something like this:

bounds.y is increased by playerYVel and jump_power.

Then when there's some type of collision, you undo what you did with the playerYVel ONLY... So I'd say that there may be a problem there...
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Small gap when landing in platform game

Post by PaperDuckyFTW »

What ive done is edited the spritesheet so now there is no transparent pixels under the player in case that was the problem. The problem I am having is that the first time it makes contact with the collision tile, there is no gap underneath the player. However when I kump onto a platform for example, there is the gap. But when i jump back onto the first set of collision tiles, there is no gap. It seems that some tiles leave a gap, while some dont leave any. Im completely stumped :(
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Small gap when landing in platform game

Post by PaperDuckyFTW »

Okay after staring at the screen while I jump onto the different tiles, I have found out that it seems that colliding with the bottom of the tile is what screws it up. However there is still a few tiels that the gap is there even if its the first tile i collide with. Im really confused becuase there are some tiles that have perfect collision, with no small gap and there are tiles that have gaps no matter what tile i previously collided with.

Okay Im about to kill someone because I just changed it so that theres no gravity and i can move up and down much like an RPG. There is still a goddam gap underneath the player and i have absolutely no clue whats up with it. If anyone knows what MIGHT be the problem, if it works i might just cry in joy. Oh and I think my status thing should be changed to "Chaos Rift NOOB" :lol:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Small gap when landing in platform game

Post by eatcomics »

its probably that your collision just needs to stop when the player is closer to the object... like if you're stopping your player from moving when there would be a collision, just make sure he stops closer to the object... or if you're just moving the player back when he hits the object just don't move him back as far...
Image
Post Reply