Original idea by elplanetaerrante.
First prototype
Seccond prototype
Current version
- Change the color of the barycenter to better differentiate it from the stars.
- Add mention to elplanetaerrante
- Add tool tips with explanation on what is each variable.
- Add a bit of space on top.
- Make sliders bigger for mobile.
- Change github link to repo link.
- Fix orbital period
The process of simulating the two-body problem in real time with the mass ratio, eccentricity, and semi-major axis as parameters was not an easy task. Fortunately, I was able to come up with a solution that works well enough to give an accurate representation of the orbits in real time.
The orbit of the first planet is created based on it’s eccentricity and semi-major axis. It’s right focus is centered in the canvas. This focus will become the barycenter later.
While the orbit itself is easy to create, ensuring that the planet moves at the correct speed at each point in time is not. For this, we need the function
A line that connects a planet to the sun sweeps out equal areas in equal times.
Adapting it to a two-body problem where the center is not the sun but the barycenter. With this, we know that
Since we know the position based on an angle:
If we can get the angle based on an area ($\theta(A)$) then we’ll have
To calculate this we first can get
Since getting the inverse of
With
With this we can calculate the position of the first planet for any given area swept. Now we just animate the area going from 0% of the ellipse to 100% in a linear manner, and we get the corresponding movement of the planet.
With the position of the first planet, we can get the position of the second planet by simply using the fact that the center of mass of both will stay at the barycenter.
Thanks to this, we can know the position of the two planets at any time based on the semi-major axis and the eccentricity. The mass ratio between the planets comes into play because it dictates the semi-major axis of the orbit used to calculate the orbital period.