Jump to content
  • entries
    941
  • comments
    5,894
  • views
    867,833

Multitouch Madness


Josh

2,404 views

 Share

I've implemented multitouch input on iOS, and gave Aria the information he needs to do the same for Android devices. Multitouch is an interesting input method. Each "touch" has a beginning, some movement, and an end. To handle multiple touches, they need to be persistent; you need to keep track of which touch is which. This is a little weird if you're coming from a mouse and keyboard paradigm.

 

On iOS, you have a pointer to a UITouch object, and on Android it's a jObject object (I think). I opted to make touches correspond to fingers...the first touch is touch 0, the second is 1, and so on. I set it to handle up to five touch inputs simultaneously. (Interestingly, there doesn't seem to be any limit on the number of touches a device can handle at once. At least, I tried ten, and ran out of fingers to test any further.)

 

Touches emit an event which you can get through the event queue, or you can supply an event callback hook. Alternatively, you can just call GetTouchDown(), GetTouchHit(), GetTouchX(), etc., which work the same as mouse input. The only difference is you need to supply an index for the finger number.

 

To implement zooming, I just checked to see if finger 0 and 1 are touched, then found the distance between the two fingers. I'll have to write some detailed tutorials about this, but until then here's my code:

    while (PeekEvent())
   {
       Event event = WaitEvent();
       Print(event.Debug());
       switch (event.id)
       {
           case EVENT_KEY_DOWN:
               if ((event.data)==KEY_ESCAPE)
               {
                   return false;
               }
               break;
           case EVENT_WINDOW_CLOSE:
               if (window==event.source) return false;
               break;
           case EVENT_TOUCH_UP:
               numTouches--;
               break;
           case EVENT_TOUCH_DOWN:
               numTouches++;
               Print("Event.data = "+String(event.data));
               Print("Event.X = "+String(event.x));
               Print("Event.Y = "+String(event.y));
               Print("");
               touchposition[event.data].x = event.x;
               touchposition[event.data].y = event.y; 
               dx = touchposition[0].x - touchposition[1].x;
               dy = touchposition[0].y - touchposition[1].y;
               touchdistance = sqrt(dx*dx+dy*dy);
               break;
           case EVENT_TOUCH_MOVE:
               if (event.data==0)
               {
                   if (numTouches==3) camera->Move(-(event.x - touchposition[0].x)*0.01,(event.y - touchposition[0].y)*0.01,0);
                   touchposition[0].x = event.x;
                   touchposition[0].y = event.y;
                   dx = touchposition[0].x - touchposition[1].x;
                   dy = touchposition[0].y - touchposition[1].y;
                   d = sqrt(dx*dx+dy*dy);
                   if (numTouches==2) camera->Move(0,0,-(touchdistance - d)*0.01);

                   touchdistance = d;
                   //Print(touchdistance);
               }
               if (event.data==1)
               {
                   touchposition[1].x = event.x;
                   touchposition[1].y = event.y;
                   dx = touchposition[0].x - touchposition[1].x;
                   dy = touchposition[0].y - touchposition[1].y;
                   d = sqrt(dx*dx+dy*dy);
                   if (numTouches==2) camera->Move(0,0,-(touchdistance - d)*0.01);            
                   touchdistance = d;
                  // Print(touchdistance);
               }
               break;
       }
   }

And here's the result in action:

 Share

6 Comments


Recommended Comments

At least, I tried ten, and ran out of fingers to test any further.

And your music is... cheesy.

 

But good work nonetheless, as usual.

Link to comment

Sweet! My friend bought an Android phone today and I told him I will be able to code for it with Leadwerks3D one day. That means I'll have to buy an Android too but small price to pay, especially since my phone is so dated (it doesn't even have a camera!).

 

A quick search seems to show that the touch amount is limited by the hardware.

http://stackoverflow.com/questions/4397249/using-ipad-with-11-fingers-extend-androids-limit-in-code

but I wouldn't rely too much on just that link alone.

 

Also, it seems like this is already covered but is the device smart enough to know if a finger left the screen? In other words, say you're zooming with two fingers and you put a third on the screen and you remove the second. What happens? I guess this depends on the user's code. You'd have to make sure the third finger stays the third, even when the second leaves. You'd have to reset the zoom distance to start from between the first and third finger, I guess.

Link to comment

Also, it seems like this is already covered but is the device smart enough to know if a finger left the screen? In other words, say you're zooming with two fingers and you put a third on the screen and you remove the second. What happens? I guess this depends on the user's code. You'd have to make sure the third finger stays the third, even when the second leaves. You'd have to reset the zoom distance to start from between the first and third finger, I guess.

You'd get a TOUCH_UP event with index 1 (it starts with 0) and know the second finger left. The next finger down would become the second, because that would be the first empty slot.

Link to comment
Guest Red Ocktober

Posted

forget the music... this Leadwerks 3 thingee is looking better and better... and better...

 

nice work J...

 

--Mike

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...