Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bounding Box Collision, Help Please

Started by chaosvine Jul 4, 2013 at 6:29 PM 2 replies 1.8k views
Original Post
chaosvine
chaosvine

I have run into a problem that I am unable to resolve while writing a small DirectX 9 game involving the Collision handling portion of the game.

I have 1 ball that I can control, and 2 balls that bounce around the screen. I want them to also bounce off each other and the one I control without moving it.

This is my code so far:


void Game::Update(float dt)
{
	float centeraX;
	float centeraY;
	float centerbX;
	float centerbY;

    double overX;
    double overY;

	//Retrieve the keyboard array
	ZeroMemory(m_keys, sizeof(m_keys));

	for(int a = 0; a < MAX_SPRITES; a++)
	{

		switch(a)
		{

		/****************************************************/
		/*				Update the main sprite				*/
		/****************************************************/

		case MAIN_SPRITE:

			//Check the keyboard and see if any keys are pressed
			switch(m_pInput->GetDeviceState(sizeof(m_keys), m_keys))
			{
			
			//If both the up and left keys are pressed
			case UPLEFTDIRECTION:

				m_Sprites[a].m_MoveY = -2;
				m_Sprites[a].m_MoveX = -2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If both the up and right keys are pressed
			case UPRIGHTDIRECTION:

				m_Sprites[a].m_MoveY = -2;
				m_Sprites[a].m_MoveX = 2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If both the down and left keys are pressed
			case DOWNLEFTDIRECTION:

				m_Sprites[a].m_MoveY = 2;
				m_Sprites[a].m_MoveX = -2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If both the down and right keys are pressed
			case DOWNRIGHTDIRECTION:

				m_Sprites[a].m_MoveY = 2;
				m_Sprites[a].m_MoveX = 2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If the up key is pressed
			case UPDIRECTION:

				m_Sprites[a].m_MoveY = -2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If the down key is pressed
			case DOWNDIRECTION:

				m_Sprites[a].m_MoveY = 2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If the left key is pressed
			case LEFTDIRECTION:

				m_Sprites[a].m_MoveX = -2;
				m_Sprites[a].m_IsMoving = true;
				break;

			//If the right key is pressed
			case RIGHTDIRECTION:

				m_Sprites[a].m_MoveX = 2;
				m_Sprites[a].m_IsMoving = true;
				break;

			default:
				
				m_Sprites[a].m_MoveX = 0;
				m_Sprites[a].m_MoveY = 0;
				m_Sprites[a].m_IsMoving = false;
				break;

			}

			/******************************************/
			/*			Collision Detection			  */
			/******************************************/

			if(m_Sprites[a].OnScreen() == TOP)
			{	

				m_Sprites[a].m_Pos.y = 0;
				m_Sprites[a].m_MoveY = 0;

			}

			if(m_Sprites[a].OnScreen() == BOTTOM)
			{
					
				m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height;
				m_Sprites[a].m_MoveY = 0;

			}

			if(m_Sprites[a].OnScreen() == LEFT)
			{

				m_Sprites[a].m_Pos.x = 0;
				m_Sprites[a].m_MoveX = 0;

			}

			if(m_Sprites[a].OnScreen() == RIGHT)
			{

				m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width;
				m_Sprites[a].m_MoveX = 0;

			}

			for(int b = 0; b < MAX_SPRITES; b++)
			{

				if(m_Sprites[a].m_Occupied && a != b && !m_Sprites[a].m_CollisionStatus)
				{

					switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b]))
					{

					case TOPLEFT:

						overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left;
						overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" TOPLEFT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";
					
						}

						m_Sprites[a].m_MoveY = 0;
						m_Sprites[a].m_MoveX = 0;
						m_Sprites[b].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[b].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();

						}

						break;

					case TOPRIGHT:

						overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left;
						overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" TOPRIGHT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_MoveY = 0;
						m_Sprites[a].m_MoveX = 0;
						m_Sprites[b].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[b].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

						}

						g_debug.close();

						break;

					case BOTTOMLEFT:

						overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left;
						overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" BOTTOMLEFT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_MoveY = 0;
						m_Sprites[a].m_MoveX = 0;
						m_Sprites[b].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[b].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();
						
						}

						break;

					case BOTTOMRIGHT:

						overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left;
						overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" BOTTOMRIGHT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_MoveY = 0;
						m_Sprites[a].m_MoveX = 0;
						m_Sprites[b].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[b].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();
						
						}

						break;
					
					}

				}

			}

			/*********************************************************/
			/*				Update the Sprite's Position			 */
			/*********************************************************/

			m_Sprites[a].UpdateSpritePos(m_Rate);

			break;

		//Update GreenBall1
		case GREEN_BALL1:
			
			if(m_Sprites[a].OnScreen() == TOP)
			{

				m_Sprites[a].m_Pos.y = 0;
				m_Sprites[a].m_MoveY *= -1;

			}

			if(m_Sprites[a].OnScreen() == BOTTOM)
			{

				m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height;
				m_Sprites[a].m_MoveY *= -1;

			}

			if(m_Sprites[a].OnScreen() == LEFT)
			{

				m_Sprites[a].m_Pos.x = 0;
				m_Sprites[a].m_MoveX *= -1;

			}

			if(m_Sprites[a].OnScreen() == RIGHT)
			{

				m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width;
				m_Sprites[a].m_MoveX *= -1;

			}

			for(int b = 0; b < MAX_SPRITES; b++)
			{

				if(m_Sprites[a].m_Occupied && a != b && !m_Sprites[a].m_CollisionStatus)
				{

					switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b]))
					{

					case TOPLEFT:

						overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left;
						overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" TOPLEFT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<g_CollisionCount<<" After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();

						}

						break;

					case TOPRIGHT:

						overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left;
						overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" TOPRIGHT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();

						}

						break;

					case BOTTOMLEFT:

						overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left;
						overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" BOTTOMLEFT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

						}

						g_debug.close();

						break;

					case BOTTOMRIGHT:

						overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left;
						overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" BOTTOMRIGHT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

						}

						g_debug.close();

						break;
					
					}

				}

			}

			if(!m_Sprites[a].m_CollisionStatus)
			{
				m_Sprites[a].UpdateSpritePos(m_Rate);
			}
			else
			{
				m_Sprites[a].m_CollisionStatus = false;
			}

			break;

		//Update GreenBall2
		case GREEN_BALL2:

			if(m_Sprites[a].OnScreen() == TOP)
			{

				m_Sprites[a].m_Pos.y = 0;
				m_Sprites[a].m_MoveY *= -1;

			}

			if(m_Sprites[a].OnScreen() == BOTTOM)
			{

				m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height;
				m_Sprites[a].m_MoveY *= -1;

			}

			if(m_Sprites[a].OnScreen() == LEFT)
			{

				m_Sprites[a].m_Pos.x = 0;
				m_Sprites[a].m_MoveX *= -1;

			}

			if(m_Sprites[a].OnScreen() == RIGHT)
			{

				m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width;
				m_Sprites[a].m_MoveX *= -1;

			}

			for(int b = 0; b < MAX_SPRITES; b++)
			{

				if(m_Sprites[a].m_Occupied && a != b && !m_Sprites[a].m_CollisionStatus)
				{

					switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b]))
					{

					case TOPLEFT:

						overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left;
						overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" TOPLEFT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();

						}

						break;

					case TOPRIGHT:

						overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left;
						overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" TOPRIGHT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

						}

						g_debug.close();

						break;

					case BOTTOMLEFT:

						overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left;
						overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" BOTTOMLEFT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"After Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

						}

						g_debug.close();

						break;

					case BOTTOMRIGHT:

						overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left;
						overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top;

						g_CollisionCount++;

						if(m_debug)
						{

							g_debug.open("debug.txt", std::ofstream::app);

							g_debug<<g_CollisionCount<<" BOTTOMRIGHT\n\n";

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug<<"OverX = "<<overX<<"\n";
							g_debug<<"OverY = "<<overY<<"\n\n";

						}

						m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1;
						m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1;
						m_Sprites[a].m_MoveY *= -1;
						m_Sprites[a].m_MoveX *= -1;
						m_Sprites[b].m_MoveY *= -1;
						m_Sprites[b].m_MoveX *= -1;
						m_Sprites[a].UpdateSpritePos(m_Rate);
						m_Sprites[b].UpdateSpritePos(m_Rate);
						m_Sprites[b].m_CollisionStatus = true;

						if(m_debug)
						{

							g_debug<<"Before Collision:\n\n";

							g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n";
							g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n";
							g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n";
							g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n";
							g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n";
							g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n";

							g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n";
							g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n";
							g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n";
							g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n";
							g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n";
							g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n";

							g_debug.close();

						}

						break;
					
					}

				}

			}

			if(!m_Sprites[a].m_CollisionStatus)
			{
				m_Sprites[a].UpdateSpritePos(m_Rate);
			}
			else
			{
				m_Sprites[a].m_CollisionStatus = false;
			}

			break;

		}

	}

}

