Sample Code

Below I have included some collision code I created. It checks the locations of the bounding boxes of all objects against one another and reports if a collision occurs. You can download the source code here.


//-----------------------------------------------------------------------------

// Name: collisionCheck()

// Desc: This function takes the bounding boxes of two objects and compares them.

// if they collide it returns true. It only checks in the x & y planes.

//-----------------------------------------------------------------------------

bool collisionCheck(D3DXVECTOR3 playMin, D3DXVECTOR3 playMax, D3DXVECTOR3 obstMin, D3DXVECTOR3 obstMax)

{

    if((playMin.x >= obstMin.x && playMin.x <= obstMax.x)

      || (playMax.x >= obstMin.x && playMax.x <= obstMax.x))

       if((playMin.y >= obstMin.y && playMin.y <= obstMax.y)

         || (playMax.y >= obstMin.y && playMax.y <= obstMax.y))

          return true;

    return false;

}



//-----------------------------------------------------------------------------

// Name: updateBBox()

// Desc: This function gets the bounding box of an object and updates it to the scale and location

// in the world.

//-----------------------------------------------------------------------------

void updateBBox(D3DXVECTOR3 *minBounds,D3DXVECTOR3 *maxBounds,D3DXVECTOR3 scale,D3DXVECTOR3 loc)

{

    D3DXVECTOR3 ctr = D3DXVECTOR3(0.0f,0.0f,0.0f);

    D3DXVECTOR3 dist;


    dist.x = (maxBounds->x - minBounds->x)/2.0f;

    dist.y = (maxBounds->y - minBounds->y)/2.0f;

    dist.z = (maxBounds->z - minBounds->z)/2.0f;


    dist.x *= scale.x;

    dist.y *= scale.y;

    dist.z *= scale.z;


    minBounds->x = ctr.x - dist.x + loc.x;

    minBounds->y = ctr.y - dist.y + loc.y;

    minBounds->z = ctr.z - dist.z + loc.z;


    maxBounds->x = ctr.x + dist.x + loc.x;

    maxBounds->y = ctr.y + dist.y + loc.y;

    maxBounds->z = ctr.z + dist.z + loc.z;

}



//-----------------------------------------------------------------------------

// Name: doAllCollision()

// Desc: This function checks all of the location data of the two players and compares

// them against each other. If a collision occurs the correct scores are given to the players

// depending on the situation.

//-----------------------------------------------------------------------------

bool doAllCollision()

