Jump to content
  • Advertisement
Sign in to follow this  

LibGDX Multi-touch finger index tracking

This topic is 887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



I am developing a new game, and have a need to observe, if a player leaves bounds of a square with his finger.

Thing is, this is multi touch application and there are many squares player can interact with (with many fingers).


Now, here comes the question!
Is finger tracking the same on most devices?


I am using LibGDX and tried out this on Samsung S4 (or something like that) and this is possible.

If i first place finger A, it gets index of 0, then i place finger B, it gets index of 1. If i remove A, B is still 1.



I remember doing same tests on HTC One S, and if i do the same sequence, B finger changes its index to 0.


I can't test it out now on HTC One S, because that phone was faulty and in the end i had to return back to seller. BUT, it still a question i need to find an answer for.


I checked what LibGDX has to say about it:



Note that touch can be implemented quite differently on different devices. This can affect how pointer indexes are specified and released and when touch events are fired. Be sure to test your control scheme on as many devices as possible. There are also many input test apps available in the market which can help determine how a particular device reports touch and aid in designing a control scheme that works best across a range of devices.


From my English understanding, it seems to me like a valid issue, and trusting on finger indexes is bad idea.


Has anyone been is same kind of situation?

Share this post

Link to post
Share on other sites

It looks like LibGDX does some slight behind the scenes and calls both IDs and indexes 'pointers', so you'll need to double-check with the code to verify the details of which one you've got.
Here is what happens in the raw, underneath LibGDX.
On Android devices there are pointers that generate MotionEvent data.  Each pointer can be a mouse, a finger, multiple fingers, etc. It is a potentially confusing name with some programming languages using the word pointer to mean the address of an object.  Here a pointer refers to something generating input data at a position.
Pointers have both a pointer index and a pointer ID.  Both are integers. 
Every time there is a new touch or pointer a new data structure is created. That data structure contains a unique ID.  Those data structures are placed inside an array of MotionEvent data, and the position inside the array is the index.  Between events and behind your back the system can move items around inside the array, which changes their index.  That is why index values may change between calls and should not be kept around between calls. ID values should remain the same between calls.  
The system provides convenience methods to switch between the ID and index.  getPointerId(int) takes an index and returns the ID of whatever value is in that array position.  getPointerIndex(int) takes a pointer ID and returns the index in the array, or -1 if that ID is not in the array.
Do not rely on index values, they are just array locations that are meaningless between calls.  If you need to track over time use the ID, which should remain the same between calls.

Share this post

Link to post
Share on other sites

I am grateful for your lengthy response.


I will try to look into in a more detailed way later on.

By doing some quick checks, i noticed that methods like


are not available from LibGDX (or at least i did not manage to find).

On the other hand, LibGDX is a crossplatform tool, so it makes sense that native android events and methods can not be accessed.


For now i will trust, that events like


public boolean touchDown(int screenX, int screenY, int pointer, int button)  { return false; }
public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; }
Operate with PointerID and not Index.
Thank you very much! I will keep this thread bookmarked as a reference to this issue!

Share this post

Link to post
Share on other sites

That's the joy of having source code available. You get to go source diving when you aren't sure of the abstract behavior.


When in doubt, go to the source code.

I don't use it so I'm not sure what LibGDX uses to make it cross platform.  You can ask around and guess, but the easiest way to know for certain is to actually review the source code to see how they relate to what Android is doing.  Review the code and figure out if they are working with the index values that are invalid immediately, or the ID values that remain valid for the duration of the touch event.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!