# 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:** 6
**Page:** 1

<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 15, 2018, 3:06am UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552/1 "2018-11-15T03:06:10Z")

</div>

I am trying to use [Touches array](https://android.processing.org/reference/multitouch/touches.html) 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:

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

```

and inside draw:

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

---

<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 15, 2018, 7:13pm UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552/2 "2018-11-15T19:13:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)
#### Post date: [November 16, 2018, 7:29am UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552/3 "2018-11-16T07:29:25Z")

</div>

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 🙂

---

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

---

<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, 5:05pm UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552/5 "2018-11-17T17:05:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [November 18, 2018, 6:28pm UTC](https://discourse.processing.org/t/how-to-calculate-velocity-from-touches-array/5552/6 "2018-11-18T18:28:48Z")

</div>

@brunoruchiga===

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