{"id":1030,"date":"2011-01-05T12:32:04","date_gmt":"2011-01-05T03:32:04","guid":{"rendered":"http:\/\/www.codedojo.com\/?p=1030"},"modified":"2011-01-05T14:12:45","modified_gmt":"2011-01-05T05:12:45","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<p>[php]<\/p>\n<p>const int MAX_TOUCHES= 11; \/\/Oops, it can handle 11, not 10.  Thanks @Bob_at_BH<\/p>\n<p>class TouchTrack<br \/>\n{<br \/>\npublic:<\/p>\n<p>\tTouchTrack()<br \/>\n\t{<br \/>\n\t\tm_touchPointer = NULL;<br \/>\n\t}<\/p>\n<p>\tvoid *m_touchPointer;<br \/>\n};<\/p>\n<p>TouchTrack g_touchTracker[MAX_TOUCHES];<\/p>\n<p>int GetFingerTrackIDByTouch(void* touch)<br \/>\n{<br \/>\n\t\tfor (int i=0; i &lt; MAX_TOUCHES; i++)<br \/>\n\t\t{<br \/>\n\t\t\tif (g_touchTracker[i].m_touchPointer == touch)<br \/>\n\t\t\t{<br \/>\n\t\t\t\treturn i;<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\/\/LogMsg(&quot;Can&#8217;t locate fingerID by touch %d&quot;, touch);<br \/>\n\treturn -1;<br \/>\n}<\/p>\n<p>int AddNewTouch(void* touch)<br \/>\n{<br \/>\n\t\tfor (int i=0; i &lt; MAX_TOUCHES; i++)<br \/>\n\t\t{<br \/>\n\t\t\tif (!g_touchTracker[i].m_touchPointer)<br \/>\n\t\t\t{<br \/>\n\t\t\t\t\/\/hey, an empty slot, yay<br \/>\n\t\t\t\tg_touchTracker[i].m_touchPointer = touch;<br \/>\n\t\t\t\treturn i;<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\tLogMsg(&quot;Can&#8217;t add new fingerID&quot;);<br \/>\n\treturn -1;<br \/>\n}<\/p>\n<p>int GetTouchesActive()<br \/>\n{<br \/>\nint count = 0;<\/p>\n<p>\tfor (int i=0; i &lt; MAX_TOUCHES; i++)<br \/>\n\t\t{<br \/>\n\t\t\tif (g_touchTracker[i].m_touchPointer)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tcount++;<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\nreturn count;<br \/>\n}<\/p>\n<p>\/\/ Handles the start of a touch<br \/>\n&#8211; (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event<br \/>\n{<br \/>\n  \t\/\/ Enumerate through all the touch objects.<\/p>\n<p>\tfor (UITouch *touch in touches)<br \/>\n\t{<br \/>\n\t\t\/\/found a touch.  Is it already on our list?<br \/>\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);<\/p>\n<p>\t\tif (fingerID == -1)<br \/>\n\t\t{<br \/>\n\t\t\t\/\/add it to our list<br \/>\n\t\t\tfingerID = AddNewTouch(touch);<br \/>\n\t\t} else<br \/>\n\t\t{<br \/>\n\t\t\t\/\/already on the list.  Don&#8217;t send this<br \/>\n\t\t\t\/\/LogMsg(&quot;Ignoring touch %d&quot;, fingerID);<br \/>\n\t\t\tcontinue;<br \/>\n\t\t}<\/p>\n<p>\t\tCGPoint pt =[touch locationInView:self];<br \/>\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);<br \/>\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_START,pt.x, pt.y,fingerID);<br \/>\n\t}\t<\/p>\n<p>\t#ifdef _DEBUG<br \/>\n\t\/\/LogMsg(&quot;%d touches active&quot;, GetTouchesActive());<br \/>\n\t#endif<br \/>\n}<\/p>\n<p>&#8211; (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event<br \/>\n{<br \/>\n  \t\/\/ Enumerate through all the touch objects.<br \/>\n\tfor (UITouch *touch in touches)<br \/>\n\t{<br \/>\n\t\t\/\/found a touch.  Is it already on our list?<br \/>\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);<br \/>\n\t\tif (fingerID != -1)<br \/>\n\t\t{<br \/>\n\t\t\tg_touchTracker[fingerID].m_touchPointer = NULL; \/\/clear it<br \/>\n\t\t} else<br \/>\n\t\t{<br \/>\n\t\t\t\/\/wasn&#8217;t on our list<br \/>\n\t\t\tcontinue;<br \/>\n\t\t}<\/p>\n<p>\t\tCGPoint pt =[touch locationInView:self];<br \/>\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);<br \/>\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_END,pt.x, pt.y, fingerID);<br \/>\n\t}<br \/>\n}<\/p>\n<p>&#8211; (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event<br \/>\n{<br \/>\n  \t\/\/ Enumerate through all the touch objects.<br \/>\n\tfor (UITouch *touch in touches)<br \/>\n\t{<br \/>\n\t\t\/\/found a touch.  Is it already on our list?<br \/>\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);<br \/>\n\t\tif (fingerID != -1)<br \/>\n\t\t{<br \/>\n\t\t\tg_touchTracker[fingerID].m_touchPointer = NULL; \/\/clear it<br \/>\n\t\t} else<br \/>\n\t\t{<br \/>\n\t\t\t\/\/wasn&#8217;t on our list<br \/>\n\t\t\tcontinue;<br \/>\n\t\t}<\/p>\n<p>\t\tCGPoint pt =[touch locationInView:self];<br \/>\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);<br \/>\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_END,pt.x, pt.y, fingerID);<br \/>\n\t}<br \/>\n}<\/p>\n<p>&#8211; (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event<br \/>\n{<br \/>\n   \/\/ Enumerate through all the touch objects.<br \/>\n\tfor (UITouch *touch in touches)<br \/>\n\t{<\/p>\n<p>\t\t\/\/found a touch.  Is it already on our list?<br \/>\n\t\tint fingerID = GetFingerTrackIDByTouch(touch);<br \/>\n\t\tif (fingerID != -1)<br \/>\n\t\t{<br \/>\n\t\t\t\/\/found it<br \/>\n\t\t} else<br \/>\n\t\t{<br \/>\n\t\t\t\/\/wasn&#8217;t on our list?!<br \/>\n\t\t\tcontinue;<br \/>\n\t\t}<\/p>\n<p>\t\tCGPoint pt =[touch locationInView:self];<br \/>\n\t\tConvertCoordinatesIfRequired(pt.x, pt.y);<br \/>\n\t\tGetMessageManager()-&gt;SendGUIEx(MESSAGE_TYPE_GUI_CLICK_MOVE,pt.x, pt.y, fingerID);<br \/>\n\t}<br \/>\n}<\/p>\n<p>[\/php]<\/p>\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}]}}