Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Tube-Sphere.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Tube;Sphere;
    »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_Sweep_TB_SP) ------------------------------------------------------------------------------------------------------------------------------------------- */
/* 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:  psSP0: Sphere primitive                                                                                                                                        */
/* Input:  psTB0: Tube primitive                                                                                                                                          */
/* Input:  vDT: The swept primitive displacement for the duration of the period of interest.                                                                              */
/* 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                                                                                                                                                    */
/*                                                                                                                                                                        */
/*  This is a highly inaccurate test, meant to be used mainly as a child function for the cylinder, and capsule routines.                                                 */
/* Problems will rest in its false negative responses at values beyond the tube's length.                                                                                 */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT V(tgCO_F_TB_Sweep_SP)( V(PC_STg2_CO_Packet) psPacket, TYPE *pfPM, V(CPC_TgTUBE) psTB0, V(CPC_TgSPHERE) psSP0, V(CPC_TgDELTA) psDT )
{
    TgERROR( V(tgGM_SP_Is_Valid)(psSP0) && V(tgGM_TB_Is_Valid)(psTB0) );

    TgERROR(!psPacket->m_bReport_Penetration); /* Penetration not implemented for tube-sphere. */

    if (0 == psPacket->m_niMaxContact || psPacket->m_niContact >= psPacket->m_niMaxContact || nullptr == psPacket->m_psContact)
    {
        return (KTgE_FAIL);
    }
    else
    {
        const TYPE                          fRS = psTB0->m_fRadius + psSP0->m_fRadius;
        const TYPE                          fR0 = psSP0->m_fRadius;
        V(STg2_CO_Packet)                   sPacketSG;
        V(STg2_CO_Contact)                  asContactSG[2];
        TgRESULT                            iResult;
        V(P_STg2_CO_Contact)                psContactSG;
        V(P_STg2_CO_Contact)                psContact;

        if (!((fRS > F(KTgEPS)) && (psTB0->m_fExtent > F(KTgEPS))))
        {
            return (KTgE_FAIL);
        };

        sPacketSG.m_psContact = asContactSG;
        sPacketSG.m_fSweepTol = F(KTgEPS);
        sPacketSG.m_niContact = 0;
        sPacketSG.m_niMaxContact = 2;

        iResult = V(tgCO_FI_TB11_Intersect_LR11)(&sPacketSG, fR0, psTB0, &psSP0->m_vOrigin, &psDT->m_vDT);

        if (TgFAILED( iResult ))
        {
            return (iResult);
        };

        TgERROR( sPacketSG.m_niContact >= 1 );

        psContactSG = asContactSG[0].m_fT0 <= asContactSG[(1 % sPacketSG.m_niContact)].m_fT0 ? asContactSG : asContactSG + 1;

        if (psContactSG->m_fT0 > *pfPM + psPacket->m_fSweepTol)
        {
            return (KTgE_NO_INTERSECT);
        }
        else
        {
            V(C_TgVEC)                          vK0 = V(F_MUL_SV)(psSP0->m_fRadius, &psContactSG->m_vN0);

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

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

            psContact->m_vS0 = V(F_SUB)(&psContactSG->m_vS0, &vK0);
            psContact->m_vN0 = psContactSG->m_vN0;
            psContact->m_fT0 = psContactSG->m_fT0;
            psContact->m_fDepth = MKL(0.0);

            ++psPacket->m_niContact;

            return (KTgS_OK);
        };
    };
}


/* ---- V(tgCO_F_Sweep_SP_TB) ------------------------------------------------------------------------------------------------------------------------------------------- */
/* 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:  psSP0: Sphere primitive                                                                                                                                        */
/* Input:  psTB0: Tube primitive                                                                                                                                          */
/* Input:  psDT: A structure holding the swept primitive displacement for the entire duration of the test period                                                          */
/* Output: tgPacket: Points of contact between the two primitives are added to it                                                                                         */
/* Output: fPM: New normalized time of first contact                                                                                                                      */
/* Return: Result Code                                                                                                                                                    */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT V(tgCO_F_SP_Sweep_TB)( V(PC_STg2_CO_Packet) psPacket, TYPE *pfPM, V(CPC_TgSPHERE) psSP0, V(CPC_TgTUBE) psTB0, V(CPC_TgDELTA) psDT )
{
    const TgSINT32                      niContact = psPacket->m_niContact;
    const TYPE                          fT = *pfPM;
    V(TgDELTA)                          sNegDT;
    TgRESULT                            iResult;
    V(P_STg2_CO_Contact)                psContact;
    TgSINT32                            iIdx;

    sNegDT.m_vUDT = V(F_NEG)(&psDT->m_vUDT);
    sNegDT.m_vDT = V(F_NEG)(&psDT->m_vDT);
    sNegDT.m_fDT = psDT->m_fDT;;
    sNegDT.m_fDT_DT = psDT->m_fDT_DT;
    sNegDT.m_fInv_DT = psDT->m_fInv_DT;

    iResult = V(tgCO_F_TB_Sweep_SP)(psPacket, pfPM, psTB0, psSP0, &sNegDT);

    if (iResult == KTgE_PREPENETRATION)
    {
        for (iIdx = niContact; iIdx < psPacket->m_niContact; ++iIdx)
        {
            psContact = psPacket->m_psContact + iIdx;

            {
                V(C_TgVEC)                          vK0 = V(F_MUL_VS)(&psContact->m_vN0, psContact->m_fDepth);

                psContact->m_vS0 = V(F_ADD)(&psContact->m_vS0, &vK0);
                psContact->m_vN0 = V(F_NEG)(&psContact->m_vN0);
            };
        };

        return (iResult);
    }

    if (TgFAILED( iResult ))
    {
        return (iResult);
    };

    iIdx = (*pfPM < fT - psPacket->m_fSweepTol ? 0 : niContact);

    TgERROR( iIdx < psPacket->m_niContact );

    for (; iIdx < psPacket->m_niContact; ++iIdx)
    {
        psContact = psPacket->m_psContact + iIdx;

        {
            V(C_TgVEC)                          vK0 = V(F_MUL_VS)(&psDT->m_vDT, psContact->m_fT0);

            psContact->m_vS0 = V(F_ADD)(&psContact->m_vS0, &vK0);
            psContact->m_vN0 = V(F_NEG)(&psContact->m_vN0);
        };
    };

    return (iResult);
}