Background: Attempting to write a game where 'FTL' travel is unaffected by gravity and acceleration is instant.
How do I calculate where a planet will be, given the Kepler orbit for the planet and a ships current position and its maximum FTL speed. (in m/s)
I can get the position of the planet for a given DateTime, but I'm struggling to figure out how to calculate where a planet will be, and where to send the ship to, without chasing the planet around the orbit.
I would iterate...
compute distance between planet and ship current position
from that you compute how much time your ship need to meet the target if the target would be static (not moving). Lets call this time
t
.compute planet position in
actual_time+t
and computet
for this new positionremember last
t
lets call itt0
. Then compute newt
in the same way as in #1 but for position of the planet aftert
.loop #2
stop if
fabs(t-t0)<accuracy
.This iterative solution should be closer to the finding
t
with each iteration unless your planet moves too fast and/or ship is really too far or too slow (initialt
is significant part or even bigger than the planets tropical year). In such case You usually first jump into the star system and then jump to planet (Like in original Elite).For obscure planetary movements (like too small orbital period) you would need different methods but realize that such case implies either planet very near to star or very heavy system central mass like black hole...
In code with constant FTL speed it would look like this:
This should converge pretty quickly (like 5-10 iterations should be enough) Now
t
should hold the time needed for travel andpp
should hold the position your ship should head to. How ever ifi>=100
no solution was found so you first need to go closer to the system, or use faster FTL or use different method but that should not be the case for common in stellar system FTL as the travel time should be far less then the targets orbital period...btw. this might interest you:
[Edit1] slower than FTL translation drive
I give it a bit of taught and change the algo a bit. First it check all the positions along whole planet period with some step (100 points per period) and remember the closest time to travel to the ship regardless of periods of planet passed during the travel. Then simply "recursively" check around best location with smaller and smaller angle step. Here Preview of result:
And updated source (full VCL app code so just use/port what you need and ignore the rest)
The important stuff is in the two classes
planet,ship
thensim_t
is actual simulated time andsim_dt
is simulated time step. In this case simulation stops after ship reach its destination. Theship::tim
is the time of travel left computed in theship::intercept()
along with direction for preset speed. The update should be called on each simulated time step ...