Multi touch improvement

Hi everyone. I’m not sure if y’all need multi touch for Android but this will do the trick. I made some improvements to Ak Eric’s code. This new code can track which finger you removed and finger# is only false if you remove that finger. It works by tracking which finger moved the most when you take it off. Heres the code. If anyone wants me to explain more how it works let me know.

// original source http://www.akeric.com/blog/?p=1411.html

import android.view.MotionEvent;

 float x,y;

String[] fontList;

PFont androidFont;

int circleBaseSize = 512; 

boolean tap;

boolean fingerRemoved;


int finger1stop = 0;
int finger2stop = 0;
int finger3stop = 0;
int finger4stop = 0;
int finger5stop = 0;

int finger1backup = 0;
int finger2backup = 0;
int finger3backup = 0;
int finger4backup = 0;
int finger5backup = 0;


boolean finger1real = false;
boolean finger2real = false;
boolean finger3real = false;
boolean finger4real = false;
boolean finger5real = false;


boolean finger1 = false;
boolean finger2 = false;
boolean finger3 = false;
boolean finger4 = false;
boolean finger5 = false;


float mouseX1 = -999999;
float mouseY1 = -999999;
float mouseX2 = -999999;
float mouseY2 = -999999;
float mouseX3 = -999999;
float mouseY3 = -999999;
float mouseX4 = -999999;
float mouseY4 = -999999;
float mouseX5 = -999999;
float mouseY5 = -999999;


int [] mouseX2keep;
int [] mouseY2keep;

int [] mouseX1keep;
int [] mouseY1keep;

int [] mouseX3keep;
int [] mouseY3keep;

int [] mouseX4keep;
int [] mouseY4keep;

int [] mouseX5keep;
int [] mouseY5keep;


int fingers = 0;

int biggestfinger = 0;

int repeat = 0;


boolean same = true;


void setup() {

  size(displayWidth, displayHeight);

  // Fix the orientation so the sketch won't reset when rotated.

  orientation(PORTRAIT);

  stroke(255);

  smooth();

  // Setup Fonts:

  fontList = PFont.list();

  androidFont = createFont(fontList[0], 16, true);

  textFont(androidFont);

  textAlign(LEFT);
mouseX2keep = new int [40];
mouseY2keep = new int [40];

mouseX1keep = new int [40];
mouseY1keep = new int [40];

mouseX3keep = new int [40];
mouseY3keep = new int [40];

mouseX4keep = new int [40];
mouseY4keep = new int [40];

mouseX5keep = new int [40];
mouseY5keep = new int [40];
}

 

void draw() {
background(0);
fingerRun();

textSize(20);



if(finger1){
fill(color(255,0,0));
ellipse(mouseX1,mouseY1,200,200);
fill(255);
text("finger 1",mouseX1 - 50,mouseY1 - 70);
}

if(finger2){
fill(color(0,255,0));
ellipse(mouseX2,mouseY2,200,200);
fill(255);
text("finger 2",mouseX2 - 50,mouseY2 - 70);
}

if(finger3){
fill(color(0,0,255));
ellipse(mouseX3,mouseY3,200,200);
fill(255);
text("finger 3",mouseX3 - 50,mouseY3 - 70);
}

if(finger4){
fill(color(255,255,0));
ellipse(mouseX4,mouseY4,200,200);
fill(255);
text("finger 4",mouseX4 - 50,mouseY4 - 70);
}

if(finger5){
fill(color(255,0,255));
ellipse(mouseX5,mouseY5,200,200);
fill(255);
text("finger 5",mouseX5 - 50,mouseY5 - 70);
}



}

 