int Sprite::GetCollisionStatus(Sprite* sprite)
{
	int collisionstatus = NULL;
	RECT a;
	RECT b;

	a.top = m_Bounding.top;
	a.bottom = m_Bounding.bottom;
	a.left = m_Bounding.left;
	a.right = m_Bounding.right;

	b.top = sprite->m_Bounding.top;
	b.bottom = sprite->m_Bounding.bottom;
	b.left = sprite->m_Bounding.left;
	b.right = sprite->m_Bounding.right;

	if(m_Index != sprite->m_Index)
	{

		if(a.bottom < b.top || a.top > b.bottom || a.right < b. left || a.left > b.right)
		{
			collisionstatus = NOCOLLISION;
		}
		else
		{

			//If A's top is less than B's top then A is overlapping B's top
			if(a.top < b.top)
			{

				collisionstatus += TOP;
			
			}
			
			//If A's top is greater than B's top then A is overlapping B's bottom
			if(a.top > b.top)
			{

				collisionstatus += BOTTOM;
			
			}
			
			//If A's left is less than B's left then A is overlapping B's left
			if( a.left < b.left)
			{

				collisionstatus += LEFT;
			
			}
			
			//If A's left is greater than B's left then A is overlapping B's right
			if( a.left > b.left)
			{

				collisionstatus += RIGHT;
			
			}

		}

	
	}

	return collisionstatus;

}

