Sign in to follow this  
walle

Help me spot the error

Recommended Posts

walle    478
Hello. On the way with my 2d sidescrolling plattform game, but now I'm trying to get the player to jump and fall correctly. The game isn't tile based. Instead I have three stl::vectors of map objects, one is solids, this is the ones I want to check against. My idea is that to use a reference pixel(maybe two later on) on the bottom of my player sprite and check if this is inside one of the solid map objects. But my code doesn't work, and I don't know why. This is the function I use to check if a pixel is inside a sprite:
bool pixel_collision(cSprite *sprite, int x, int y)
{
	if(x <= sprite->getx() & x >= sprite->getx() + sprite->getw())
	{
		pDebug->write("here: %i, %i\n",x,y);
		if(y <= sprite->gety() & y >= sprite->gety() + sprite->geth())
        {
        	return true;
        }
	}
return false;
}

And this is the player update function, used every time the player is drawn:
void cPlayer::update()
{
	if(jumping == true)
	{
		vel.y -= 3;
        for(int i = 0; i < solid_objects.size(); i++)
        {
        	if(pixel_collision(&solid_objects.at(i), pPlayer->sprite.getx() + pPlayer->sprite.getw() / 2, 
        	pPlayer->sprite.gety() + pPlayer->sprite.geth()))
        	{
                pos.y = solid_objects.at(i).gety() - pPlayer->sprite.geth();
                jumping = false;
                vel.y = 0;
        	}
        }
	}
	pos.y -= vel.y * framerate.dtime();
}

If someone could point me in the right direction I would appreciate it greatly. Please tell me if I should add something more to make it understandable. Thanks.

Share this post


Link to post
Share on other sites
SiCrane    11839
My first guess is that this line doesn't do what you expect:

if(x <= sprite->getx() & x >= sprite->getx() + sprite->getw())

You probably want something like:

if ( (x <= sprite->getx()) &&
(x >= sprite->getx() + sprite->getw()))

Share this post


Link to post
Share on other sites
walle    478
Nope, I changed the pixel_collision function to:

bool pixel_collision(cSprite *sprite, int x, int y)
{
if( ( (x <= sprite->getx()) && (x >= sprite->getx() + sprite->getw())))
{
pDebug->write("here: %i, %i\n",x,y);
if( ( (y <= sprite->gety()) && (y >= sprite->gety() + sprite->geth())))
{
return true;
}
}
return false;
}



But it doesn't work...

Share this post


Link to post
Share on other sites
Enigma    1410
The next problem would be that your comparisons are the wrong way around. I'll plug in some example numbers to demonstrate:
if(x <= 7 && x >= 7 + 2)

Pick an x which is both less than or equal to seven and also greater than or equal to nine! You likely wanted:
bool pixel_collision(cSprite *sprite, int x, int y)
{
if(x >= sprite->getx() && x <= sprite->getx() + sprite->getw())
{
pDebug->write("here: %i, %i\n",x,y);
if(y >= sprite->gety() && y <= sprite->gety() + sprite->geth())
{
return true;
}
}
return false;
}

Enigma

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