Пример #1
0
bool LineCheckForBoxOnIsle2(const Vect2 &minVal, const Vect2 &maxVal, const Vect2 &v1, const Vect2 &v2)
{
    float tMax = M_INFINITE;
    float tMin = -M_INFINITE;

    Vect2 rayVect = v2-v1;
    float rayLength = rayVect.Len();
    Vect2 rayDir = rayVect*(1.0f/rayLength);

    Vect2 center = ((maxVal-minVal)*0.5f)+minVal;
    Vect2 E = maxVal - center;
    Vect2 T = center-v1;

    for(int i=0; i<2; i++)
    {
        float e = T.ptr[i];
        float f = rayDir.ptr[i];
        float fI = 1.0f/f;

        if(fabs(f) > 0.0f)
        {
            float t1 = (e+E.ptr[i])*fI;
            float t2 = (e-E.ptr[i])*fI;
            if(t1 > t2)
            {
                if(t2 > tMin) tMin = t2;
                if(t1 < tMax) tMax = t1;
            }
            else
            {
                if(t1 > tMin) tMin = t1;
                if(t2 < tMax) tMax = t2;
            }
            if(tMin > tMax) return false;
            if(tMax < 0.0f) return false;
        }
        else if( ((-e - E.ptr[i]) > 0.0f) ||
                 ((-e + E.ptr[i]) < 0.0f) )
        {
            return FALSE;
        }

        if(tMin > rayLength) return false;
    }

    return true; 
};
Пример #2
0
BOOL Line2::LinesIntersect(const Line2 &collider) const
{
    //this looks more complex than it really is.  I'm just an optimization freak.
    //...okay, this probably isn't the kind of function that's called every frame or something.
    //but I'm compulsive about optimization!  ..okay, I'll make it cleaner one of these days.

    Vect2 vec = (A-B);
    float len = vec.Len();
    Vect2 norm = (vec/len);
    Vect2 cross = norm.GetCross();
    float dist = cross.Dot(A);

    float fADist = collider.A.Dot(cross)-dist;
    float fBDist = collider.B.Dot(cross)-dist;

    BOOL bAAbove = (fADist > -EPSILON);
    BOOL bBAbove = (fBDist > -EPSILON);

    BOOL bFoundZeroLine1 = FALSE;

    if( (fabs(fADist) < EPSILON) ||
        (fabs(fBDist) < EPSILON) )
    {
        bFoundZeroLine1 = TRUE;
    }

    if(bAAbove == bBAbove)
        return FALSE;

    vec = (collider.A-collider.B);
    len = vec.Len();
    norm = (vec/len);
    cross = norm.GetCross();
    dist = cross.Dot(collider.A);

    fADist = A.Dot(cross)-dist;
    fBDist = B.Dot(cross)-dist;

    bAAbove = (fADist > -EPSILON);
    bBAbove = (fBDist > -EPSILON);

    if(fabs(fADist) < EPSILON)
        bAAbove = bBAbove;
    if(fabs(fBDist) < EPSILON)
        bBAbove = bAAbove;

    BOOL bFoundZeroLine2 = FALSE;

    if( (fabs(fADist) < EPSILON) ||
        (fabs(fBDist) < EPSILON) )
    {
        bFoundZeroLine2 = TRUE;
    }

    if(bAAbove == bBAbove)
        return FALSE;

    if(bFoundZeroLine1 && bFoundZeroLine2)
        return FALSE;

    return TRUE;
}