How to move something to where something else will be?

I am writing a football video game, and I just started the passing system. I am trying to figure out how to place the ball where the receiver will be. Here’s what I’m working with:

A Player object has a position and velocity, which are both PVectors. Let’s say that the quarterback’s position is (0, 0), and he is not moving. The receiver’s position is 100 pixels to the right of the quarterback, and he is moving upwards (the negative y direction) at a rate of 1 pixel per frame.

A Ball object has a position and velocity, which are both PVectors. When the ball is thrown, the position is set to the passer’s position, and the velocity is set to some number of pixels per second (for this example, let’s say 5 pixels per second). How would I calculate the velocity PVector so that the ball moves where the receiver will be along his route?

Note: there is no acceleration, so all players and the ball are moving in straight lines.

1 Like
  • The ball starts at 0,0 with speed of 5 pixels per second.

  • receiver is at 100,0 and moving UP at a rate of 1 pixel per frame.

Solution 1

Ball: one sec has approx. 60 frames. So 5 pixels per 60 frames. So rate is 1 pixel per 12 frames.

  • receiver.y = 0 - 1 * frameCounterMy; // rate of 1 pixel per frame.

  • ball.y = 0 - 12 * frameCounterMy; // rate is 1 pixel per 12 frames

I just realized the math is way too complicate for me.

     * 100, ? 
    **
c  * *    b  
  *  *
 *   * 
****** 100, 0 
0,0
  -- a -- 

c = sqrt ( a*a + b*b );

with a = 100; // x dist
and b = ? // y dist

Solution 2

Let’s assume both had the same speed.

Then the receiver would make 25 px e.g.
Then the ball would fly to 100,-25

Look at lerp();

This would adjust the speed automatically so it hits the position of the receiver when you use the assumption of 25 or 60.

1 Like

Hello @millibyte,

A simplified example of what I did with some vectors:

// Vector Example
// v1.0.1
// GLV 2021-01-12

PVector d1, d2, d3;

void settings()
  {
  size(400, 400, P2D);
  }

void setup()
  {
  d1 = new PVector(200, 0);  // x-axis
  d2 = new PVector(0, 1);    // y-axis football player
  d3 = new PVector(0, 0);    // ball trajectory straight line

  int t1 = 1;
  int t2 = 100;
  int t3 = 100;

  //d1.mult(t1);
  d2.mult(t2);
  d3.mult(t3);
  strokeWeight(3);
  }

public void draw()
  {
  background(255);
  translate(width/4, height/2);

  line(0, 0, d1.x, d1.y);
  line(d1.x, d1.y, d1.x + d2.x, d1.y + d2.y);
  line(0, 0, d1.x + d2.x, d1.y + d2.y);
  }

Once I had the final vectors I was able to generate random vectors and plot any trajectory over time.
I will leave that for you to think about.

From above code:

image

Animation (hit Play button):

vectors

I cooked this up very quickly as a vector example with some initial vector and variable names.
The variable names in example do not represent anything at this time such as velocity, distance and time.

References:
https://processing.org/reference/PVector.html
https://processing.org/tutorials/pvector
https://natureofcode.com/book/chapter-1-vectors/

:)