 # How to calculate velocity from Touches array

I am trying to use Touches array on Processing for Android to calculate an array with the velocity of multiple touches.

I know I can use `touches[i].x` and `touches[i].y` to get the current position of multiple touches. Them also have an unique ID `touches[i].id`. I was thinking to store previous `touches[i].x` and previous `touches[i].y`, and calculate a `PVector[] velocity` somehow from them.

I tried copying the value of `touches[i].x` this way, but i am always getting the current values equal to the previous values:

``````float[] prevTouchesX;
float[] prevTouchesY;
``````

and inside draw:

``````  prevTouchesX = new float[touches.length];
prevTouchesY = new float[touches.length];
for(int i = 0; i < touches.length; i++) {
prevTouchesX[i] = touches[i].x;
prevTouchesY[i] = touches[i].y;
}
``````

I am sure something is wrong.

How can I do that? Any other suggestions? Thanks, everyone!

I don’t know why, but now it’s working.

Hi @brunoruchiga, That’s great that your program is working. Copy your working program to see if you can reproduce the error to try figure out what works and what doesn’t 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.

I also now have a bug because the unique Id that identifies each touch sometimes is not the same as its index on the array.

@brunoruchiga===

the index (first index, second index…) has NOTHING to see with the pointer id: and that is always true …