void Sprite::UpdateSpritePos(float dt)
{

	m_AnimRate = dt;
	m_Pos.x				+= m_MoveX * m_AnimRate;
	m_Pos.y				+= m_MoveY * m_AnimRate;
	m_Bounding.top		= m_Pos.y + m_BoundingOffsetH;
	m_Bounding.left		= m_Pos.x + m_BoundingOffsetW;
	m_Bounding.bottom	= m_Pos.y + m_OffsetHeight;
	m_Bounding.right	= m_Pos.x + m_OffsetWidth;

}

The balls are not behaving as they should because multiple collisions are occuring when they touch and I don't know why. I have exhaustively looked through my code and algorithms, and looked through online collision detection resources and I can not figure out what I am doing wrong. Please Help if you can.

Norman Barrows
Norman Barrows

here's how i would do it:

1. move the players's ball.

2. process any collisions resulting from moving player's ball.

3. move green ball #1.

4. process any collisions resulting from moving green ball #1.

5. move green ball #2.

6. process any collisions resulting from moving green ball #2.

each ball would have an x,y position, and a bounding box radius (say 25 pixels).

then you need to select a level of accuracy for the collision checks:

1. bounding box distance, and bounding boxes. low accuracy, fast, all ints.

2. bounding sphere and true 2d distance. slower, more accurate (especially diagonally). uses floats and square root. speed should not be an issue for just 3 objects. bounding sphere will give you ~"pixel perfect" collisions with circular sprites.

