Jump to content
  • Advertisement
Sign in to follow this  
walle

Help me spot the error

This topic is 4620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!