Measure velocity of a dynamic point

edited February 2014 in How To...

Referencing this tutorial:

I am aware how to create a velocity of an object / boundaries, etc. What I am trying to achieve is measuring a dynamic velocity of a point moving within window coordinate space. I am using a kinect thats tracking the coordinates of a ball, and would like it to compare the velocity from its current point to its new previous point. I checked the API, but some functions were depreciated.

Answers

  • So what's the problem exactly?

    If you have all the positions, then you have all the velocities (aka the distance between consecutive points).

  • Please clarify: Are you trying to find the velocity of the point in the window that the ball is at, or the velocity of the ball in real-world space?

  • edited February 2014

    Velocity of the "virtual" ball (point im choosing from skeleton data) in relation to window, i.e.

    ellipse(movingX, movingY, 30,30);  
    

    and velocity of current location(movingX, movingY) to its new location of movingX + Y

  • Answer ✓

    Well, record the ball's position from the previous frame, and then the velocity is the vector between those two points.

  • Answer ✓

    To get the actual velocity you would need to measure the distance moved between frames and divide this by the elapsed time.

    The code below does that. Just in case you are confusing velocity with speed it also calculates the speed.

    // The system time for this and the previous frame (in milliseconds)
    int currTime, prevTime;
    // The elapsed time since the last frame (in seconds)
    float deltaTime;
    
    float lastMovingX, lastMovingY;
    float movingX, movingY;
    float velX, velY, speed;
    
    void setup() {
      size(400, 400);
    
      // Initialise the timer
      currTime = prevTime = millis();
      // initialize position
      lastMovingX = lastMovingY = Float.MAX_VALUE;
    }
    
    void draw() {
      // get the current time
      currTime = millis();
      // calculate the elapsed time in seconds
      deltaTime = (currTime - prevTime)/1000.0;
      // remember current time for the next frame
      prevTime = currTime;
    
      ellipse(movingX, movingY, 30,30); 
      // Calculate velocity in X and Y directions (pixels / second)
      if(lastMovingX != Float.MAX_VALUE){
        velX = (movingX - lastMovingX) / deltaTime;
        velY = (movingY - lastMovingY) / deltaTime;
        speed = sqrt(velX*velX + velY*velY);
      }
      lastMovingX = movingX;
      lastMovingY = movingY;
    }
    
Sign In or Register to comment.