Jump to content

Bounding box collision broken

TEDWARDS

Hi,

My brother has university project to create a Pacman game and is currently stuck on the bounding box collision. What happens when you debug the game in visual studio is all of the munchies disappear as if they are all being collided with and hes spent sometime trying to figure it out but hes reached a point where nothing works as he thinks its something to do with how he is using the sprites.

 

This is the check collision:

 boolean Pacman::CheckCollision(int x1, int y1, int width1, int height1, int x2, int y2, int width2, int height2)
{
	int left1 = x1;
	int left2 = x2;
	int right1 = x1 + width1;
	int right2 = x2 + width2;
	int top1 = y1;
	int top2 = y2;
	int bottom1 = y1 + height1;
	int bottom2 = y2 + height2;

	if (bottom1 < top2 && top1 > bottom2 && right1 < left2 && left1 > right2)
	{
		return true;
	}


}

This is the Munchie collision, the pacman pellets:

void Pacman::MunchieCollision()
{
	for (int i = 0; i < MUNCHIECOUNT; i++)
	{
		if(CheckCollision(_pacman->Position->X, _pacman->Position->Y, _pacman->Position->Width, 
			_pacman->Position->Height, _munchies[i]->MunchieRect->X, _munchies[i]->MunchieRect->Y, _munchies[i]->MunchieRect->Width, _munchies[i]->MunchieRect->Height) )
		{
			_munchies[i]->MunchieRect->X  = 1300;
			_munchies[i]->MunchieRect->Y  = 1300;
		}
	}


}

This is where the rectangle is located:

	// Load Pacman
	_pacman->Texture = new Texture2D();
	_pacman->Texture->Load("Textures/Pacman.tga", false);
	_pacman->Position = new Rect(350.0f, 350.0f, 32, 32);
	_pacman->SourceRect = new Rect(0.0f, 0.0f, 15, 15);

	//Load ghost
	_ghosts[0]->texture = new Texture2D();
	_ghosts[0]->texture->Load("Textures/GhostBlue.tga", true);
	_ghosts[0]->posRect = new Rect(rand() % Graphics::GetViewportWidth(), rand() % Graphics::GetViewportHeight(), 28, 28);
	_ghosts[0]->sourceRect = new Rect(0.0f, 0.0f, 16, 16);

	
	// Load Munchie
	Texture2D* munchieTexture = new Texture2D();
	munchieTexture->Load("Textures/MunchieText.png", true);
	for (int i = 0; i < MUNCHIECOUNT; i++)
	{
	
		_munchies[i]->MunchieTexture = munchieTexture;
		_munchies[i]->MunchieRect = new Rect(rand() % Graphics::GetViewportWidth(), rand() % Graphics::GetViewportHeight(), 20, 20);
		_munchies[i]->munchieSourceRect = new Rect(0.0f, 0.0f, 7, 7);

	}


he also wanted me to attach the game incase anyone wants to take a look :/

 

Thanks for your help

Pacman.rar

Edited by TEDWARDS
Added code

PC Specs:


CPU: i5 4670K  4.5 GHz  | CPU COOLER: H80i | GPU:EVGA GTX 780Ti  |  Motherboard: MSI Z87I   |  Case: Bitfenix Prodigy  |  RAM: 8GB Avexir Venom  |  HDD: Seagate 1TB , 60GB Agility 3  Monitor: 32" HD TV (LOL)   PSU: Corsair 750W

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not going to be downloading any arbitrary zip files I'm afraid. Moreover the code that you have pasted is absolute garbage I'm sorry to say; full of magic numbers that don't really mean anything to the one trying to decipher things. You should really have a think about your design and I would suggest that you have look at the Quadtree data structure since this is a common and efficient way to handle collision detection.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Please mention the language you're working with. It looks like C++ but the `boolean` return type for the first function disagrees, unless it is a typedef, we're not mind readers.

 

If it is C++, the first function is already broken, since it returns nothing if the ` if` condition evaluates as `false`. Your compiler should warn against that, if it does not, enable your compiler's `all warnings` mode. (-Wall on g++)

 

 

Link to comment
Share on other sites

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

×