# Calculating velocity state vector with orbital elements in 2D

I'm writing a little project that simulates an orbit by converting from initial state vectors $$\vec{r}$$ and $$\vec{v}$$ to Keplerian elements, then converting back to $$\vec{r}$$ and $$\vec{v}$$ from those Keplerian elements, but stepped one frame ahead. I'm having some trouble with that last part.

I've been using René Schwarz's conversion guide quite a lot, but the transformations and rotations at the end honestly just confuse me, so I ended up just using the True Anomaly and $$|\vec{r}|$$ to calculate the new $$\vec{r}$$, which does seem to work, but I'm stumped on how to calculate $$\vec{v}$$, I have the equation for the velocity at any point on an orbit with

$$v^2 = \mu \left(\frac{2}{|\vec{r}|}-\frac{1}{a}\right)$$

Where I just plug in the new $$|\vec{r}|$$ and I get $$|\vec{v}|$$, but trying to use this equation for the tangent vector to any point on an ellipse given the angle, $$-a \cos\theta i + b \cos\theta j$$

Where $$a$$ is the semi major axis, $$b$$ is the semi minor axis, and $$\theta$$ is the angle around the ellipse (I've been using the True Anomaly as input for that angle, is that correct?) doesn't seem to work to find the vector tangent to the ellipse. So my questions are:

• How do I find the vector tangent to a 2D orbital ellipse given $$e$$ and $$a$$, as well as $$|\vec{r}|$$, $$|\vec{v}|$$, and the True, Eccentric, and Mean anomaly at that point? (I also have some additional elements that I haven't used yet such as the argument of periapsis if that's useful.)

• How do I determine which direction that vector should face (so it doesn't point toward retrograde when it should point towards prograde) given the same values as above?

• +1 for a great rewrite! :) In this case there might be an existing answer here that answers your question. I know there have been at least a few somewhat similar questions. If your question ends up being marked as a duplicate by pointing to an existing answer, don't think of it as a bad thing. It's just stackexchange's way of making sure future readers are guided to fewer, but higher quality answers. If it doesn't help, please say so, and remember you can ask as many more (quality) follow-up as well as totally new questions as you like!
– uhoh
Commented Jul 4, 2017 at 4:48
• – uhoh
Commented Jul 4, 2017 at 5:04
• There is (at least) one more answer somewhere that I can't find yet that may also be helpful. Also, one of the two documents linked in this answer may be helpful as well.
– uhoh
Commented Jul 4, 2017 at 5:06
• I've found most of those before, but no.4 looks helpful, thanks. Commented Jul 4, 2017 at 5:37
• I'm not sure if I'm implementing something wrong or not; using the transformations in no.4 doesn't produce coherent results, I'm using the argument of periapsis as $\omega =\arctan 2({e_{y}},{e_{x}})$ and setting inclination and longitude of the ascending node to 0 (Wikipedia says that's what the convention is on it's argument of periapsis page). Commented Jul 4, 2017 at 6:08

Assuming that the direction of the periapsis is the positive direction of the x-axis, then the vector $-a\sin E\cdot \mathbb{i} + b\cos E\cdot \mathbb{j}$ is tangent to the orbit at the point with the eccentric anomaly $E$. The velocity is codirectional with this vector if the orbit goes counter-clockwise, and contradirectional if the orbit goes clockwise.

If the angle between the positive direction of the x-axis and the direction of the periapsis is $\omega$ (measured counter-clockwise), then you need to rotate the vector by this angle, and the result is $(-a\sin E\cos \omega - b\cos E\sin \omega)\mathbb{i} + (b\cos E\cos\omega - a\sin E\sin\omega)\mathbb{j}$.

• For the y component of that last equation you're giving me the output goes between -a and a, does that mean I'm calculating the argument of periapsis wrong? I used this equation from wikipedia: $\omega =\arctan 2({e_{y}},{e_{x}})$. The x value also ranges from some really low value to another. Commented Jul 4, 2017 at 11:01
• Are $e_x$ and $e_y$ the coordinates of the periapsis relative to the main focus? Then it's correct. But I don't why the $y$-component of the last expression would change between $-a$ and $a$. If you fix $\omega$ and vary $E$, then it should change between $-\sqrt{(a\sin\omega)^2+(b\cos\omega)^2}$ and $\sqrt{(a\sin\omega)^2+(b\cos\omega)^2}$. Commented Jul 4, 2017 at 11:18
• Fixed those problems, just random bugs that appeared because of my constant tweaking. The result still doesn't look right. I'm starting with a state vector of (0,-350) (m/s) and (0,~200) (km) from my main mass, so the velocity shouldn't have a magnitude above 350 (periapsis is where I start), but using $\vec{v} = |\vec{v}| \cos(t)i + |\vec{v}| \sin(t)j$ where $t$ is the heading of the vector you gave has magnitude that ranges from 0 to 700. Looking at the numbers, it makes $\vec{v}_x$ oscillate oppositely to $\vec{v}_y$, like one is at apoapsis while the other is at periapsis. Commented Jul 4, 2017 at 19:46
• Also, my coding library has a vector rotation function, using that on $-a\sin E\cdot \mathbb{i} + b\cos E\cdot \mathbb{j}$ should work, correct? From what I can tell from the source, it rotates by converting the vector to an angle, adding the input angle, then converting back to a vector, is that an equivalent rotation? Commented Jul 4, 2017 at 20:00
• "I'm starting with a state vector of (0,-350) (m/s) and (0,~200) (km) from my main mass": does the starting velocity point toward the main mass? Then it's not a periapsis, About the rotation: yes, the way you describe should work. Commented Jul 4, 2017 at 21:11

To calculate the velocity vector in a two-body problem, you can use the flight path angle $\phi_{fpa}$ (from Fundamental of Astrodynamics and Applications, D. Vallado). $\phi_{fpa}$ is the angle measured from the local horizon to the velocity vector. For this end, calculate the angular momentum, which is constant: $$\vec{h} = \vec{r}_0\times \vec{v}_0$$

The position vector norm $||r||$ is: $$||r|| = \frac{a\sqrt{1-e^2}}{1+e\cos{f}}$$ where $f$ is the true anomaly. As you previously showed, the velocity norm as a function of $r$ is $$v^2 = \mu \left(\frac{2}{||r||}-\frac{1}{a} \right)$$

So, $\phi_{fpa}$ is calculated is as such: $$\cos{\phi_{fpa}} = \frac{||h||}{||r||||v||}$$ $$\sin{\phi_{fpa}} = \frac{||h||}{||r||||v||}\times\frac{e\sin{f}}{1 + e\cos{f}}$$ $$\phi_{fpa} = \text{atan2}(\sin{\phi_{fpa}}, \cos{\phi_{fpa}})$$

Before I show you the velocity vector expression, we need to define the following direction vectors:

Where $\hat{r}$ is the direction of $\vec{r}$, $\hat{t}$ is in the velocity direction, and $\hat{n}$ is normal to the orbit and to $\hat{t}$. Furthermore, the directions $\hat{r}$ and $\hat{s}$ are perpendicular. Thus, the velocity vector $\vec{v}$ can be expressed as: $$\vec{v} = ||v||\hat{t} = ||v||(\hat{s}\cos{\phi_{fpa}} + \hat{r}\sin{\phi_{fpa}})$$

The calculation of $\vec{r}$ and $\hat{s}$ as a function of some inertial direction is an easy task, using the true anomaly angle $f$.

• I'll try this one out tomorrow, but it looks like you have a little typo at first glance, when you show $\vec{v} = v\hat{t}$ do you mean that $\vec{v} = |\vec{v}|\cos(\hat{t})i + |\vec{v}|\sin(\hat{t})k$ ? Commented Jul 4, 2017 at 11:28
• No. $\hat{t}$ is a unit vector, not a scalar. So, calculating its sine or cosine has no meaning. $v = ||v||$ is the norm of $\vec{v}$ and $\hat{t}$ is a unit vector in $\vec{v}$ direction. Commented Jul 4, 2017 at 11:35