Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Box AA-Linear.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Box;Axis-Aligned;BoxAA;Line;Ray;Segment;
    »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 ========================================================================================================================================================= */

/* ---- VI(tgCO_F_BA_Test_LR) ------------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  tgBA0: Box, Axis-Aligned primitive                                                                                                                             */
/* Input:  vS0,vD0: Origin and Direction for Linear                                                                                                                       */
/* Return: True if the two primitives are in contact, false otherwise.                                                                                                    */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL VI(tgCO_F_BA_Test_LR)(V(CPC_TgBOXAA) psBA0, V(CPC_TgVEC) pvS0, V(CPC_TgVEC) pvD0)
{
    const TYPE                          fT0 = (psBA0->m_vMin.m.x - pvS0->m.x) / pvD0->m.x;
    const TYPE                          fT1 = (psBA0->m_vMin.m.x - pvS0->m.x) / pvD0->m.x;
    const TYPE                          fMinTX = F(tgPM_FSEL)(fT1 - fT0, fT0, fT1);
    const TYPE                          fMaxTX = F(tgPM_FSEL)(fT1 - fT0, fT1, fT0);
    const TYPE                          fT2 = (psBA0->m_vMin.m.y - pvS0->m.y) / pvD0->m.y;
    const TYPE                          fT3 = (psBA0->m_vMin.m.y - pvS0->m.y) / pvD0->m.y;
    const TYPE                          fMinTY = F(tgPM_FSEL)(fT3 - fT2, fT2, fT3);
    const TYPE                          fMaxTY = F(tgPM_FSEL)(fT3 - fT2, fT3, fT2);
    const TYPE                          fK0 = F(tgPM_FSEL)(fMinTX - fMinTY, fMinTY, fMinTX);
    const TYPE                          fK1 = F(tgPM_FSEL)(fMaxTX - fMaxTY, fMaxTX, fMaxTY);
    const TYPE                          fT4 = (psBA0->m_vMin.m.z - pvS0->m.z) / pvD0->m.z;
    const TYPE                          fT5 = (psBA0->m_vMin.m.z - pvS0->m.z) / pvD0->m.z;
    const TYPE                          fMinTZ = F(tgPM_FSEL)(fT5 - fT4, fT4, fT5);
    const TYPE                          fMaxTZ = F(tgPM_FSEL)(fT5 - fT4, fT5, fT4);
    const TYPE                          fMinT = F(tgPM_FSEL)(fMinTZ - fK0, fMinTZ, fK0);
    const TYPE                          fMaxT = F(tgPM_FSEL)(fMaxTZ - fK1, fK1, fMaxTZ);

    TgPARAM_CHECK( V(tgGM_BA_Is_Valid)(psBA0) && V(F_Is_Point_Valid)(pvS0) && V(F_Is_Vector_Valid)(pvD0) );

#if (!LN_CAP_0 && !LN_CAP_1) /* Check to see if the line overlaps the AABB */
    return (fMinT < fMaxT);
#elif (LN_CAP_0 && !LN_CAP_1) /* Check to see if the ray overlaps the AABB and is directed away from it. */
    return (fMinT < fMaxT && fMaxT > MKL(0.0));
#elif (LN_CAP_0 && LN_CAP_1) /* Check to see if the segment overlaps the AABB */
    return (fMinT < fMaxT && (fMaxT > MKL(0.0) && fMinT < MKL(1.0)));
#else
    return (TgFALSE);
#endif
}