Collision!

Hello. I need help with some pong collision. I have the Player collision down -

1
2
3
4
if(BoxX == PlayerX + 18 && BoxY + 50 >= PlayerY && BoxY + 50 <= PlayerY + 225){
		Left = false;
		Right = true;
	}

And that works! But this Bot collision is not working. The bot is on the left side, while the Player is on the right by the way. Bot -

1
2
3
4
if(BotX == BoxX + 50 && BotY + 225 <= BoxX + 50 && BotY + 225 && BoxY + 50){
		Left = true;
		Right = false;
	}


Do you see whats wrong with it? I've been on this for 4 days now..

And i'm using allegro, if that matters at all. Thanks!
Last edited on
if(BotX == BoxX + 50 && BotY + 225 <= BoxX + 50 && BotY + 225 && BoxY + 50)

Maybe this?
Oops, i meant to do

if(BotX == BoxX + 50 && BotY + 225 <= BoxX + 50 && BotY + 225 >= BoxY + 50)

Which still doesn't work.. :(
Rather than using constants, why not use Allegro's BITMAP::w and BITMAP::h for width and height in your checks? This will prevent you typing width X for an image with Y width.

1
2
3
4
BITMAP* bot = load_bitmap("bot.bmp", NULL);

if (bot->w /*blah blah*/ && bot->h/*blah blah*/)
something();


Maybe you should use (botX <= boxX + 50)??? How many units does your bot move per frame? If it is more than one, an equality check may fail.

ex.

[frame1]
botX==boxX+51
botX -= 2

[frame2]
botX==boxX+49
botX -= 2


As you can see, boxX+50 gets "skipped". A relative (< or <=) check will solve that.
Wait, allegro can find out how big your image is? Didn't know that..


It works! Why does <= work, but == doesn't?

My units is 2 for everything, but I guess that's not a problem.

Thanks!
Do you know how to make the bot follow the box?

I have

1
2
3
4
5
6
if(BotY < BoxY + 125 && BoxY != BotY){
		BotY+=2;
	}
if(BotY > BoxY + 125 && BoxY != BotY){
		BotY-=2;
	}


But it only follows if the top of BotY is less or more then BoxY. How do I make it so it follows it when it's not equal to the MIDDLE of the BotY, not the top.. And I guess adding half of the BotY doesn't work.
kong288 wrote:
And I guess adding half of the BotY doesn't work


Why not? Have you even tried it? It will work, provided your doing it right.
Topic archived. No new replies allowed.