Skip to main content
10 of 10
Rollback to Revision 8
doppelgreener
  • 7.3k
  • 7
  • 43
  • 69

Here's part of a solution. I didn't get to finish it in time. I'll try again later.

If I understand correctly, you have a planet's position & velocity, as well as a ship's position and speed. You want to get the ship's movement direction. I'm assuming the ship's and planet's speeds are constant. I also assume, without loss of generality, that the ship is at \$(0,0)\$; to do this, subtract the ship's position from the planet's, and add the ship's position back onto the result of the operation described below.

Let:

  • \$s_s\$ = the ship's speed (\$s_s.x, s_s.y\$, likewise)
  • \$s_a\$ = the ship's bearing (angle of movement, what we want to calculate)
  • \$p_p\$ = the planet's initial position, global coords
  • \$p_r\$ = the planet's distance (radius) from the center of orbit, derivable from \$p_p\$
  • \$p_a\$ = the planet's initial angle in radians, relative to the center of orbit
  • \$p_s\$ = the planet's angular velocity (rad/sec)
  • \$t\$ = the time to collision (this turns out to be something we must calculate as well)

Here's the equations for the position of the two bodies, broken down into components: $$ ship.x = s_s.x * t * cos(s_a) $$ $$ ship.y = s_s.y * t * sin(s_a) $$ $$ planet.x = p_r * cos(p_a + p_s * t) + p_p.x $$ $$ planet.y = p_r * sin(p_a + p_s * t) + p_p.y $$

Since we want \$ship.x = planet.x\$ and \$ship.y = planet.y\$ at some instant \$t\$, we obtain this equation (the \$y\$ case is nearly symmetrical):

$$ s_s.x * t * cos(s_a) = p_r * cos(p_a + p_s * t) + p_p.x $$ $$ s_s.y * t * sin(s_a) = p_r * sin(p_a + p_s * t) + p_p.y $$

Solving the top equation for \$s_a\$:

$$ s_s.x * t * cos(s_a) = p_r * cos(p_a + p_s * t) + p_p.x \\ \implies s_a = \frac{arccos((p_r * cos(p_a + p_s * t) + p_p.x)}{(s_s.x * t))}$$

Substituting this into the second equation results in a fairly terrifying equation that Wolfram alpha won't solve for me. There may be a better way to do this not involving polar coordinates. If anyone wants to give this method a shot, you're welcome to it; I've made this a wiki. Otherwise, you may want to take this to the Math StackExchange.

Chaosed0
  • 773
  • 5
  • 17