I'm working on a game, that is set in space. The game is about orbits and my problem is the following:
I want to be able to draw a predicted orbit for my satellite. Currently, my satellite orbits the host planet. What I have available from the physics simulator is the satellite's velocity, its position and the pulling force from the planet.
As I have researched, I understand that I should be able to calculate the ellipse of the orbit and a starting point could be to first calculate the semi major axis of the ellipse using the total energy equation (taken from Calculating specific orbital energy, semi-major axis, and orbital period of an orbiting body):
\begin{align} E &= \frac{1}{2}v^2 - \frac{\mu}{r}= -\frac{\mu}{2a}, \end{align}
Which can be rewritten to \begin{align} a &= - \frac{\mu r}{r v^2 - 2 \mu } \end{align}
Where $a$ is the semi major axis, $\mu = G(M+m)$, $v$ is the velocity and $M$, $m$ are the bigger and smaller masses, respectively. Using a simulation with two bodies, a planet (with mass 9999999999) and a satellite (with mass 1) placed 10 units from the planet, and a circular orbit, I expect the semi major axis to be 10 but what I get is a negative number.
P.S I want the planet to be the primary.
Any hints of what might be wrong?