# Would you call this way to measure velocity "correct"?

**URL:** <https://discourse.processing.org/t/would-you-call-this-way-to-measure-velocity-correct/31697>\
**Category:** Coding Questions\
**Created:** [August 12, 2021, 11:52am UTC](https://discourse.processing.org/t/would-you-call-this-way-to-measure-velocity-correct/31697 "2021-08-12T11:52:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![G03TH3](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/g03th3/32/14033_2.png) [@G03TH3](https://discourse.processing.org/u/G03TH3)\
**Post date:** [August 12, 2021, 11:52am UTC](https://discourse.processing.org/t/would-you-call-this-way-to-measure-velocity-correct/31697/1 "2021-08-12T11:52:14Z")

</div>

```auto
float x;
float playbackSpeed;
float x_po, pr_x, pa_x, x_d;
float prevTime = 0;
float dt, velocity = 1;
float currTime;

void setup() {
  size(640, 360);
  background(255);
}

void draw() {
  x_po+=10; // X1-POSITION
  if (x_po > width) // CONDITIONAL STATEMENT FOR RESETTING THE X-POSITION
    x_po = 0;
  x_d = abs((pa_x-x_po)); // CALCULATE THE PAST-X AND PRESENT-X DIFFERENCE

  println("x_po = "+x_po); // PRINT X-POSITION
  println("pa_x = "+ pa_x); // PRINT PAST-X POSITION
  println("x_d = "+x_d); // DIFFERENCE 
  pa_x = x_po; // SAVE CURRENT-X VALUE INTO PAST-X VALUE

  println("");

  currTime = millis(); // TAKE TIME
  if (x_d != 0) { // IF THERE IS MOVEMENT D CALCULATE DELTA TIME
    dt = abs(prevTime-currTime);
  } else if (x_d == 0) { IF THERE IS NO MOVEMENT DO NO CALCULATE DELTA TIME
    dt = 0.1;
  }
  println("currTime"+currTime); //PRINT PRESENT TIME
  println("prevTime"+prevTime); // PRINT PAST TIME
  println("dt ="+dt); //PRINT DELTA TIME
  velocity =x_d/(dt/1000); // CALCULATE DELTA X/DELTA TIME = VELOCITY
  println("Velocity= "+velocity); // PRINT VELOCITY
  prevTime = currTime; // SAVE OLD PRESENT TIME INTO PREV TIME.
  float velocity_c = map(velocity, 0, 1000,0,100); // CONVERT VELOCITY INTO A MORE "UNDERSTANDABLE" VALUE. 
  velocity_c = constrain(velocity_c, 0, 100); // CONSTRAIN NEW VELOCITY VALUE
  println("velocity_c"+velocity_c); // PRINT NEW CONSTRAINED VELOCITY VALUE

  fill(255, 0, 0); // RED 
  ellipse(x_po, height/2, 45, 45); // DISPLAY MY X-1 COORDINATE AS A BALL

  println("");
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 12, 2021, 12:54pm UTC](https://discourse.processing.org/t/would-you-call-this-way-to-measure-velocity-correct/31697/2 "2021-08-12T12:54:30Z")

</div>

> [@G03TH3](#):
>
> if (x\_d != 0) { // IF THERE IS MOVEMENT D CALCULATE DELTA TIME  
> dt = abs(prevTime-currTime);  
> } else if (x\_d == 0) { IF THERE IS NO MOVEMENT DO NO CALCULATE DELTA TIME  
> dt = 0.1;  
> }

Taking into account

- currTime is always greater than prevTime
- and if x\_d != 0 then the else statement is only executed if x\_d ==0, no need to test again

the code becomes

```auto
if (x_d != 0) { // IF THERE IS MOVEMENT D CALCULATE DELTA TIME
  dt = currTime - prevTime;
} else { //IF THERE IS NO MOVEMENT DO NO CALCULATE DELTA TIME
  dt = 0.1;
}

```

Also need to initialise prevTime and currTime in setup

```auto
void setup() {
  size(640, 360);
  background(255);
  currTime = prevTime = millis();
}

```

Don’t assume prevTime is actually zero, depends on how much work is done in `setup`

The technique you are using will compensate for changes in framerate so is good 👍

---

<div class="post-metadata">

**Author:** ![G03TH3](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/g03th3/32/14033_2.png) [@G03TH3](https://discourse.processing.org/u/G03TH3)\
**Post date:** [August 13, 2021, 6:46am UTC](https://discourse.processing.org/t/would-you-call-this-way-to-measure-velocity-correct/31697/3 "2021-08-13T06:46:28Z")

</div>

quark!, thank you so much for your review, I will make sure to learn and implement all the things you’ve mentioned, thanks for your time and advice 🕶

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 13, 2021, 9:27am UTC](https://discourse.processing.org/t/would-you-call-this-way-to-measure-velocity-correct/31697/4 "2021-08-13T09:27:50Z")

</div>

Hello,

This helps to see the status of frameCount and millis() in setup() and draw():

```auto
void setup() 
  {
  frameRate(1); // I slowed this down for testing
  currTime = prevTime = millis();
  println(frameCount, currTime, prevTime, dt);  
  }

void draw() 
  {
  currTime = millis();
  dt = currTime - prevTime;
  println(frameCount, currTime, prevTime, dt);	  
  prevTime = currTime;
  }

```

I also slowed down frameRate() in this example for testing.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/86400a4a13abb2f63e47bd8307ecaa7328465cf2.png)

I used variable names and suggestions in topic.

@G03TH3 I also provided an example of how to properly format code. Hint.

[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`