void infoCircle(float x, float y, float siz, int id) {

  // What is drawn on sceen when touched.

  


  

  

 






if(id == 0){
biggestfinger = 1;
}

if(id == 1){
biggestfinger = 2;
}

if(id == 2){
biggestfinger = 3;
}

if(id == 3){
biggestfinger = 4;
}

if(id == 4){
biggestfinger = 5;
}




if(tap){
if(id == 0){
finger1real = true;
mouseX1 = x;
mouseY1 = y;
finger1stop = 2;
finger1backup = 7;
}
if(id == 1){
finger2real = true;
mouseX2 = x;
mouseY2 = y;
finger2stop = 2;
finger2backup = 7;
}
if(id == 2){
finger3real = true;
mouseX3 = x;
mouseY3 = y;
finger3stop = 2;
finger3backup = 7;
}

if(id == 3){
finger4real = true;
mouseX4 = x;
mouseY4 = y;
finger4stop = 2;
finger4backup = 7;
}

if(id == 4){
finger5real = true;
mouseX5 = x;
mouseY5 = y;
finger5stop = 2;
finger5backup = 7;
}


if(!(id == 0)){
finger1backup = finger1backup - 1;
}

if(!(id == 1)){
finger2backup = finger2backup - 1;
}

if(!(id == 2)){
finger3backup = finger3backup - 1;
}

if(!(id == 3)){
finger4backup = finger4backup - 1;
}

if(!(id == 4)){
finger5backup = finger5backup - 1;
}



}






}

 

//-----------------------------------------------------------------------------------------

// Override Processing's surfaceTouchEvent, which will intercept all

// screen touch events.  This code only runs when the screen is touched.

 

 boolean surfaceTouchEvent(MotionEvent me) {

  // Number of places on the screen being touched:

  int numPointers = me.getPointerCount();

  for (int i=0; i < numPointers; i++) {

    int pointerId = me.getPointerId(i);

    float x = me.getX(i);

    float y = me.getY(i);


    

    // Added the .5 to make the outer ellipse noticeable

    float siz = me.getSize(i) + .5;

    infoCircle(x, y, siz, pointerId);

  }

  // If you want the variables for motionX/motionY, mouseX/mouseY etc.

  // to work properly, you'll need to call super.surfaceTouchEvent().

  return super.surfaceTouchEvent(me);

}

void touchStarted(){
fingers = fingers + 1;
} 

void touchEnded(){
fingers = fingers - 1;
fingerRemoved = true;
}



