Graphing Calculators and Solving (Algebraically) Unsolvable Equations! | Orbital Mechanics in Roblox 2
Hey folks! Welcome back to the Orbital Mechanics series, where the development of a two-body patched conics orbital mechanics solver in Roblox is documented!
Last time, we introduced the project and talked about the basics of orbital mechanics from both a physics and computational lens. Be sure to check it out if you haven’t already; while not required, it does introduce a lot of terms and ideas we will be building upon in this blog.
Among the most important things we discussed was about the problem of orbital prediction. In order to meaningfully simulate spacecraft trajectories, we need some form of mathematical relation that allows us to calculate the position of a spacecraft along its orbit at any arbitrary point in time. Today, we’re going to discuss:
- The mathematical relationship behind orbital position as a function of time
- Why it can’t be solved using regular algebra
- How to solve it anyway, like a boss
and also
- Having excuses to make graphing calculators in Roblox.
Kepler’s Equation and Orbital Motion
The Elegance and Perfection of Circular Orbits
In high school physics, we learn about uniform circular motion. Uniform circular motion is a type of motion characterized by movement around a circle at a constant speed.
In circular orbits, a spacecraft’s distance from its parent planet never changes. This causes the force of gravity to remain constant, providing a constant centripetal acceleration that maintains the circular movement. These two facts together confirm that circular orbits exhibit uniform circular motion, and this observation allows us to make a lot of assumptions that make calculations simple.
Because the orbital motion is uniform circular motion, the math for representing position as a function of time only requires a bit of high-school level physics and math to derive. Here’s how it’s done; excuse my poor MS Paint skills!
As you can see, circles and circular motion is well-understood. However, it is not particularly useful on its own; these equations only hold true when an orbit’s eccentricity is exactly 0, which is an event that occurs with a probability of 0. There are many, many more orbits that are elliptical than are circular.
INTERACTIVE: From Circular to Elliptical Orbital Motion
Unlike circular orbits, doing any sort of math on an elliptical orbit is an entirely different beast. Unlike a circular orbit, in an elliptical orbit a spacecraft’s distance from its parent planet constantly changes. This causes the force from gravity to change as the spacecraft orbits the planet, in turn causing orbital velocity to constantly change. With so many changing variables, even advanced mathematics can fail to bring about an elegant solution.
When faced with complex situations and problems, it is often a good strategy to try to find a relationship between the complex situation and a simpler version of the problem. This is exactly what Johannes Kepler did when he was trying to model modern celestial mechanics for the first time in the early 17th century. Kepler modelled the motion of orbits by breaking down the problem into three related variables (also called ‘anomalies.’):
- The True Anomaly (f) is the angle of an object along its elliptical orbit, measured counterclockwise from the lowest point in the orbit.
- The Mean Anomaly (M) is the angle of an object along its orbit if that orbit was circular. Because we assume the orbit is circular, we can calculate the Mean Anomaly easily using a similar derivation to the one in the Circular Orbits section.
- The Eccentric Anomaly (E) is a special variable that relates the True Anomaly to the Eccentric Anomaly.
Eccentric Anomaly has a really involved definition that I won’t explain, but you can read about it here. All you need to know is that Eccentric Anomaly is related to the True Anomaly through the equation:
…and Eccentric Anomaly is related to Mean Anomaly via the equation…
…and this equation in particular is called Kepler’s Equation.
Kepler’s Equation: The Unsolvable Equation
Great! We have:
- Mean Anomaly, which is easy to calculate as a function of time, and we can use it to find the…
- Eccentric Anomaly, which we can then plug into a function to get the…
- True Anomaly, which is the position along the orbit.
All right, all we have to do is solve M = E-esin(E)
for E
! Easy, right? Right…?
Uh oh.
Turns out, Kepler’s equation cannot be solved for E. This is because Kepler’s Equation belongs to a family of functions called the Transcendentals, and they cannot be solved algebraically.
But, we know a solution should exist! It’s pretty easy to come up with examples that work.
We know the solution exists, but algebra isn’t enough to solve it. We’re going to need some bigger mathematical artillery to throw at this problem. And that artillery? Numerical Analysis.
Solution: Numerical Analysis and Root Finding Algorithms
Numerical analysis is an enormous field that deals with the study of algorithms to calculate accurate approximations to computationally hard or complex problems. Today, we’re going to take a very abbreviated look at a small subsection of numerical analysis, root-finding.
Root-finding Algorithms are algorithms that approximate the zeros of a given function. They do this without any algebraic manipulation of the function. If you have ever wondered how your calculator actually calculates the result of a square root or logarithm, the answer is that these functions are implemented using root-finding algorithms.
How can root-finding algorithms help us here? Well, if we rewrite Kepler’s Equation…
as the following by subtracting M from both sides…
…we get the above expression, whose root (or zero) occurs if and only if E-esin(E)
is equal to M
. Aint that convenient?
Newton-Raphson’s Method
There are many different algorithms for root-finding, but we will choose the Newton-Raphson Method because…
- Newton-Raphson’s Method converges quadratically. In plain English, it finds an accurate result fairly quickly in comparison to other algorithms.
- The derivative of Kepler’s Equation is known. Newton-Raphson is fast because it uses the derivative to “inform” its approximations.
- Because of lots of math done by very smart people™, we know that Newton’s Method should always give back a valid result for Kepler’s Equation for most eccentricities.
- It’s mentioned briefly in many intro-level calculus courses, but very few teachers and professors give a compelling reason for why students should know it. (This might be because teachers themselves don’t find it compelling) Hopefully, this problem will convince some calculus students that this is indeed a useful thing to know.
The algorithm for Newton-Raphson’s Method is as follows:
- Make a guess at some value
x1
that is vaguely close to the zero of the function. - Draw a tangent line from the function at
x1
down to the x-axis. - The intersection between that tangent line,
x2
, becomes - If the value of the function at
F(x2)
is accurate enough to please you, you acceptx2
as the answer. If not, you repeat step 2 and 3 to produce a new more accurate guess. You can continue repeating this process until the value ofx
is as accurate as required.
The below animation gives a good visual demonstration of these steps in action. The idea is that by drawing the tangent line, we “ride” the function to a better and better guess each time. In other words, we use the shape and behavior of the function to inform every subsequent guess. This is why it converges so quickly.
Putting it Together
So what does this look like when applied to orbital mechanics? Here’s how my interactive Desmos demo calculates the eccentric anomaly:
You can see that in our demo, we repeat the guess-refinement process five times. This produces accurate results for almost any eccentricity. Notice in this picture how the value “converges” towards 5.209...
. You can see that even after a few iterations, the improvement to approximation becomes negligible. That’s the power of Newton-Raphson’s Method!
To bring it into Roblox, it was just a matter of implementing the math using pure math operations and loops. The results are pretty neat!
And to think, it took pretty much a short math textbook to get here. Don’t worry, it wasn’t time wasted; we will use similar numeric approaches to tackle a variety of future problems. One might even say numerical analysis will become a crutch!
Building Desmos in Roblox to Visualize and Debug Numerical Algorithms
To close out this devblog, I thought I would share a bit of tooling I made to help in debugging. This part of the project required a lot of graphing; so much graphing, in fact, that it justified the creation of this simple graphing utility!
In addition to graphing basic functions, a built-in graphing utility is especially useful because numerical analysis often involves lots of iterative and repeated calculations that simply cannot be entered into a graphing calculator like Desmos. Before making this graphing utility, debugging these functions felt like trying to decipher a “black box” as it was really hard to observe their behavior using just print statements. Visuals can be much more intuitive than tables, and the results speak for themselves.
Demo: Sphere of Influence Intersection Detection Function
For example, consider this function (pos1(t) - pos2(t)).magnitude - certainDistance = 0
., which represents the distance between two objects on different orbits. This is kind of a sneak peek to the next devblog. We are trying to solve for the zero, which represents the time at which the two objects are exactly separated by a certain distance of one another.
As this function calls the orbital prediction function twice, and the orbital prediction function takes a total of 14 iterations on average to solve for the two positions, it would take a long time to graph this in an external graphing utility.
However, with our Roblox graphing utility, seeing if our graphs and numerical solvers are working as expected is a breeze! Also note the output window displaying the results of the numeric solver for this particular problem.
To Open Source or Not To Open Source…?
Is there any interest in a general release of this plotting library to the public? I was considering releasing it open source, but it will need a bit of refactoring to make it ready for a public release. If an open-source release of the plotting library interests you, please let me know in the comments! If there is enough interest, I’ll put in the time.
Next Time: Tracking Station!
Between these first two devblogs, we now have all the physics and mathematical foundations needed to make the rest of the game. Our next blog will be documenting the programming of the Tracking Station, a dynamic map that can be used to view the solar system and plot maneuvers for spacecraft.
Numerical Analysis turns out to be a very critical part of this next part of the project. First, We’ll need to write some intricate math code that will allow the rendering of orbits using Roblox beams. Next, we’ll implement a patched-conics solver to calculate changes to spacecraft trajectories if their orbits enter the gravity wells of other planets. Both of these projects will use our full arsenal of mathematical and engineering tools to their fullest extent!
For now, here’s a teaser of the conic section cubic bezier approximation!
Until then, we’ll see you next time!
Acknowledgements
-
My good friend, Math major, and official Project Ourbits Mathematician @DarkInfernoDrago has been invaluable in teaching me about the mathematics necessary to implement this. He also straight-up derived several core parts of the solver. Without him, this project would not be possible!
-
Shout out to everyone in the Computer Science Helpdesk channel for following the development of this project and being a sounding board for ideas.