# How to calculate velocity from Touches array

**URL:** <https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552>\
**Category:** Processing for Android\
**Created:** [November 15, 2018, 3:06am UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552 "2018-11-15T03:06:10Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![brunoruchiga](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brunoruchiga/32/2235_2.png) [@brunoruchiga](https://discourse.processing.org/u/brunoruchiga)\
**Post date:** [November 17, 2018, 4:58pm UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552/4 "2018-11-17T16:58:33Z")

</div>

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.

```auto
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.

---

_[View the full topic](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552)._