void fingerRun(){
if(fingers > 0){
tap = true;
}
if(fingers == 0){
tap = false;
biggestfinger = 0;
}


if(fingerRemoved){

if(biggestfinger == 1){
//finger2 = false;
}





repeat = 0;
same = true;
while(repeat < 20){
if(mouseX2keep[repeat] > mouseX2keep[repeat + 1] + 2|| mouseX2keep[repeat] < mouseX2keep[repeat + 1] - 2){
same = false;
}
if(mouseY2keep[repeat] > mouseY2keep[repeat + 1] + 2|| mouseY2keep[repeat] < mouseY2keep[repeat + 1] - 2){
same = false;
}


repeat = repeat + 1;
}
if(same == false){
//println("done");
finger2real = false;
}








repeat = 0;
same = true;
while(repeat < 20){
if(mouseX1keep[repeat] > mouseX1keep[repeat + 1] + 2|| mouseX1keep[repeat] < mouseX1keep[repeat + 1] - 2){
same = false;
}
if(mouseY1keep[repeat] > mouseY1keep[repeat + 1] + 2|| mouseY1keep[repeat] < mouseY1keep[repeat + 1] - 2){
same = false;
}


repeat = repeat + 1;
}
if(same == false){
//println("done");
finger1real = false;
}




repeat = 0;
same = true;
while(repeat < 20){
if(mouseX3keep[repeat] > mouseX3keep[repeat + 1] + 2|| mouseX3keep[repeat] < mouseX3keep[repeat + 1] - 2){
same = false;
}
if(mouseY3keep[repeat] > mouseY3keep[repeat + 1] + 2|| mouseY3keep[repeat] < mouseY3keep[repeat + 1] - 2){
same = false;
}


repeat = repeat + 1;
}
if(!(same)){
//println("done");
finger3real = false;
}





repeat = 0;
same = true;
while(repeat < 20){
if(mouseX4keep[repeat] > mouseX4keep[repeat + 1] + 2|| mouseX4keep[repeat] < mouseX4keep[repeat + 1] - 2){
same = false;
}
if(mouseY4keep[repeat] > mouseY4keep[repeat + 1] + 2|| mouseY4keep[repeat] < mouseY4keep[repeat + 1] - 2){
same = false;
}


repeat = repeat + 1;
}
if(same == false){
//println("done");
finger4real = false;
}




repeat = 0;
same = true;
while(repeat < 20){
if(mouseX5keep[repeat] > mouseX5keep[repeat + 1] + 2|| mouseX5keep[repeat] < mouseX5keep[repeat + 1] - 2){
same = false;
}
if(mouseY5keep[repeat] > mouseY5keep[repeat + 1] + 2|| mouseY5keep[repeat] < mouseY5keep[repeat + 1] - 2){
same = false;
}


repeat = repeat + 1;
}
if(same == false){
//println("done");
finger5real = false;
}






fingerRemoved = false;
}





if(fingers == 0){
finger1real = false;
finger2real = false;
finger3real = false;
finger4real = false;
finger5real = false;
}


repeat = 20;
mouseX2keep[0] = round(mouseX2);
while(repeat > 0){
mouseX2keep[repeat] = mouseX2keep[repeat - 1];
repeat = repeat - 1;
}

repeat = 20;
mouseY2keep[0] = round(mouseY2);
while(repeat > 0){
mouseY2keep[repeat] = mouseY2keep[repeat - 1];
repeat = repeat - 1;
}





repeat = 20;
mouseY1keep[0] = round(mouseY1);
while(repeat > 0){
mouseY1keep[repeat] = mouseY1keep[repeat - 1];
repeat = repeat - 1;
}

repeat = 20;
mouseX1keep[0] = round(mouseX1);
while(repeat > 0){
mouseX1keep[repeat] = mouseX1keep[repeat - 1];
repeat = repeat - 1;
}




repeat = 20;
mouseY3keep[0] = round(mouseY3);
while(repeat > 0){
mouseY3keep[repeat] = mouseY3keep[repeat - 1];
repeat = repeat - 1;
}

repeat = 20;
mouseX3keep[0] = round(mouseX3);
while(repeat > 0){
mouseX3keep[repeat] = mouseX3keep[repeat - 1];
repeat = repeat - 1;
}




repeat = 20;
mouseY4keep[0] = round(mouseY4);
while(repeat > 0){
mouseY4keep[repeat] = mouseY4keep[repeat - 1];
repeat = repeat - 1;
}

repeat = 20;
mouseX4keep[0] = round(mouseX4);
while(repeat > 0){
mouseX4keep[repeat] = mouseX4keep[repeat - 1];
repeat = repeat - 1;
}




repeat = 20;
mouseY5keep[0] = round(mouseY5);
while(repeat > 0){
mouseY5keep[repeat] = mouseY5keep[repeat - 1];
repeat = repeat - 1;
}

repeat = 20;
mouseX5keep[0] = round(mouseX5);
while(repeat > 0){
mouseX5keep[repeat] = mouseX5keep[repeat - 1];
repeat = repeat - 1;
}







if(finger1real){
finger1 = true;
finger1stop = 4;
}

if(!(finger1real)){
finger1stop = finger1stop - 1;
}

if(finger1stop < 0){
finger1 = false;
}


if(finger2real){
finger2 = true;
finger2stop = 4;
}

if(!(finger2real)){
finger2stop = finger2stop - 1;
}

if(finger2stop < 0){
finger2 = false;
}



if(finger3real){
finger3 = true;
finger3stop = 4;
}

if(!(finger3real)){
finger3stop = finger3stop - 1;
}

if(finger3stop < 0){
finger3 = false;
}



if(finger4real){
finger4 = true;
finger4stop = 4;
}

if(!(finger4real)){
finger4stop = finger4stop - 1;
}

if(finger4stop < 0){
finger4 = false;
}



if(finger5real){
finger5 = true;
finger5stop = 4;
}

if(!(finger5real)){
finger5stop = finger5stop - 1;
}

if(finger5stop < 0){
finger5 = false;
}




if(finger1backup < 1){
finger1real = false;
finger1 = false;
}


if(finger2backup < 1){
finger2real = false;
finger2 = false;
}


if(finger3backup < 1){
finger3real = false;
finger3 = false;
}



if(finger4backup < 1){
finger4real = false;
finger4 = false;
}


if(finger5backup < 1){
finger5real = false;
finger5 = false;
}



}
3 Likes

Hey, great contribution. Works perfect on my Kitkat and Lollipop. Thanks

If you want me to explain how i got it to work (detect which finger was removed) let me know and I’ll add comments to the code