Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Box-Point.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Box;Point;
    »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_BX_ParamSq_VT) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  psBX0: Box primitive                                                                                                                                           */
/* Input:  vS0: Point                                                                                                                                                     */
/* Output: tyB0,tyB1,tyB2: Parametric parameters to generate the point of closest proximity on the box (one for each axis)                                                */
/* Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_F_BX_ParamSq_VT)(TYPE *pfB0, TYPE *pfB1, TYPE *pfB2, V(CPC_TgBOX) psBX0, V(CPC_TgVEC) pvS0)
{
    V(C_TgVEC)                          vDS = V(F_SUB)(pvS0, &psBX0->m.m.vOrigin);
    TYPE                                fDistSq = MKL(0.0);
    const TYPE                          fT0 = V(F_DOT)(&vDS, psBX0->m.m.avAxis + 0);
    const TYPE                          fT1 = V(F_DOT)(&vDS, psBX0->m.m.avAxis + 1);
    const TYPE                          fT2 = V(F_DOT)(&vDS, psBX0->m.m.avAxis + 2);

    TgPARAM_CHECK( V(tgGM_BX_Is_Valid)(psBX0) && V(F_Is_Point_Valid)(pvS0) );

    if (fT0 < -psBX0->m_vExtent.m.x)
    {
        const TYPE                          fDist = fT0 + psBX0->m_vExtent.m.x;
        fDistSq += fDist*fDist;
        *pfB0 = -psBX0->m_vExtent.m.x;
    }
    else if (fT0 > psBX0->m_vExtent.m.x)
    {
        const TYPE                          fDist = fT0 - psBX0->m_vExtent.m.x;
        fDistSq += fDist*fDist;
        *pfB0 = psBX0->m_vExtent.m.x;
    }
    else
    {
        *pfB0 = fT0;
    };

    if (fT1 < -psBX0->m_vExtent.m.y)
    {
        const TYPE                          fDist = fT1 + psBX0->m_vExtent.m.y;
        fDistSq += fDist*fDist;
        *pfB1 = -psBX0->m_vExtent.m.y;
    }
    else if (fT1 > psBX0->m_vExtent.m.y)
    {
        const TYPE                          fDist = fT1 - psBX0->m_vExtent.m.y;
        fDistSq += fDist*fDist;
        *pfB1 = psBX0->m_vExtent.m.y;
    }
    else
    {
        *pfB1 = fT1;
    };

    if (fT2 < -psBX0->m_vExtent.m.z)
    {
        const TYPE                          fDist = fT2 + psBX0->m_vExtent.m.z;
        fDistSq += fDist*fDist;
        *pfB2 = -psBX0->m_vExtent.m.z;
    }
    else if (fT2 > psBX0->m_vExtent.m.z)
    {
        const TYPE                          fDist = fT2 - psBX0->m_vExtent.m.z;
        fDistSq += fDist*fDist;
        *pfB2 = psBX0->m_vExtent.m.z;
    }
    else
    {
        *pfB2 = fT2;
    };

    return (fDistSq <= MKL(0.0) ? -F(KTgMAX) : fDistSq);
}