Collision detection – ShiVa Engine

Collision detection

sKey1 and sKey2 are the string key identifiers for two HUD components. This function will return true if there is a collision between the 2 components and false if there is no collision.

--------------------------------------------------------------------------------
function ai_main.checkCollision ( sKey1, sKey2 )
--------------------------------------------------------------------------------
    local hUser = application.getCurrentUser ( )
    -- Get the tags and set a handler
    local hComp1 = hud.getComponent ( hUser, sKey1 )
    local hComp2 = hud.getComponent ( hUser, sKey2 )
    -- Make sure the handlers aren't null
    if ( hComp1 ) and ( hComp2 )
    then
        -- Component 1
        local nWidth1, nHeight1 = hud.getComponentSize ( hComp1 )
        local nPosX1, nPosY1 = hud.getComponentPosition ( hComp1 )
        local nRangeStartX1 = nPosX1 - ( nWidth1 / 2 )
        local nRangeEndX1 = nPosX1 + ( nWidth1 / 2 )
        local nRangeStartY1 = nPosY1 - ( nHeight1 / 2 )
        local nRangeEndY1 = nPosY1 + ( nHeight1 / 2 )
        -- Component 2
        local nWidth2, nHeight2 = hud.getComponentSize ( hComp2 )
        local nPosX2, nPosY2 = hud.getComponentPosition ( hComp2 )
        local nRangeStartX2 = nPosX2 - ( nWidth2 / 2 )
        local nRangeEndX2 = nPosX2 + ( nWidth2 / 2 )
        local nRangeStartY2 = nPosY2 - ( nHeight2 / 2 )
        local nRangeEndY2 = nPosY2 + ( nHeight2 / 2 )
        -- Collision check
        if ( ( nRangeStartX2 >= nRangeStartX1 and nRangeStartX2 = nRangeStartX1 and nRangeEndX2 = nRangeStartY1 and nRangeStartY2 = nRangeStartY1 and nRangeEndY2



Need more answers?

  • slackBanner