Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Box AA-Plane.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Box;Axis-Aligned;BoxAA;Plane;
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Collision ========================================================================================================================================================= */

/* ---- V(tgCO_F_PN_Penetrate_BA) --------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  tgPacket: The current series of contact points for this query-series, and contact generation parameters.                                                       */
/* Input:  psPN0: Plane Triangle primitive                                                                                                                                */
/* Input:  tgBA0: Box, Axis-Aligned primitive - contact points are generated on this primitive                                                                            */
/* Output: tgPacket: Points of penetration between the two primitives are added to it                                                                                     */
/* Return: Result Code                                                                                                                                                    */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT V(tgCO_F_PN_Penetrate_BA)( V(PC_STg2_CO_Packet) psPacket, V(CPC_TgPLANE) psPN0, V(CPC_TgBOXAA) psBA0 )
{
    V(P_STg2_CO_Contact)                psContact;
    V(TgVEC)                            vBA0;
    TYPE                                fDist;

    TgPARAM_CHECK( V(tgGM_PN_Is_Valid)(psPN0) && V(tgGM_BA_Is_Valid)(psBA0) );

    if (0 == psPacket->m_niMaxContact || psPacket->m_niContact >= psPacket->m_niMaxContact || nullptr == psPacket->m_psContact)
    {
        return (KTgE_FAIL);
    };

    vBA0 = V(FS_SETP)(
        F(tgPM_FSEL)(psPN0->m_vNormal.m.x, psBA0->m_vMin.m.x, psBA0->m_vMax.m.x),
        F(tgPM_FSEL)(psPN0->m_vNormal.m.y, psBA0->m_vMin.m.y, psBA0->m_vMax.m.y),
        F(tgPM_FSEL)(psPN0->m_vNormal.m.z, psBA0->m_vMin.m.z, psBA0->m_vMax.m.z)
    );

    fDist = V(tgCO_F_PN_Sign_Dist_VT)(psPN0, &vBA0);

    if (fDist >= MKL(0.0))
    {
        return (KTgE_NO_INTERSECT);
    };

    psContact = psPacket->m_psContact + psPacket->m_niContact;

    psContact->m_vS0 = vBA0;
    psContact->m_vN0 = psPN0->m_vNormal;
    psContact->m_fT0 = MKL(0.0);
    psContact->m_fDepth = -fDist;

    ++psPacket->m_niContact;

    return (KTgS_OK);
}


/* ---- V(tgCO_F_PN_Sweep_BA) ------------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  tgPacket: The current series of contact points for this query-series, and contact generation parameters.                                                       */
/* Input:  fPM: Current normalized time of first contact.                                                                                                                 */
/* Input:  bPenetrate: If the swept primitives are in penetration, if true the function will return points of penetration.                                                */
/* Input:  psPN0: Plane primitive                                                                                                                                         */
/* Input:  tgBA0: Box, Axis-Aligned Primitive                                                                                                                             */
/* Input:  psDT: A structure holding the swept primitive displacement for the entire duration of the test period                                                          */
/* Output: tgPacket: Contact points are added or replace the current set depending on the time comparison and given parameters                                            */
/* Output: fPM: New normalized time of first contact                                                                                                                      */
/* Return: Result Code                                                                                                                                                    */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT V(tgCO_F_PN_Sweep_BA)( V(PC_STg2_CO_Packet) psPacket, TYPE *pfPM, V(CPC_TgPLANE) psPN0, V(CPC_TgBOXAA) psBA0, V(CPC_TgDELTA) psDT )
{
    const TYPE                          fDT_N = V(F_DOT)(&psDT->m_vDT, &psPN0->m_vNormal);
    V(TgVEC)                            vBA0;
    TYPE                                fDist;
    V(P_STg2_CO_Contact)                psContact;

    TgPARAM_CHECK( V(tgGM_PN_Is_Valid)(psPN0) && V(tgGM_BA_Is_Valid)(psBA0) );

    if (0 == psPacket->m_niMaxContact || psPacket->m_niContact >= psPacket->m_niMaxContact || nullptr == psPacket->m_psContact)
    {
        return (KTgE_FAIL);
    };

    vBA0 = V(FS_SETP)(
        F(tgPM_FSEL)(psPN0->m_vNormal.m.x, psBA0->m_vMin.m.x, psBA0->m_vMax.m.x),
        F(tgPM_FSEL)(psPN0->m_vNormal.m.y, psBA0->m_vMin.m.y, psBA0->m_vMax.m.y),
        F(tgPM_FSEL)(psPN0->m_vNormal.m.z, psBA0->m_vMin.m.z, psBA0->m_vMax.m.z)
    );

    fDist = V(tgCO_F_PN_Sign_Dist_VT)(psPN0, &vBA0);

    if (fDist < MKL(0.0))
    {
        if (*pfPM > psPacket->m_fSweepTol)
        {
            psPacket->m_niContact = 0;
        };

        *pfPM = MKL(0.0);

        if ((TgTRUE == psPacket->m_bReport_Penetration) && KTgE_MAX_CONTACTS == V(tgCO_F_PN_Penetrate_BA)(psPacket, psPN0, psBA0))
        {
            return (KTgE_MAX_CONTACTS);
        };

        return (KTgE_PREPENETRATION);
    };

    if (fDist >= MKL(0.0) && fDist + fDT_N >= MKL(0.0))
    {
        return (KTgE_NO_INTERSECT);
    }
    else
    {
        const TYPE                          fT0 = fDist / (-fDT_N);

        if (fT0 > *pfPM + psPacket->m_fSweepTol)
        {
            return (KTgE_NO_INTERSECT);
        };

        if (fT0 < *pfPM - psPacket->m_fSweepTol)
        {
            psPacket->m_niContact = 0;
            *pfPM = fT0;
        };

        psContact = psPacket->m_psContact + psPacket->m_niContact;

        psContact->m_vS0 = vBA0;
        psContact->m_vN0 = psPN0->m_vNormal;
        psContact->m_fT0 = fT0;
        psContact->m_fDepth = MKL(0.0);

        ++psPacket->m_niContact;

        return (KTgS_OK);
    };
}