Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have rocks orbiting moons, moons orbiting planets, planets orbiting suns, and suns orbiting black holes, and the current system could have many many layers of orbitage.

the position of any object is a function of time and relative to the object it orbits. (so far so good).

now I want to know for a given 2 objects(A,B), a start time and a speed, how can I work out the when and where to go. I can work out where A and B is given a time. so i just need.

1: direction to travel in from A to B(remember B is moving(not in a straight line)) 2: Time to get to b in a straight line.

travel must be in a straight line with the shortest possible distance.

as an extension to this question, how will i know if its better to wait, EG is it faster to stay on object A and wait for a hour when the objects may be closer, than to set off from A to B at the start.

Cheers, it hurt my brain.

share|improve this question

2 Answers 2

If your bodies are subject to complex orbits it may well be difficult or impossible to solve using a closed-form expression (solve an equation to get the desired results.

One approach would be to perform a mathematical simulation over time to arrive at a solution.

In your case, a body leaving A at a time t0 travelling in a straight line with velocity v will be somewhere on the circumference of a circle or (sphere in 3D) with radius vt (t=time since leaving). The centre of the circle will be the position of A at the time of launch (Apos(t0)). You can imagine this circle like a ripple on a pond getting bigger over time)

Assuming you can calculate the position of B at time t+t0 (Bpos(t+t0)), you can create a simulation with a loop to find out the earliest time that the growing circle intersects B's path

searching=true
t=0
while(searching){
    if (distance between Apos(t0) and Bpos(t0+t)) < v*t then{
        searching=false //found a solution
        target_position=Bpos(t0+t) // can be used to find direction to head in
        time_to_reach_B=t
    }
    else
        t+=delta_t //a small time step (smaller = more accuracy)
}

You will of course have to add code to prevent an infinite loop in the case that there is no solution (or no solution to be found in reasonable time).

Depending on the complexity or B's motion and the value of delta_t, this algorithm is not guaranteed to find the ideal solution in every case, but should work well for the vast majority of cases.

share|improve this answer

That's not so bad if you know the position of A and B at all times. Example:

A is at the origin. B is traveling around the origin at a distance of 1. (ie, it's orbit is the unit circle) Let Y be the object leaving A.

Let the speed of B be such that every second B crosses an axis. So the period of B is 4 seconds. Therefore the position of B at any given point in time is: (cos(t * pi/2), sin(t * pi/2))

Let the speed of Y be such that every second Y can travel s distance. So after one second, if travelling along the positive x axis, Y will be s away from A. Therefore A creates another "orbit" to how far it can go every second based on:

sqrt(x^2+y^2) = s*t

Now you just need to determine what value of t is required to determine when these two things will intersect.

This is a very simple case but it will more or less work for any 2D situation you would just obviously need to change the numbers around. Since you already have a function for B you can just replace mine with yours. The other equation is the same for every case.

Now if you add another dimension and introduce physics modeling more of how our world works then this becomes nontrivial and could be very difficult and I'd just avoid it entirely.

You'll know to wait if it takes longer to travel at the current position than it does at the time it takes to travel at a later position plus the wait time.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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