3. pixel vs pixel collision (used for irregularly shaped sprites). first you do a bbox or bsphere check, then check pix vs pix if the first check indicates collision.

start with method #1, the math is easy.

definition of BBox distance:

greater of Dx or Dy between two points.

ie:


 
int BBdist(int x1,int y1,int x2,int y2)
{
int dx,dy;
dx=x2-x1;
dy=y2-y1;
if (dx>dy) return(dx) 
return(dy);
}
 
 

so now you want to check for a collision between 2 balls:

lets say the bbox radius of a ball is 25 pixels.


 
#define ball_bbox_radius  25
 
 
// checks for collision between two balls at x1,y and x2,y2. returns 1 on collision, else returns 0.
int collided(int x1,int y1,int x2,int y2)
{
int d;
d=BBdist(x1,y1,x2,y2);
if (d<ball_bbox-radius*2) return(1);
return(0);
}
 
 

once you move a ball (such as ball #1) you then check for collisions between it and the other balls:


 
if (collided(ball1x,ball1y,player_ball_x,player_ball_y))
    {
    // handle collision with player ball
    return;
    }
else if (collided(ball1x,ball1y,ball2x,ball2y))

    {
    // handle collision with ball #2
    }
 
 
Norm Barrows Rockland Software Productions "Building PC games since 1989"</
chaosvine
chaosvine

Hi, thank you for responding. :)

I'm a little unsure of what it is you're saying. As far as I can tell I am already doing what you are saying (or attempting to), it's just not working for me.

My first switch for and switch loop itirates through all the sprites, but it updates their movement and then handles collisions that occur (for each ball).

My problem as far as I can tell is not in the collision detection, my algorithm there already works using the bounding boxes, it's in the response to the collision. I am trying to seperate the balls accurately when they collide, however multiple collisions are being detected when I try to move them, so they bounce around eachother.

Interestingly if the balls touch pretty much completely diagnoly (where the corners touch), there is no bouncing.

This tells me there is likely something wrong with the way I am trying to seperate them. I have tried almost everything I can think of and still it does not work.

What I am currently doing is calculating the overlaping distance of the collision when it occurs and then moving either A (if it's one of the green balls being collided with) or B (if it's the main sprite being collided with). Then I reverse the velocities of the balls so that in theory they move away from eachother. This is however not working in all cases and I do not know why. Sometimes only 1 collision registers (I display the CollisionCounts variable to the screen so I can see it increase when they touch), and sometimes I get as many as 3 or 4 collisions at once and the balls kind of blip as a result.

Norman Barrows
Norman Barrows

My problem as far as I can tell is not in the collision detection, my algorithm there already works using the bounding boxes, it's in the response to the collision.
ah! ok, that's totally different.
ok first off, there are 2 ways to do collision checks:
1. move everything (all 3 balls), then see what overlaps, then try to backsolve and figure out what should have happened. this seems to the the current popular approach. i don't use it, as it seems to be a bit unnecessarily complex to me. however, i've never given it a fully evaluation. there may be some savings over other methods.
2. calculate the new location of one object, then check for collisions with the new location and other objects. do so for each object in turn.
either way you do it, you have all the necessary info to figure out what should happen.
whichever way you do it, the next step after detecting a collision is to determine the exact point of collision.
with method 2, your object hasn't moved yet, but you know that moving it the full distance results in collision. so you step move at the resolution you want for collisions (say 1 pixel) until the collision occurs. at this point you have the position, velocity, and mass of two inelastic rigid bodies in collision. then simply apply the appropriate physics formulas. any decent physics text will have them. its the one that uses billiard balls as examples.
with method 1, its a bit more work, since both objects have moved. first, you move them both back to their original positions. then you step move both at once til they collide. then you use those positions, velocities, and masses to solve for the new velocity vectors after collision, as in method 2.
depending on the situation, it can sometimes be possible to directly calculate the exact point of collision from the geometry, without step moving.
Norm Barrows Rockland Software Productions "Building PC games since 1989"</

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.