# Determinstic implementation Sphere of Influence change using Patched conic approximation

wasn't 100% sure which StackExchange to ask this on, as it could be related to Gamedev (as it's for a game), maths (as it's a approximation model rather than a physical description) or StackOverflow (as I'm implementing it in a programming language, lua to be specific), so sorry if this is the incorrect place.

So a short description, I'm working on a game with a patched conic approximation of orbits ala Kerbal Space Program.

I've got a simple keplerian orbital simulation going on, calculating the orbital elements from my position and velocity vectors and drawing my orbit by calculating radius as a function of true anomaly (side question: is there a better way to do this? I've noticed this method leads to distortion in some orbits, usually elliptical ones with a very high eccentricity)

So, I really have two questions, which are fairly closely related.

Is there a deterministic, preferably without iteration, method of calculating when and where an orbit will go above or below a certain altitude? I'm doing this currently using binary intersection using the apsis' as starting values. I'm sure there's a better and more accurate way, and importantly faster way. The value I'm using to determine the "position" along the orbit is the true anomaly, if there's a better variable to use I'd like to know that too!

Is there a way to determine the true anomaly (or superior equivalent) when it intercepts a target with a known sphere of influence? The target and orbit will always be around the same body (And the SoI of the currently orbited object will always be "higher" than the maximum sphere of influence at the Apoapsis of the intercepted object) and the target will never leave the currently orbited objects sphere of influence either.

I was trying to solve a similar problem, and did, with help on StackOverflow. My question was here: https://stackoverflow.com/questions/16501182/find-first-root-of-a-black-box-function-or-any-negative-value-of-same-function

I asked it more abstractly. The way I saw it, you have a ship and moon (for example), and for different values of time they have a changing distance between them. If the ship is destined to encounter the sphere of influence of the moon, at some point the distance between the ship and the moon, minus the radius of the moons SoI, will be negative, or exactly zero at the point it actually first encounters the SoI. So if we have a function that gets the distance between them minus SoI radius which takes time as a parameter, we need to find the first root of that function, ie the first point that function equals zero.

To find the time that results in the encounter you need a root solver. You might be using the Newton-Raphson root solver to solve keplers equation, but as that needs the derivative of the function to solve, it wasn't usable for me here. Also bisection methods wouldn't work as you need a value of time either side of the actual root.

The answer on StackOverflow sounds/is complex, but it works. It basically makes an approximation of the function, and then uses eigenvalues to solve for the root. And it works, pretty much perfectly for me so far. Kerbal Space Program do something along the same idea, though they use a different root solver and probably other differences, but it comes down to the same principle. Find the time where f(time)=0.

Regards you're first question, about when an orbit goes above or below a certain altitude, that can be found using conic intersections, which for two orbits around the same central body, can be solved with this: http://orbiter-forum.com/showthread.php?p=192617&postcount=6 nice and analytical. To find when it goes above a certain altitude just intersect the orbit with a circle. I use that process a lot as part of a preliminary candidate check on my ship and moons to see if an encounter is possible before doing the relatively expensive encounter prediction root solving stuff.

For a Keplerian orbit you can find the true anomaly by just use the inverse function of the radius as a function of the true anomaly, $$r(\theta)=\frac{a(1-e^2)}{1+e\cos{\theta}},$$ so, $$\theta(r)=\cos^{-1}\left(\frac{a(1-e^2)-r}{er}\right).$$ To determine the when can also be solved analytically, $$t(r)=\sqrt{\frac{a^3}{\mu}}\left(2\tan^{-1}{\sqrt{\frac{r-a(1-e)}{a(1+e)-r}}}-\sqrt{e^2-\left(\frac{r}{a}-1\right)^2}\right),$$ where $t$ is equal to the time since periapsis passage.

However there will be no deterministic answer to your second answer, unless both orbits are exact circular ($e=0$). So you probably want to use some kind of analytical solver like pagnatious mentioned.