Touch selection – ShiVa Engine

Touch selection

Do you need to know how to use a touch instead of a mouse event to cast a ray into a scene to see which object is there? Then this snippet is for you.
The touch events do not include ray info, but it’s easily calculated in really just a few lines of script.
In the example below, nX0 and nY0 are the touch events passed in through the touch API. They range from -1 to 1.

--[[nX0 and nY0 are the touch events passed in through the touch API, to onTouchesChanged for example. They range from -1 to 1]]--
    local cam = application.getCurrentUserActiveCamera ( )
    local s = application.getCurrentUserScene ( )
    local camX, camY, camZ  = object.getTranslation ( cam, object.kGlobalSpace )
    local nRayPntX, nRayPntY, nRayPntZ = camera.unprojectPoint ( cam, nX0, nY0, 0 )
    local vX,vY,vZ = math.vectorSubtract ( nRayPntX, nRayPntY, nRayPntZ, camX, camY, camZ )
    local nRayDirX,nRayDirY,nRayDirZ = math.vectorNormalize ( vX, vY, vZ )
    if  ( s )
        local hHitObject, nHitDist, nHitSID = scene.getFirstHitSensor( s, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, 2000)
        if  ( hHitObject )
            log.message ( "Hit Sensor ID :" .. nHitSID )

Need more answers?

  • slackBanner