- Home /
How can I get an object to intercept a moving object's path?
I have been unsuccessful in trying to find an answer to this question on Google based on wording.
If I have a stationary object and there is a moving object coming near it, how can I set that object on a path to intercept the moving one? I have the very beginning of it, detecting when that object is within a certain radius, but now I'm wondering where do I start to calculate an intercept path based on the moving object's trajectory?
Answer by Aevek · Aug 11, 2013 at 04:43 AM
Basically, just Vector math. You're going to want to work out a how long your stationary object is (probably) going to take to get to an intercept point on the other one, then work out where the other one is going to be at that time, probably by multiplying that object's movement vector by the time.
You could also run a loop which moves the intercept point around until it finds the best match, knowing the other objects speed and path. It's problems like this that always prompt me to keep plenty of scratch paper near my desk.
But as a third point, typically for a basic 'zombie' AI, I just have the object move to where the moving object is, and that creates a nice chase thing.
Answer by PoyrazGoksel · Mar 08, 2020 at 03:53 AM
This is what i did using simple maths and physics works 100% precise
/// <summary>
/// <para>Since Laser speed is constant no need to calculate relative speed of laser to get interception pos!</para>
/// <para>Calculates interception point between two moving objects where chaser speed is known but chaser vector is not known(Angle to fire at * LaserSpeed"*Sort of*")</para>
/// <para>Can use System.Math and doubles to make this formula NASA like precision.</para>
/// </summary>
/// <param name="PC">Turret position</param>
/// <param name="SC">Speed of laser</param>
/// <param name="PR">Target initial position</param>
/// <param name="VR">Target velocity vector</param>
/// <returns>Interception Point as World Position</returns>
public Vector3 CalculateInterceptionPoint3D(Vector3 PC, float SC, Vector3 PR, Vector3 VR)
{
//! Distance between turret and target
Vector3 D = PC - PR;
//! Scale of distance vector
float d = D.magnitude;
//! Speed of target scale of VR
float SR = VR.magnitude;
//% Quadratic EQUATION members = (ax)^2 + bx + c = 0
float a = Mathf.Pow(SC, 2) - Mathf.Pow(SR, 2);
float b = 2 * Vector3.Dot(D, VR);
float c = -Vector3.Dot(D, D);
if ((Mathf.Pow(b, 2) - (4 * (a * c))) < 0) //% The QUADRATIC FORMULA will not return a real number because sqrt(-value) is not a real number thus no interception
{
return Vector2.zero;//TODO: HERE, PREVENT TURRET FROM FIRING LASERS INSTEAD OF MAKING LASERS FIRE AT ZERO!
}
//% Quadratic FORMULA = x = ( -b+sqrt( ((b)^2) * 4*a*c ) ) / 2a
float t = (-(b) + Mathf.Sqrt(Mathf.Pow(b, 2) - (4 * (a * c)))) / (2 * a);//% x = time to reach interception point which is = t
//% Calculate point of interception as vector from calculating distance between target and interception by t * VelocityVector
return ((t * VR) + PR);
}
Answer by rupakyeware · Sep 06, 2020 at 11:17 AM
@PoyrazGoksel how do I make my game object move to the point of interseption?