5
\$\begingroup\$

Let's say I have a GameObject A that's moving in a circular path in orbit around a central point, and I have a second GameObject B that is capable of independent movement, and can move at a higher velocity than A. B may be located at an arbitrary point, either inside or outside of A's orbit. I want to calculate the fastest route that will cause object B to intercept object A. (The problem is not exactly "I want a spaceship to arrive at a planet", but close enough for the purposes of this question.)

The obvious idea is to compute a bunch of concentric circles around B and use some sort of divide-and-conquer guesswork to narrow in on a circle that ends up touching A's orbit at exactly the point where A would be if B moved to that point at its maximum speed. But that feels a bit clunky, not to mention computationally expensive for something that I'd want to do fairly frequently, potentially multiple times per frame. Is there a better way to calculate this?

For simplicity's sake, assume that all calculations are occurring in a 2D plane.

\$\endgroup\$
  • \$\begingroup\$ Just to make sure I'm understanding, B is able to travel in a straight line to intercept A? \$\endgroup\$ – user111144 Oct 4 '19 at 15:00
  • 1
    \$\begingroup\$ @Bilkokuya Yes, that's what I'm aiming for. (No pun intended.) \$\endgroup\$ – Mason Wheeler Oct 4 '19 at 15:23
  • \$\begingroup\$ is A's speed constant? may B's speed change? is B forced to move on a straight line? \$\endgroup\$ – Leggy7 Oct 4 '19 at 15:56
  • 1
    \$\begingroup\$ I'm going to be honest, I'm pretty certain there is an analytical solution - if you create equations for A's position and B's possible positions for a given time t (they are both circles, the radius of B's is determined by it's speed and the given time), and then solve them against each other for the intersection. But I've got half way with the maths and messed up - I'll have a rethink tonight, but hopefully somebody can write up the specific solution before then. \$\endgroup\$ – user111144 Oct 4 '19 at 16:01
  • 2
    \$\begingroup\$ You may be pleased to know that we already have two previous Q&A threads addressing exactly this! "How can I intercept an object with circular motion?", and "Determining the first future intersection possible between ships and a planet". Have you tried putting the solutions described in these answers into practice? If they're not serving your needs, please edit your question to explain where you need help, and we can re-open the question for new answers. \$\endgroup\$ – DMGregory Oct 4 '19 at 16:43
0
\$\begingroup\$

This approach uses approximation, since I'm not sure how this would be solved for a definitive answer, but this solution will probably work for your purposes, as long as you aren't Nasa.

First, you need to find how long it takes object A to make 1 full rotation (also known as the period of object A). Call this P. Then, find the distance from object A to the center of its rotation (or the radius of object A's rotation). Call that R. These are constant variables, and should be determined ahead of time.

Then, at runtime, find the distance from object A to object B, and divide that distance by B's velocity to find the time it will take B to reach A if A remained stationary. Call that time T2. To get the distance around circle, create a new Vector2 (Mathf.Cos(T2/P), Mathf.Sin(T2/P))and multiply it by R. Add the orbit's position to that vector to give you the point that A will be at when B would reach A. Call this position A'.

Finally, all you have to do is tell B to aim straight towards A'. A' is recalculated every frame, so B will travel in a more or less straight line and hit A.

Now, you will notice that A' isn't where A actually will be when B hits A. If you want more accuracy ahead of time, you can repeat this process multiple times, each time finding the distance between the projected point A' and B instead of A and B. You could do this, say, 30 times right before you launch B, and that might give you a close enough estimate that you don't need to calculate it once per frame. Or you could do this 3 times per frame, adding a little lag to give a better estimate. It really just depends on how much accuracy you need, how much performance you're willing to sacrifice for it, and whether you prefer lag spikes or slower frames.

| improve this answer | |
\$\endgroup\$

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