How far to move a point to get it out of the view frustum?

33 views Asked by At

I'm creating a view frustum from a combo matrix (function at the end of the question for reference).

What I want to do is, I have an object at xyz... and a direction vector abc, both of these in world space. I want to know how far to move xyz along abc to put it outside the view frustum-- i.e. colliding with one of the planes of the view frustum.

I'm currently doing this by just moving the object by an interval and checking it's still in the frustum. That seems clumsy and slow.

How can I either convert the view frustum into world space, or, alternately, convert the xyz/abc into clip space, do the plane collision, and convert it back again?

Here's the function via which I create the frustum:

void ViewFrustum::FromMatrix(Matrix& theMatrix, float theMult)
{
    Plane* aP;
    Vector aCenter;

    aP=&mPlane[FRUSTRUM_RIGHT];
    aP->mPos.mX=theMatrix.mData.mm[3]-theMatrix.mData.mm[0];
    aP->mPos.mY=theMatrix.mData.mm[7]-theMatrix.mData.mm[4];
    aP->mPos.mZ=theMatrix.mData.mm[11]-theMatrix.mData.mm[8];
    aP->mD=theMatrix.mData.mm[15]-theMatrix.mData.mm[12];
    aCenter+=aP->mPos;

    aP=&mPlane[FRUSTRUM_LEFT];
    aP->mPos.mX=theMatrix.mData.mm[3]+theMatrix.mData.mm[0];
    aP->mPos.mY=theMatrix.mData.mm[7]+theMatrix.mData.mm[4];
    aP->mPos.mZ=theMatrix.mData.mm[11]+theMatrix.mData.mm[8];
    aP->mD=theMatrix.mData.mm[15]+theMatrix.mData.mm[12];
    aCenter+=aP->mPos;

    aP=&mPlane[FRUSTRUM_BOTTOM];
    aP->mPos.mX=theMatrix.mData.mm[3]+theMatrix.mData.mm[1];
    aP->mPos.mY=theMatrix.mData.mm[7]+theMatrix.mData.mm[5];
    aP->mPos.mZ=theMatrix.mData.mm[11]+theMatrix.mData.mm[9];
    aP->mD=theMatrix.mData.mm[15]+theMatrix.mData.mm[13];
    aCenter+=aP->mPos;

    aP=&mPlane[FRUSTRUM_TOP];
    aP->mPos.mX=theMatrix.mData.mm[3]-theMatrix.mData.mm[1];
    aP->mPos.mY=theMatrix.mData.mm[7]-theMatrix.mData.mm[5];
    aP->mPos.mZ=theMatrix.mData.mm[11]-theMatrix.mData.mm[9];
    aP->mD=theMatrix.mData.mm[15]-theMatrix.mData.mm[13];
    aCenter+=aP->mPos;

    aP=&mPlane[FRUSTRUM_FAR];
    aP->mPos.mX=theMatrix.mData.mm[3]-theMatrix.mData.mm[2];
    aP->mPos.mY=theMatrix.mData.mm[7]-theMatrix.mData.mm[6];
    aP->mPos.mZ=theMatrix.mData.mm[11]-theMatrix.mData.mm[10];
    aP->mD=theMatrix.mData.mm[15]-theMatrix.mData.mm[14];
    aCenter+=aP->mPos;

    aP=&mPlane[FRUSTRUM_NEAR];
    aP->mPos.mX=theMatrix.mData.mm[3]+theMatrix.mData.mm[2];
    aP->mPos.mY=theMatrix.mData.mm[7]+theMatrix.mData.mm[6];
    aP->mPos.mZ=theMatrix.mData.mm[11]+theMatrix.mData.mm[10];
    aP->mD=theMatrix.mData.mm[15]+theMatrix.mData.mm[14];
    aCenter+=aP->mPos;
    aCenter/=6;

    for (int aCount=0;aCount<6;aCount++) {mPlane[aCount].mNormal=Normalize(aCenter-mPlane[aCount].mPos);}
    for (int aCount=0;aCount<6;aCount++) mPlane[aCount].NormalizePlus(theMult);
}

0

There are 0 answers