ShiVa3D
[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
by 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.
If you needed this, I am happy to have provided it, it is a real deal breaker for some projects ;)
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.
Join the Shiva IRC Chat! irc.shivaplace.com:6669 or irc.shivaplace.com:16669
Or, use the Java chat client: http://www.shivaplace.com/irc
Or, use the Java chat client: http://www.shivaplace.com/irc
Re:[SNIPPET] Using a Touch to select an object
by 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 :))
(just saw both your question and your solution :))
-

NiCoX - Platinum Boarder

- Posts: 5626
- Location: France
Re:[SNIPPET] Using a Touch to select an object
by 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.
-

clockmaster - Platinum Boarder

- Posts: 481
- Location: Germany
Re:[SNIPPET] Using a Touch to select an object
by Piedcarre » 02 Apr 2010, 17:45
Great! Thank you it will probably be very useful for many members :)
-

Piedcarre - Platinum Boarder

- Posts: 426
Re:[SNIPPET] Using a Touch to select an object
by dpharaoh » 02 Apr 2010, 17:55
My pleasure! One less question for you guys to handle so you can focus on other juicy stuff ;)
Join the Shiva IRC Chat! irc.shivaplace.com:6669 or irc.shivaplace.com:16669
Or, use the Java chat client: http://www.shivaplace.com/irc
Or, use the Java chat client: http://www.shivaplace.com/irc
Re: [SNIPPET] Using a Touch to select an object
by psk » 08 Feb 2011, 18:48
Id vote this a win if i had a button 
- psk
- Fresh Boarder

- Posts: 4
Re: [SNIPPET] Using a Touch to select an object
by 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
-

DoctorPepper - Platinum Boarder

- Posts: 420
- Location: Belgium
Re: [SNIPPET] Using a Touch to select an object
by 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" 
I guess I am a dumb, ...However, some "more" pictures in the Shiva Documentation (Website) could help, in some future, dumbs like me
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
!
I guess I am a dumb, ...However, some "more" pictures in the Shiva Documentation (Website) could help, in some future, dumbs like me
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

- Posts: 164
Re: [SNIPPET] Using a Touch to select an object
by fzuleta » 05 Nov 2011, 05:48
wow seriously thanks for this 
-

fzuleta - Senior Boarder

- Posts: 79
Re: [SNIPPET] Using a Touch to select an object
by Selzier » 13 Jul 2012, 16:10
Thanks dpharaoh! It's still being put to use!
- Selzier
- Platinum Boarder

- Posts: 355
- Location: Boise
Re: [SNIPPET] Using a Touch to select an object
by oo7 » 21 Jul 2012, 21:19
How to get the nX0 and nY0 for onTouchSequenceBegin and onTouchSequenceEnd events?
- oo7
- Fresh Boarder

- Posts: 7
Re: [SNIPPET] Using a Touch to select an object
by dpharaoh » 23 Jul 2012, 08:02
They are passed in to both those functions by Shiva and are automatically available.
Join the Shiva IRC Chat! irc.shivaplace.com:6669 or irc.shivaplace.com:16669
Or, use the Java chat client: http://www.shivaplace.com/irc
Or, use the Java chat client: http://www.shivaplace.com/irc
Re: [SNIPPET] Using a Touch to select an object
by 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
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

- Posts: 55
Re: [SNIPPET] Using a Touch to select an object
by 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.
Join the Shiva IRC Chat! irc.shivaplace.com:6669 or irc.shivaplace.com:16669
Or, use the Java chat client: http://www.shivaplace.com/irc
Or, use the Java chat client: http://www.shivaplace.com/irc
Re: [SNIPPET] Using a Touch to select an object
by 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

- Posts: 355
- Location: Boise
16 posts
• Page 1 of 2 • 1, 2