2
\$\begingroup\$

This question has been asked many times before, but most of the answers give solution to the problem where ball (target) is assumed to be moving at a constant velocity.

In my scenario, I'm making a football game. When I kick the ball into a direction by applying some force to the ball's rigidbody, I want a second AI player to find the point in the ball's path where he can intercept the ball. Now this ball can keep slowing down per frame, it does not keep moving with a constant speed.

I have attached an image for visualisation.

enter image description here

I know the ball's position, ball's current velocity, AI player's position, AI player's constant speed. How do I find such a point, where AI player can intercept the ball?

I am able to calculate such a point with the current velocity of the ball, but that point is not accurate, and changes as ball's current velocity changes per frame, as the ball can keep slowing down after every frame. This makes the AI player act kind of like a homing missile. Instead, I want the AI player to predict a point and just run to it.

Here's the C# (Unity) Code I have till now:

public Vector3 CalculateIntercept()    
{       
     Vector3 pos = Ball.GetInstance().transform.position;
     Vector3 dir = Ball.GetInstance().GetVelocity();
     dir.y = transform.position.y;

     float dist = (pos - transform.position).magnitude;
     return pos + (dist / 6.17f) * dir; //6.17 is the AI player's constant speed.
}

There's no drag on the ball, but there is a physics material with friction. I suppose to solve this, I might need to account for acceleration maybe.

Please help me figure this out. Thanks!

\$\endgroup\$
0
\$\begingroup\$

Your only option is to dry run the simulation as the ball is kicked, then have the Ai begin moving to that location.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ What do you mean by dry run? Do you mean run the simulation ahead of time? Can you help me understand how this can be done in unity if possible? \$\endgroup\$ – Ashish Gogna Aug 18 at 17:03
  • \$\begingroup\$ Why would this be the only option? Granted, modelling the slowdown of the ball and intercepting this non-linear motion is messy, but it's by no means impossible. \$\endgroup\$ – DMGregory Aug 18 at 17:20
  • \$\begingroup\$ By Dry Run I mean you need to calculate the position of the ball at a specified time, this is the same as running the simulation-ish, as you'll need to run the equations to calculate this. @DMGregory I guess it's not the only option on a second look. You could use a graph function that closely resembles distance at time and query that using an initial velocity provided by the kick. I guess that would model psychology better as humans can judge initial velocity and then query their own experience for how far that ball will travel. \$\endgroup\$ – Natalo77 Aug 19 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.