Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Code Snippets

[SNIPPET] Using a Touch to select an object

Paste down any little snippets or request a new one.

[SNIPPET] Using a Touch to select an object

Postby dpharaoh » 02 Apr 2010, 16:47

This took me a few hours to get correct, so I thought I would post a solution. If you are reading this, it's probably because 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.

The touch events do not include ray info, but it's easily calculated by us 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.

Code: Select all
    --[[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 )
    then
        local hHitObject, nHitDist, nHitSID = scene.getFirstHitSensor( s, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, 2000)
        if  ( hHitObject )
        then
            log.message ( "Hit Sensor ID :" .. nHitSID )
        end
    end


If you needed this, I am happy to have provided it, it is a real deal breaker for some projects ;)
Last edited by dpharaoh on 01 Aug 2010, 19:31, edited 1 time in total.
dpharaoh
Platinum Boarder
Platinum Boarder
 
Posts: 1516
Location: New Bedford, MA

Re:[SNIPPET] Using a Touch to select an object

Postby NiCoX » 02 Apr 2010, 17:13

This is exactly what the engine does for onMouseXXX handlers :cheer:

(just saw both your question and your solution :))
User avatar
NiCoX
Platinum Boarder
Platinum Boarder
 
Posts: 5627
Location: France

Re:[SNIPPET] Using a Touch to select an object

Postby clockmaster » 02 Apr 2010, 17:41

Thank you very much for this Code Snippet. That was one of the next Tasks, I have to do for my Game. You are really Great.
User avatar
clockmaster
Platinum Boarder
Platinum Boarder
 
Posts: 486
Location: Germany

Re:[SNIPPET] Using a Touch to select an object

Postby Piedcarre » 02 Apr 2010, 17:45

Great! Thank you it will probably be very useful for many members :)
User avatar
Piedcarre
Platinum Boarder
Platinum Boarder
 
Posts: 433

Re:[SNIPPET] Using a Touch to select an object

Postby dpharaoh » 02 Apr 2010, 17:55

My pleasure! One less question for you guys to handle so you can focus on other juicy stuff ;)
dpharaoh
Platinum Boarder
Platinum Boarder
 
Posts: 1516
Location: New Bedford, MA

Re: [SNIPPET] Using a Touch to select an object

Postby psk » 08 Feb 2011, 18:48

Id vote this a win if i had a button :)
psk
Fresh Boarder
Fresh Boarder
 
Posts: 4

Re: [SNIPPET] Using a Touch to select an object

Postby DoctorPepper » 26 May 2011, 01:25

This is amazing, just what i was searching for! Still dont understand why its not in the engine by default
User avatar
DoctorPepper
Platinum Boarder
Platinum Boarder
 
Posts: 469
Location: Belgium

Re: [SNIPPET] Using a Touch to select an object

Postby FR_dZastrX » 11 Jul 2011, 13:25

Thank you, I was still a few hours from finding it because I tried to use "project" instead of "unproject" :lol:
I guess I am a dumb, ...However, some "more" pictures in the Shiva Documentation (Website) could help, in some future, dumbs like me :wink:

I am agree this may not be the more juicy thing to plan right now, but, well, that's the kind of tasks young students might be able to do, more or less for free, during some developer stage...? Well, as far as they are not as dumb as me :) !
FR_dZastrX
Gold Boarder
Gold Boarder
 
Posts: 164

Re: [SNIPPET] Using a Touch to select an object

Postby fzuleta » 05 Nov 2011, 05:48

wow seriously thanks for this :)
User avatar
fzuleta
Senior Boarder
Senior Boarder
 
Posts: 79

Re: [SNIPPET] Using a Touch to select an object

Postby Selzier » 13 Jul 2012, 16:10

Thanks dpharaoh! It's still being put to use!
Selzier
Platinum Boarder
Platinum Boarder
 
Posts: 494
Location: Boise

Re: [SNIPPET] Using a Touch to select an object

Postby oo7 » 21 Jul 2012, 21:19

How to get the nX0 and nY0 for onTouchSequenceBegin and onTouchSequenceEnd events?
oo7
Fresh Boarder
Fresh Boarder
 
Posts: 7

Re: [SNIPPET] Using a Touch to select an object

Postby dpharaoh » 23 Jul 2012, 08:02

They are passed in to both those functions by Shiva and are automatically available.
dpharaoh
Platinum Boarder
Platinum Boarder
 
Posts: 1516
Location: New Bedford, MA

Re: [SNIPPET] Using a Touch to select an object

Postby Lourdez » 04 Sep 2012, 22:42

Hi !

I've tried this code but I can't get any hit object. Is there anyone who has a sample working ?

Thanks
Lourdez
Senior Boarder
Senior Boarder
 
Posts: 55

Re: [SNIPPET] Using a Touch to select an object

Postby dpharaoh » 05 Sep 2012, 01:29

It is working off of sensors right now. I usually add collision to the mix, so it can pick either an object with sensors or an object with collision. It's really up to you and how you want to have your logic.
dpharaoh
Platinum Boarder
Platinum Boarder
 
Posts: 1516
Location: New Bedford, MA

Re: [SNIPPET] Using a Touch to select an object

Postby Selzier » 05 Sep 2012, 03:07

Here is my working example. I tried to comment the code as good as possible. This is in AI model AICamera which is attached to the player's camera.

Code: Select all
--------------------------------------------------------------------------------
function AICamera.onActivateShoot (  )
--------------------------------------------------------------------------------
   
    local hUser = application.getCurrentUser ( )
   
    -- Get a handler to the player camera
    local o = this.getObject ( )

    -- Get a handler to the scene
    local s = object.getScene ( o )

    if ( s ~= nil )
    then
        -- Get the camera's translation and direction so we know where to shoot a ray from and to
        local nRayPntX, nRayPntY, nRayPntZ = object.getTranslation ( o, object.kGlobalSpace )
        local nRayDirX, nRayDirY, nRayDirZ = object.getDirection ( o, object.kGlobalSpace )
       
        --Shoot a ray looking for a Sensor, with ray length 150 and sensor ID between 0 and 99
        local hHitSensorObject, nHitSensorDist, nHitSensorSurfaceID = scene.getFirstHitSensorWithIDInRange ( s, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ, 150, 0, 99 )
                                     
        -- If the ray hit a sensor and the distance is less than 2 meters
        -- I want this to only work for very close objects in my case!
        if ( hHitSensorObject and nHitSensorDist < 2 )
        then
           
            -- Get the string name of the model that was hit
            local sSensor = object.getModelName ( hHitSensorObject )
            -- Get the string name of the AI Model hit
            local sAIModel = object.getAIModelNameAt ( hHitSensorObject, 0 )
           
            log.message ( "hHitSensorObject   : " .. sSensor )
            log.message ( "nHitSensorDist     : " .. nHitSensorDist )
            log.message ( "sAI   at index 0   : " .. sAIModel )
           
            -- Send an onTouch Event Handler to the object that was hit
            -- All my objects that may be hit have an AI Model with a custom "onTouch" handler
            object.sendEvent ( hHitSensorObject, sAIModel, "onTouch" )
           
        -- Nothing was hit or it was farther than 2 meters.
        else
            log.message ( "Nothing was hit" )
        end
       
    end

--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
Selzier
Platinum Boarder
Platinum Boarder
 
Posts: 494
Location: Boise

Next

Return to Code Snippets