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.
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:
Animation (hit Play button):
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.