예제 #1
0
//--------------------------------------------------------------------------------------
// Test collisions between pairs of collision objects using XNACollision functions
//--------------------------------------------------------------------------------------
void Collide()
{
    // test collisions between objects and frustum
    g_SecondarySpheres[0].collision = g_PrimaryFrustum.Contains( g_SecondarySpheres[0].sphere );
    g_SecondaryOrientedBoxes[0].collision = g_PrimaryFrustum.Contains( g_SecondaryOrientedBoxes[0].obox );
    g_SecondaryAABoxes[0].collision = g_PrimaryFrustum.Contains( g_SecondaryAABoxes[0].aabox );
    g_SecondaryTriangles[0].collision = g_PrimaryFrustum.Contains( g_SecondaryTriangles[0].pointa,
                                                                   g_SecondaryTriangles[0].pointb,
                                                                   g_SecondaryTriangles[0].pointc );

    // test collisions between objects and aligned box
    g_SecondarySpheres[1].collision = g_PrimaryAABox.Contains( g_SecondarySpheres[1].sphere );
    g_SecondaryOrientedBoxes[1].collision = g_PrimaryAABox.Contains( g_SecondaryOrientedBoxes[1].obox );
    g_SecondaryAABoxes[1].collision = g_PrimaryAABox.Contains( g_SecondaryAABoxes[1].aabox );
    g_SecondaryTriangles[1].collision = g_PrimaryAABox.Contains( g_SecondaryTriangles[1].pointa,
                                                                 g_SecondaryTriangles[1].pointb,
                                                                 g_SecondaryTriangles[1].pointc );

    // test collisions between objects and oriented box
    g_SecondarySpheres[2].collision = g_PrimaryOrientedBox.Contains( g_SecondarySpheres[2].sphere );
    g_SecondaryOrientedBoxes[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryOrientedBoxes[2].obox );
    g_SecondaryAABoxes[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryAABoxes[2].aabox );
    g_SecondaryTriangles[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryTriangles[2].pointa,
                                                                       g_SecondaryTriangles[2].pointb,
                                                                       g_SecondaryTriangles[2].pointc );

    // test collisions between objects and ray
    float fDistance = -1.0f;

    float fDist;
    if ( g_SecondarySpheres[3].sphere.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
    {
        fDistance = fDist;
        g_SecondarySpheres[3].collision = INTERSECTS;
    }
    else
        g_SecondarySpheres[3].collision = DISJOINT;

    if ( g_SecondaryOrientedBoxes[3].obox.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
    {
        fDistance = fDist;
        g_SecondaryOrientedBoxes[3].collision = INTERSECTS;
    }
    else
        g_SecondaryOrientedBoxes[3].collision = DISJOINT;

    if ( g_SecondaryAABoxes[3].aabox.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
    {
        fDistance = fDist;
        g_SecondaryAABoxes[3].collision =  INTERSECTS;
    }
    else
        g_SecondaryAABoxes[3].collision =  DISJOINT;

    if ( TriangleTests::Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction,
                                    g_SecondaryTriangles[3].pointa,
                                    g_SecondaryTriangles[3].pointb,
                                    g_SecondaryTriangles[3].pointc,
                                    fDist ) )
    {
        fDistance = fDist;
        g_SecondaryTriangles[3].collision = INTERSECTS;
    }
    else
        g_SecondaryTriangles[3].collision = DISJOINT;

    // If one of the ray intersection tests was successful, fDistance will be positive.
    // If so, compute the intersection location and store it in g_RayHitResultBox.
    if( fDistance > 0 )
    {
        // The primary ray's direction is assumed to be normalized.
        XMVECTOR HitLocation = XMVectorMultiplyAdd( g_PrimaryRay.direction, XMVectorReplicate( fDistance ),
                                                    g_PrimaryRay.origin );
        XMStoreFloat3( &g_RayHitResultBox.aabox.Center, HitLocation );
        g_RayHitResultBox.collision = INTERSECTS;
    }
    else
    {
        g_RayHitResultBox.collision = DISJOINT;
    }
}