# Help calculating the correct direction between two points

This topic is 1883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey

I have an animation of two objects rotating in different circles at different speeds. I then have two possible directions in which to travel to and from each object (red & green line represent this).

What I am stuck with is how I work out which path is the correct path to travel on. To explain what is going on in the animation see this image:

In the above image the red path is the correct path, going from inner to outer orbit the travel direction on the red path is clockwise, given that the green path is bowing into a smaller orbit, it is not the correct path to take.

In the below image the objects are in different locations! In this situation the green path is correct going from inner to outer orbit the travel direction on the green path is anti-clockwise, given that the red path is bowing into a smaller orbit, it is not the correct path to take.

My animation is here showing the paths calculated at the same time to show two possible choices at any given time: http://jsfiddle.net/Kz32b/

How can I work out which line is best between red and green. When the set of rules are the path must not go below the smallest orbit.

Edited by thefollower

##### Share on other sites

Find the midpoint of the segment that connects the origin and destination points. Project a ray from the center of the orbits through this midpoint. Find the intersection of your possible paths with the ray. The one which is further along the ray, i.e. further away from the center of the orbits, will be correct.

##### Share on other sites

Ah! You are correct here is the changes i added:

http://jsfiddle.net/FSYC5/

Slight follow up question for it if you see that link ^ notice there is a small margin of time when the orbit goes below the smaller orbit, is there any way I can correct that at all ?

Edited by thefollower

##### Share on other sites

Slight follow up question for it if you see that link ^ notice there is a small margin of time when the orbit goes below the smaller orbit, is there any way I can correct that at all ?

Your calculated path isn't always going to look good as just a choice between two circular segments with a fixed radius.

I doubt the path between orbits should be a circular segment, but I have no idea. I'd expect a good-looking path and a somewhat realistic path to not have any sharp angles transitioning into or out of an orbit.

Edit: I may not have put enough thought into this, and I've never used this HTML5 canvas thing before, but it seems like you might want to draw a quadratic curve.

Edited by Pink Horror

##### Share on other sites

I don't understand what you mean ?

##### Share on other sites

I don't understand what you mean ?

I'm not sure how to respond. I hope someone else can write something in a way you understand. Maybe I missed something in your code? Are your connecting paths not how I described them?

##### Share on other sites

If we're talking about orbits, the appropriate term for moving from one orbit to another is an "orbital transfer". If you google that term, and check out the idea behind the "Hohmann transfer orbit", then it should achieve what you want without relying on hardcoded circles. Also, check out Kerbal Space Program, which features quite accurate orbital mechanics as part of its gameplay.

##### Share on other sites
What you are trying to do is both wrong (your red and green circles have a completely arbitrary radius) and impossible: there are many point locations for which both paths cross the smaller orbit (in the demo, whenever the "currentAngle" difference between the squares falls in a certain interval around 180° that depends on arc radius). If you want a good looking path, use splines. You have a simple interpolation between two points, presumably with tangent constraints that you should make explicit. If you want an orbital transfer, use a physical simulation without computing an arbitrary "path". Hohmann transfers are certainly suitable, spirals without braking are also good, but in fact any sufficiently general control system can execute an orbit transfer (or similar tasks like reaching a point).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634078
• Total Posts
3015367
×