{

    D3DXVECTOR3 p_minBounds = g_pMeshData[yourCraft].minBounds;

    D3DXVECTOR3 p_maxBounds = g_pMeshData[yourCraft].maxBounds;

    updateBBox(&p_minBounds, &p_maxBounds, g_pMeshData[yourCraft].Scale,

                         g_pMeshData[yourCraft].Transform);


    D3DXVECTOR3 e_minBounds = g_pMeshData[enemyCraft].minBounds;

    D3DXVECTOR3 e_maxBounds = g_pMeshData[enemyCraft].maxBounds;

    updateBBox(&e_minBounds, &e_maxBounds, g_pMeshData[enemyCraft].Scale,

                         g_pMeshData[enemyCraft].Transform);

    //------------------------------------------------------

    //check everything against enemy

    //------------------------------------------------------

    if(collisionCheck(p_minBounds, p_maxBounds, e_minBounds, e_maxBounds))

    {

       //players collide, both die

       g_Player.incScore(-20);

       g_Enemy.incScore(-20);

       //player dies...

       KeyInput->stopMoving();

       Vector3D resetEye;

       Vector3D resetLookat;

       if(yourCraft==0)

       {

          g_pMeshData[yourCraft].Transform = D3DXVECTOR3( -113.0f, -20.0f, 0.0f );

         resetEye = Vector3D(-113.0f,4.0f,-80.0f);

         resetLookat = Vector3D(-113.0f,0.0f,5.0f);

         //enemy dies...

          g_pMeshData[enemyCraft].Transform = D3DXVECTOR3( 135.5f, -23.0f, 0.0f );

      }

       else

      {

          g_pMeshData[yourCraft].Transform = D3DXVECTOR3( 135.5f, -23.0f, 0.0f );

          resetEye = Vector3D(135.0f,4.0f,-80.0f);

          resetLookat = Vector3D(135.0f,0.0f,5.0f);

          //enemy dies...

          g_pMeshData[enemyCraft].Transform = D3DXVECTOR3( -113.0f, -20.0f, 0.0f );

      }


       g_Player.SetVDirection(STILL);

       g_Enemy.SetHDirection(STILL);

       KeyInput->setEye(resetEye);

       KeyInput->setLook(resetLookat);

       return 0;

    }

    //------------------------------------------------------

    //player's ammo against enemy

    if(!g_Player.canBomb())

    {

       D3DXVECTOR3 b_minBounds = g_pMeshData[3].minBounds;

       D3DXVECTOR3 b_maxBounds = g_pMeshData[3].maxBounds;

       D3DXVECTOR3 b_Loc = D3DXVECTOR3 (g_Player.BombX(),g_Player.BombY(),g_Player.BombZ());

       updateBBox(&b_minBounds,&b_maxBounds,g_pMeshData[3].Scale,b_Loc);

       if(collisionCheck(b_minBounds, b_maxBounds, e_minBounds, e_maxBounds))

       {

          //bomb hit enemy, building go boom, points ++

          g_Player.DestroyBomb();

          Vector3D tmp(0.0f,-999.0,0.0f);

          g_Player.BombCoords(tmp);

          g_Player.incScore(50);


          g_Enemy.SetHDirection(STILL);

          if(enemyCraft==0)

             g_pMeshData[enemyCraft].Transform = D3DXVECTOR3( -113.5f, -20.0f, 0.0f );

          else

             g_pMeshData[enemyCraft].Transform = D3DXVECTOR3( 135.5f, -23.0f, 0.0f );


       }

    }

    for(int j=0;j < g_Player.RoundsShot(); j++)

   {

       D3DXVECTOR3 b_minBounds = g_pMeshData[2].minBounds;

       D3DXVECTOR3 b_maxBounds = g_pMeshData[2].maxBounds;

       D3DXVECTOR3 b_Loc = D3DXVECTOR3 (g_Player.BulletX(j),g_Player.BulletY(j),g_Player.BulletZ(j));

       updateBBox(&b_minBounds,&b_maxBounds,g_pMeshData[2].Scale,b_Loc);

       if(collisionCheck(b_minBounds, b_maxBounds, e_minBounds, e_maxBounds))

       {

          //bullet hit something, building go boom, points ++

          g_Player.DestroyBullet();

          g_Player.incScore(50);


          //enemy dies...

          g_Enemy.SetHDirection(STILL);

          if(enemyCraft==0)

             g_pMeshData[enemyCraft].Transform = D3DXVECTOR3( -113.5f, -20.0f, 0.0f );

          else

             g_pMeshData[enemyCraft].Transform = D3DXVECTOR3( 135.5f, -23.0f, 0.0f );

       }

    }


    //------------------------------------------------------

    //enemy's ammo against enemy

    if(!g_Enemy.canBomb())

    {

       D3DXVECTOR3 b_minBounds = g_pMeshData[3].minBounds;

       D3DXVECTOR3 b_maxBounds = g_pMeshData[3].maxBounds;

       D3DXVECTOR3 b_Loc = D3DXVECTOR3 (g_Player.BombX(),g_Player.BombY(),g_Player.BombZ());

       updateBBox(&b_minBounds,&b_maxBounds,g_pMeshData[3].Scale,b_Loc);

       if(collisionCheck(b_minBounds, b_maxBounds, p_minBounds, p_maxBounds))

       {

          //bomb hit enemy, building go boom, points ++

          g_Enemy.DestroyBomb();

          Vector3D tmp(0.0f,-999.0,0.0f);

          g_Enemy.BombCoords(tmp);

          g_Enemy.incScore(50);


          g_Player.SetHDirection(STILL);

          if(yourCraft==0)

             g_pMeshData[0].Transform = D3DXVECTOR3( -113.0f, -20.0f, 0.0f );

          else

             g_pMeshData[0].Transform = D3DXVECTOR3( 135.5f, -23.0f, 0.0f );

       }

    }

    for(int j=0;j < g_Enemy.RoundsShot(); j++)

    {

       D3DXVECTOR3 b_minBounds = g_pMeshData[2].minBounds;

       D3DXVECTOR3 b_maxBounds = g_pMeshData[2].maxBounds;

       D3DXVECTOR3 b_Loc = D3DXVECTOR3(g_Enemy.BulletX(j),g_Enemy.BulletY(j),g_Enemy.BulletZ(j));

       updateBBox(&b_minBounds,&b_maxBounds,g_pMeshData[2].Scale,b_Loc);

       if(collisionCheck(b_minBounds, b_maxBounds, p_minBounds, p_maxBounds))

       {

          //bullet hit something, building go boom, points ++

          g_Enemy.DestroyBullet();

          g_Enemy.incScore(50);


          //player dies...

          g_Player.SetHDirection(STILL);

          g_Player.SetVDirection(STILL);

          if(yourCraft==0)

             g_pMeshData[0].Transform = D3DXVECTOR3( -113.0f, -20.0f, 0.0f );

          else

             g_pMeshData[0].Transform = D3DXVECTOR3( 135.5f, -23.0f, 0.0f );

       }

    }



    //------------------------------------------------------

    //check everything against buildings

    //------------------------------------------------------

    for(int i=0;i    {

       if(g_build[i].show)

       {

          D3DXVECTOR3 minBounds = g_pMeshData[4+g_build[i].type].minBounds;

          D3DXVECTOR3 maxBounds = g_pMeshData[4+g_build[i].type].maxBounds;

          updateBBox(&minBounds,&maxBounds,g_pMeshData[4+g_build[i].type].Scale,g_build[i].BuildPos);


          if(collisionCheck(p_minBounds, p_maxBounds, minBounds, maxBounds))

          {

             //player hit building... he dies, building goes boom

             int tmp = 0;

             g_build[i].show = false;

             curNumBuilding--;

             g_Player.incScore(-20);


             Vector3D resetEye;

             Vector3D resetLookat;


             KeyInput->stopMoving();

             if(yourCraft==0)

             {

                g_pMeshData[yourCraft].Transform = D3DXVECTOR3( -113.0f, -20.0f, 0.0f );

                resetEye = Vector3D(-113.0f,4.0f,-80.0f);

                resetLookat = Vector3D(-113.0f,0.0f,5.0f);

             }

             else

             {

                g_pMeshData[yourCraft].Transform = D3DXVECTOR3( 135.0f, -23.0f, 0.0f );

                resetEye = Vector3D(135.0f,4.0f,-80.0f);

                resetLookat = Vector3D(135.0f,0.0f,5.0f);

             }


             KeyInput->setEye(resetEye);

             KeyInput->setLook(resetLookat);


             return 0;

          }

          if(!g_Player.canBomb())

          {

             D3DXVECTOR3 b_minBounds = g_pMeshData[3].minBounds;

             D3DXVECTOR3 b_maxBounds = g_pMeshData[3].maxBounds;

             D3DXVECTOR3 b_Loc =

                                                 D3DXVECTOR3(g_Player.BombX(),g_Player.BombY(),g_Player.BombZ());

             updateBBox(&b_minBounds,&b_maxBounds,g_pMeshData[3].Scale,b_Loc);

             if(collisionCheck(b_minBounds, b_maxBounds, minBounds, maxBounds))

             {

                //bomb hit something, building go boom, points ++

                g_Player.DestroyBomb();

                Vector3D tmp(0.0f,-999.0,0.0f);

                g_Player.BombCoords(tmp);

                g_build[i].show = false;

                curNumBuilding--;

                g_Player.incScore(20);

             }

          }

          for(int j=0;j < g_Player.RoundsShot(); j++)

          {

             D3DXVECTOR3 b_minBounds = g_pMeshData[2].minBounds;

             D3DXVECTOR3 b_maxBounds = g_pMeshData[2].maxBounds;

             D3DXVECTOR3 b_Loc =

                                               D3DXVECTOR3 (g_Player.BulletX(j),g_Player.BulletY(j),g_Player.BulletZ(j));

             updateBBox(&b_minBounds,&b_maxBounds,g_pMeshData[2].Scale,b_Loc);

             if(collisionCheck(b_minBounds, b_maxBounds, minBounds, maxBounds))

             {

                //bullet hit something, building go boom, points ++

                g_Player.DestroyBullet();

                g_build[i].show = false;

                curNumBuilding--;

                g_Player.incScore(20);

             }

          }

       }

    }

}



//-----------------------------------------------------------------------------

// Name: replaceBuildings()

// Desc: Checks if the maximum number of buildings are showing, if not a timer is

// started and the buildings respawn accordingly.

//-----------------------------------------------------------------------------

void replaceBuildings()

{

    if(curNumBuilding < NUM_BUILD && !newBuilding)

    {

       buildWait = clock() + TIME_BUILD * CLOCKS_PER_SEC;

       newBuilding = true;

    }

    if(newBuilding)

    {

       if(clock() >= buildWait)

       {

          if(buildNext ==13)

             buildNext = 0;

          if(!g_build[buildNext].show)

          {

             g_build[buildNext].show = true;

             curNumBuilding++;

             newBuilding = false;

          }

          buildNext++;

       }

    }

}