»ç°¢Çü°ú ¿øÀÇ Ãæµ¹

MyEngine ¹öÀüÀ» ver 1.03À¸·Î ¿Ã·È´Ù.

¿ø°ú¿øÀÇ Ãæµ¹Àº °£´ÜÇÏ´Ù.
ÇÏÁö¸¸ »ç°¢Çü°ú ¿øÀÇ Ãæµ¹Àº °£´Ü ÇÒ °Í °°Áö¸¸ ½ÇÁ¦·Î ±¸ÇöÇغ¸¸é °£´ÜÇÏÁö°¡ ¾Ê´Ù.
±× ÇعýÀº ¾Æ·¡ »çÀÌÆ®¿¡ ÀÖ´Ù.

http://hq.scene.ro/blog/read/circle-box-intersection-revised/



±×¸²Ã³·³ 4¹ø ¿µ¿ªÀÇ »ç°¢ÇüÀ» ±âÁØÀ¸·Î 9°³ ¿µ¿ªÀ¸·Î ³ª´«´Ù.
»ç°¢Çü°ú ¿øÀÌ Ãæµ¹ÇÏ´Â °æ¿ì´Â ¼¼°¡Áö °æ¿ìÀÌ´Ù.

1. ¿øÀÇ Áß½ÉÀÌ »ç°¢Çü¿¡ Æ÷ÇԵǴ °æ¿ì (4¹ø ¿µ¿ª)

¿øÀÇ Áß½ÉÀÌ »ç°¢Çü left, bottomº¸´Ù Å©°í, right, topº¸´Ù ÀÛÀºÁö °Ë»çÇÑ´Ù.

2. ¿øÀÇ Áß½ÉÀÌ »ç°¢Çü ¹ÛÀÇ ¸ð¼­¸®¿¡ ÀÖ´Â °æ¿ì (0, 2, 6, 8¹ø ¿µ¿ª)

¿øÀÌ ÀÌ ¿µ¿ª¿¡ ÀÖÀ» ¶§, ¿ø¿¡ °¡Àå °¡±î¿î Á¡Àº »ç°¢ÇüÀÇ ¸ð¼­¸®ÀÌ´Ù.
»ç°¢ÇüÀÇ ¸ð¼­¸®°¡ ¿ø¿¡ Æ÷ÇÔ µÇ´ÂÁö °Ë»çÇÑ´Ù.

3. ¿øÀÇ Áß½ÉÀÌ »ç°¢Çü º¯¿¡ ÀÖ´Â °æ¿ì (1, 3, 5, 7¹ø ¿µ¿ª)

xÃàÀ̳ª yÃàÀÇ ÇÑÃàÀÇ »ç°¢Çü°ú ¿ø »çÀÌÀÇ ±æÀÌ°¡
xÃàÀ̳ª yÃàÀÇ »ç°¢Çü ¹ÝÁö¸§°ú ¿øÀÇ ¹ÝÁö¸§º¸´Ù ÀÛÀ¸¸é ±³Â÷ÇÑ´Ù.

int MyRect::GetRectZone( float circleX, float circleY )

{

    int xZone = ( circleX <  min.x ) ? 0 :          

                ( circleX >  max.x ) ? 2 : 1;

    int yZone = ( circleY <  min.y ) ? 0 :          

                ( circleY >  max.y ) ? 2 : 1;

    int nZone = xZone + 3*yZone;

    return nZone;

}

 

bool MyRect::IntersectCircle( MyCircle& circle )

{

    bool collisionDetected = false;

    int  nZone = GetRectZone( circle.x, circle.y );

    MyVector2 box = GetCenter();

    float halfHeight = GetHeight();

    float halfWidth = GetWidth();

 

    switch (nZone )

    {

    // top, bottom º¯ÀÇ ¿µ¿ª¿¡¼­, ¿øÀÇ ¼¾ÅÍ¿Í ¼öÁ÷°Å¸®¸¦ °Ë»çÇÑ´Ù.

    case 1:

    case 7:

        {

            float distY = fabs( circle.y - box.y );

            if( distY <= ( circle.radius + halfHeight ) )

                collisionDetected = true;

        }

        break;

    // left, right º¯ÀÇ ¿µ¿ª¿¡¼­. ¿øÀÇ ¼¾ÅÍ¿Í ¼öÆò°Å¸®¸¦ °Ë»çÇÑ´Ù.

    case 3:

    case 5:

        {

            float distX = fabs( circle.x - box.x );

            if( distX <= ( circle.radius + halfWidth ) )

                collisionDetected = true;

        }

        break;

    // »ç°¢Çü ¿µ¿ªÀÇ ³»ºÎ

    case 4:

        collisionDetected = true;

        break;

    // ¸ð¼­¸® ¿µ¿ª, ¸ð¼­¸®°¡ ¿øÀÇ ³»ºÎ¿¡ Æ÷ÇԵǴÂÁö °Ë»çÇÑ´Ù.

    default:

        {

            float cornerX = ( nZone == 0 || nZone == 6 ) ? box.x - halfWidth : box.x + halfWidth;

            float cornerY = ( nZone == 0 || nZone == 2 ) ? box.y - halfHeight : box.y + halfHeight;

            if( circle.IncludePoint( cornerX, cornerY ) )   

                collisionDetected = true;

        }

        break;

    }

 

    return collisionDetected;

}