# How to move something to where something else will be?

**URL:** <https://discourse.processing.org/t/how-to-move-something-to-where-something-else-will-be/34545>\
**Category:** Project Guidance\
**Created:** [January 9, 2022, 6:09pm UTC](https://discourse.processing.org/t/how-to-move-something-to-where-something-else-will-be/34545 "2022-01-09T18:09:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![millibyte](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@millibyte](https://discourse.processing.org/u/millibyte)\
**Post date:** [January 9, 2022, 6:09pm UTC](https://discourse.processing.org/t/how-to-move-something-to-where-something-else-will-be/34545/1 "2022-01-09T18:09:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 12, 2022, 10:31pm UTC](https://discourse.processing.org/t/how-to-move-something-to-where-something-else-will-be/34545/2 "2022-01-12T22:31:01Z")

</div>

> [@millibyte](#):
>
> How would I calculate the velocity PVector so that the ball moves where the receiver will be along his route?

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

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

---

<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:** [January 13, 2022, 1:35am UTC](https://discourse.processing.org/t/how-to-move-something-to-where-something-else-will-be/34545/3 "2022-01-13T01:35:12Z")

</div>

Hello @millibyte,

A simplified example of what I did with some vectors:

```auto
// 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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ede3f1415b522569bf2568a4500c3a71d375a3d2.png)

Animation (hit Play button):

![vectors](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/26477989aa3090b4c126e8e7c9eacc7c1842db16.gif)

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/reference/PVector.html)  
[https://processing.org/tutorials/pvector](https://processing.org/tutorials/pvector)  
[https://natureofcode.com/book/chapter-1-vectors/](https://natureofcode.com/book/chapter-1-vectors/)

`:)`
