# Converting Cartesian vectors to Keplerian Orbital elements - having problems

I have my position and velocity vectors, and also the mass and position of the sun. I need to convert my vectors to the Keplerian Orbit elements, but I can't get a definite number when I do. The orbit isn't changing, yet the 'constant' values I'm calculating are.

I've been reading this and others such as this.

I first calculate the angular momentum with $\vec{h} = \vec{r} \times \vec{v}$. This value changes as the planet goes around the orbit.

Then I calculate the eccentricity with $$\vec{e} = \frac{(v^2-\mu /r)\vec{r}-(\vec{r} \cdot \vec{v})\vec{v}}{\mu}$$

But this changes between 0.9 and 1.1 as the planet orbits, which as far as I'm aware, shouldn't happen. What is going wrong? Perhaps I'm missing something, but surely the calculations I have say that the orbit changes from an ellipse to a hyperbola, and back again.

Any help would be appreciated, thanks.

• The specific angular momentum vector, the eccentricity vector, and the specific energy (you didn't mention that last one) are all constants of motion. They do not change over the orbit. What are you doing to get the $\vec{r}$ and $\vec{v}$ values around the orbit? – Mark Adler Jul 9 '15 at 14:51
• Are you using a barycentric frame of reference? – fibonatic Jul 9 '15 at 14:52
• To get the the r and v vectors, I'm just taking them from the object (using rigidbody.position, and rigibody.velocity). My velocity vector is changing as I'm applying force, in the direction vector x (which directs towards the sun). Does that affect why this is changing? And I'm not sure about the barycentric frame of reference, I'm just using a grid coordinate system (If that's what you mean) – Jacob Morris Jul 9 '15 at 15:05
• Barycentric frame of reference means that the origin of the coordinate system lies at the center of mass, therefore momentum of all bodies is zero, such that the center of mass does not move away from the origin of the coordinate system. – fibonatic Jul 9 '15 at 15:21
• "My velocity vector is changing as I'm applying force, in the direction vector x (which directs towards the sun)" x is always towards the Sun? Is your force proportional to the inverse square of the distance from the Sun? It sounds like you're implementing the physics yourself, so you need to make sure that it's correct. – Mark Adler Jul 10 '15 at 1:31