{"id":1030,"date":"2011-01-05T12:32:04","date_gmt":"2011-01-05T03:32:04","guid":{"rendered":"http:\/\/www.codedojo.com\/?p=1030"},"modified":"2026-09-08T04:09:09","modified_gmt":"2026-09-08T04:09:09","slug":"how-to-get-rock-solid-multi-touch-input-finger-tracking-on-ios","status":"publish","type":"post","link":"https:\/\/www.codedojo.com\/?p=1030","title":{"rendered":"How to get rock solid multi-touch input finger tracking on iOS"},"content":{"rendered":"<p>So I&#8217;ve got this game prototype idea I wanted to try but I needed better multi-touch input support (accurate and comprehensive fingerID tracking) to make it happen.<\/p>\n<div id=\"attachment_1038\" style=\"width: 610px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.codedojo.com\/wp-content\/uploads\/2011\/01\/multitouch.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1038\" class=\"size-full wp-image-1038\" title=\"multitouch\" src=\"https:\/\/www.codedojo.com\/wp-content\/uploads\/2011\/01\/multitouch.jpg\" alt=\"\" width=\"600\" height=\"373\" srcset=\"https:\/\/www.codedojo.com\/wp-content\/uploads\/2011\/01\/multitouch.jpg 600w, https:\/\/www.codedojo.com\/wp-content\/uploads\/2011\/01\/multitouch-300x186.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-1038\" class=\"wp-caption-text\">The new test in RTSimpleApp: You can&#39;t see it in the shot, but each square has a fingerID # on it as well<\/p><\/div>\n<p>It works great and has been added to the <a href=\"http:\/\/www.protonsdk.com\">Proton SDK<\/a> svn along with a new &#8220;Multitouch input test&#8221; option in RTSimpleApp.<\/p>\n<p>Some stuff I noticed:<\/p>\n<ul>\n<li>Tracks 11 fingers on an iPad<\/li>\n<li>Tracks 4 fingers on an iPod Touch G1 (5 sort of.. but not reliably..)<\/li>\n<li>Tracks 5 fingers on an iPhone4<\/li>\n<li>Impossible to confuse it with fast movement, sliding off the screen or fast button mashing.\u00a0 Solid!<\/li>\n<li>Tracks 1.5 fingers on a Google Nexus One (It&#8217;s the <a href=\"http:\/\/androidandme.com\/2010\/03\/news\/is-multitouch-broken-on-the-nexus-one\/\">HW&#8217;s fault<\/a>.\u00a0 Curious to see the results from a Tab or other new devices though)<\/li>\n<\/ul>\n<p>For those interested, here is the relevant tracking code to get the &#8220;finger id&#8221;:\u00a0 (yeah, I could have done it a more Obj-C way, but meh.. also, you may need your file named .mm, not .m for this code to work&#8230;)<\/p>\n<pre><code class=\"language-php\">const int MAX_TOUCHES= 11; \/\/Oops, it can handle 11, not 10.  Thanks @Bob_at_BH\r\n\r\nclass TouchTrack\r\n{\r\npublic:\r\n\r\n\tTouchTrack()\r\n\t{\r\n\t\tm_touchPointer = NULL;\r\n\t}\r\n\r\n\tvoid *m_touchPointer;\r\n};\r\n\r\nTouchTrack g_touchTracker[MAX_TOUCHES];\r\n\r\nint GetFingerTrackIDByTouch(void* touch)\r\n{\r\n\t\tfor (int i=0; i &lt; MAX_TOUCHES; i++)\r\n\t\t{\r\n\t\t\tif (g_touchTracker[i].m_touchPointer == touch)\r\n\t\t\t{\r\n\t\t\t\treturn i;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\/\/LogMsg(&quot;Can't locate fingerID by touch %d&quot;, touch);\r\n\treturn -1;\r\n}\r\n\r\nint AddNewTouch(void* touch)\r\n{\r\n\t\tfor (int i=0; i &lt; MAX_TOUCHES; i++)\r\n\t\t{\r\n\t\t\tif (!g_touchTracker[i].m_touchPointer)\r\n\t\t\t{\r\n\t\t\t\t\/\/hey, an empty slot, yay\r\n\t\t\t\tg_touchTracker[i].m_touchPointer = touch;\r\n\t\t\t\treturn i;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\tLogMsg(&quot;Can't add new fingerID&quot;);\r\n\treturn -1;\r\n}\r\n\r\nint GetTouchesActive()\r\n{\r\nint count = 0;\r\n\r\n\tfor (int i=0; i &lt; MAX_TOUCHES; i++)\r\n\t\t{\r\n\t\t\tif (g_touchTracker[i].m_touchPointer)\r\n\t\t\t{\r\n\t\t\t\tcount++;\r\n\t\t\t}\r\n\t\t}\r\nreturn count;\r\n}\r\n\r\n\/\/ Handles the start of a touch\r\n- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event\r\n{\r\n  \t\/\/ Enumerate through all the touch objects.\r\n\r\n\tfor (UITouch *touch in touches)\r\n\t{\r\n\t\t\/\/found a touch.  Is it already on our list?\r\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);\r\n\r\n\t\tif (fingerID == -1)\r\n\t\t{\r\n\t\t\t\/\/add it to our list\r\n\t\t\tfingerID = AddNewTouch(touch);\r\n\t\t} else\r\n\t\t{\r\n\t\t\t\/\/already on the list.  Don't send this\r\n\t\t\t\/\/LogMsg(&quot;Ignoring touch %d&quot;, fingerID);\r\n\t\t\tcontinue;\r\n\t\t}\r\n\r\n\t\tCGPoint pt =[touch locationInView:self];\r\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);\r\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_START,pt.x, pt.y,fingerID);\t\t\t\r\n\t}\t\r\n\r\n\t  \r\n\t#ifdef _DEBUG\r\n\t\/\/LogMsg(&quot;%d touches active&quot;, GetTouchesActive());\r\n\t#endif\r\n}\r\n\r\n- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event\r\n{\r\n  \t\/\/ Enumerate through all the touch objects.\r\n\tfor (UITouch *touch in touches)\r\n\t{\r\n\t\t\/\/found a touch.  Is it already on our list?\r\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);\r\n\t\tif (fingerID != -1)\r\n\t\t{\r\n\t\t\tg_touchTracker[fingerID].m_touchPointer = NULL; \/\/clear it\r\n\t\t} else\r\n\t\t{\r\n\t\t\t\/\/wasn't on our list\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\t\r\n\t\tCGPoint pt =[touch locationInView:self];\r\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);\r\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_END,pt.x, pt.y, fingerID);\r\n\t}\t\r\n}\r\n\r\n- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event\r\n{\r\n  \t\/\/ Enumerate through all the touch objects.\r\n\tfor (UITouch *touch in touches)\r\n\t{\r\n\t\t\/\/found a touch.  Is it already on our list?\r\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);\r\n\t\tif (fingerID != -1)\r\n\t\t{\r\n\t\t\tg_touchTracker[fingerID].m_touchPointer = NULL; \/\/clear it\r\n\t\t} else\r\n\t\t{\r\n\t\t\t\/\/wasn't on our list\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\t\r\n\t\tCGPoint pt =[touch locationInView:self];\r\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);\r\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_END,pt.x, pt.y, fingerID);\r\n\t}\t\r\n}\r\n\r\n\r\n- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event\r\n{\r\n   \/\/ Enumerate through all the touch objects.\r\n\tfor (UITouch *touch in touches)\r\n\t{\r\n\t\r\n\t\t\/\/found a touch.  Is it already on our list?\r\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);\r\n\t\tif (fingerID != -1)\r\n\t\t{\r\n\t\t\t\/\/found it\r\n\t\t} else\r\n\t\t{\r\n\t\t\t\/\/wasn't on our list?!\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\r\n\t\tCGPoint pt =[touch locationInView:self];\r\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);\r\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_MOVE,pt.x, pt.y, fingerID);\r\n\t}\t\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;ve got this game prototype idea I wanted to try but I needed better multi-touch input support (accurate and comprehensive fingerID tracking) to make it happen. It works great and has been added to the Proton SDK svn along with a new &#8220;Multitouch input test&#8221; option in RTSimpleApp. Some stuff I noticed: Tracks 11 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,16,6],"tags":[],"class_list":["post-1030","post","type-post","status-publish","format-standard","hentry","category-development","category-proton","category-tech-tips"],"_links":{"self":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/1030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1030"}],"version-history":[{"count":20,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/1030\/revisions"}],"predecessor-version":[{"id":1533,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=\/wp\/v2\/posts\/1030\/revisions\/1533"}],"wp:attachment":[{"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedojo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}