This code works:
Maybe there are some bad flaws, but now its working this way. It calculates an array of PVector velocities for each touch at the same time on the screen (with maximum of 10 touches), for multitouch on Android Mode.
int maxMultiTouch = 10;
PVector[] vels = new PVector[maxMultiTouch];
float[] prevTouchesX = new float[maxMultiTouch];
float[] prevTouchesY = new float[maxMultiTouch];
void setup() {
fullScreen();
for (int i = 0; i < maxMultiTouch; i++) {
vels[i] = new PVector();
prevTouchesX[i] = -1;
prevTouchesY[i] = -1;
}
}
void draw() {
background(0);
translate(width/2, height/2);
for (int i = 0; i < touches.length && i < maxMultiTouch; i++) {
if (prevTouchesX[i] != -1 && prevTouchesY[i] != -1) {
//Calculate velocities from previous touch position and current touch position
vels[i].set(touches[i].x - prevTouchesX[i], touches[i].y - prevTouchesY[i]);
//Display
stroke(255);
line(0, 0, vels[i].x, vels[i].y);
println(i, vels[i].x, vels[i].y);
}
}
//Update previous or set to -1 if no longer exists
for (int i = 0; i < prevTouchesX.length; i++) {
if (i < touches.length) {
prevTouchesX[i] = touches[i].x;
prevTouchesY[i] = touches[i].y;
} else {
prevTouchesX[i] = -1;
prevTouchesY[i] = -1;
}
}
}
I am not sure how to deal with the touches when they end, so I am setting them to -1
. I am not sure if I should have and array with variable size for that.