<p><strong>ANSWER</strong></p>
<p>I would put the massive object at the origin.  Then, I would calculate the velocity at periapsis, when <span class="math-container">$r=a(1-e)$</span> as <span class="math-container">$$v = \sqrt{GM\frac{1}{a}\left(\frac{1+e}{1-e}\right)}$$</span> (stolen from Uhoh's answer to <a href="https://astronomy.stackexchange.com/questions/34041/calculating-object-velocity-at-perihelion?rq=1">Calculating object velocity at perihelion</a> ).</p>
<p>Put the periapsis on the positive y axis.  Then the velocity vector at periapsis is <span class="math-container">$\vec{v}=[v,0]$</span>.  If you want the periapsis at a counter-clockwise angle <span class="math-container">$\omega$</span> from the positive y-axis, then multiply by your <a href="https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions" rel="nofollow noreferrer">standard angle rotation matrix</a>.</p>
<p><strong>EXAMPLE</strong></p>
<p>Let the mass of the central body be twice a solar mass <span class="math-container">$m=2M_{\odot}$</span> with a planet with semi-major axis <span class="math-container">$a=1$</span> AU and highly eccentric with <span class="math-container">$e=0.5$</span>.  Then periapsis is at <span class="math-container">$r=0.5$</span> AU.</p>
<p>Here, the gravitational constant is <span class="math-container">$$G \approx 887.352 \frac{AU}{M_{\odot}}(km/s)^2$$</span>
Plugging these values into Uhoh's equation for velocity above, we get <span class="math-container">$|\vec{v}| \approx 72.97$</span> km/s.  So, when the orbiting body's position at periapsis is at <span class="math-container">$\vec{p} = [0,0.5]$</span> AU, its velocity is <span class="math-container">$\vec{v} = [72.97,0]$</span> km/s.</p>
<p><strong>Want to do this in 3D?</strong></p>
<p>Then <span class="math-container">$\vec{p} = [0,0.5,0]$</span>, <span class="math-container">$\vec{v} = [72.97,0,0]$</span>, with an argument of periapsis <span class="math-container">$\omega$</span>, inclination <span class="math-container">$i$</span>, and longitude of ascending node <span class="math-container">$\Omega$</span>, then we can calculate new position and velocity vectors as <span class="math-container">$$\vec{p}' = R_z(\Omega)R_x(i)R_z(\omega)\vec{p}$$</span> and <span class="math-container">$$\vec{v}' = R_z(\Omega)R_x(i)R_z(\omega)\vec{v}$$</span> where <span class="math-container">$R_x(\theta)$</span> and <span class="math-container">$R_z(\theta)$</span> are <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations" rel="nofollow noreferrer">the 3D rotation matrices around the x and z axes respectively</a>.  Note that if <span class="math-container">$i=\omega=\Omega=0$</span> then all of the rotation matrices are the identity matrix so no rotations take place.</p>
<p><a href="https://i.sstatic.net/bUbnj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bUbnj.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2021-08-20T08:36:03.910Z
Aaaah, yes, this is perfect, thank you! It took me some time to understand because I'm a rookie, but it's crystal clear.
Eol
CC BY-SA 4.0
2021-08-19T17:02:41.567Z
2021-08-19T17:55:34.750Z
2021-08-19T17:55:34.750Z
Connor Garcia
<p><em>As stated <a href="https://astronomy.stackexchange.com/questions/45379/given-mass-position-and-eccentricity-is-there-a-way-to-get-initial-velocity-in#comment99213_45379">here</a>, I've badly expressed my problem and have made what we call a <a href="https://xyproblem.info" rel="nofollow noreferrer">XY problem</a> (the question was well answered nonetheless) so I restate the question. Sorry for the inconvenience</em></p>
<p>I have a 2D orbit simulator using Euler-Cromer method for now;
What I'm trying to do is to generate a central body with a given mass
and a body orbiting this body with the orbit having:</p>
<ul>
<li>a procedurally generated eccentricity ranged in <span class="math-container">$\mathbf{0≤e&lt;1}$</span></li>
<li>a procedurally generated semi-major axis</li>
</ul>
<p>What I'm trying to achieve is to get the initial velocity for the simulator to make any orbit that correspond this mass, eccentricity and semi-major axis - no matter the plane of the orbit nor direction. For simplification, let's state everything appends on the same plane and goes clockwise.</p>
<p>So far, thanks to @ConnorGarcia 's <a href="https://astronomy.stackexchange.com/a/45383/42546">answer</a>, I may guess that I can:</p>
<ul>
<li>use the eccentricity to find <span class="math-container">$\mathbf{c}$</span> - the distance between the focal and the center of the ellipse - like that: <span class="math-container">$\mathbf{c = a \cdot e}$</span></li>
<li>place the body at any random distance to the orbited body equal to the distance between one focal and apogee using <span class="math-container">$\mathbf{a + c}$</span></li>
<li>use the vis-viva equation to have speed, and</li>
<li>set velocity vector using speed as magnitude and making its direction perpendicular to the semi-major axis.</li>
</ul>
<p>Does it feels right? If I was building this generator in 3D, should I just have to add the plane to make it correct?</p>

CC BY-SA 4.0
2021-08-19T12:18:19.603Z
The mass of the orbiting body is irrelevant unless the mass of that orbiting body is a significant fraction of the mass of the central body.
David Hammen
CC BY-SA 4.0
2021-08-19T12:23:04.323Z
You still have an X-Y problem because you have not specified the size of the orbit, you have not specified the orbital plane, and in the case of a non-circular orbit, you have not specified the orientation of the ellipse. An orbit has at least six degrees of freedom, and at least seven if mass is a factor.
David Hammen
CC BY-SA 4.0
2021-08-19T12:25:48.513Z
You appear to be conflating velocity and speed. Velocity is a vector. Speed is the magnitude of that velocity vector.
David Hammen
CC BY-SA 4.0
2021-08-19T13:38:32.753Z
@DavidHammen I tried to edit but this is hard to explain. I'm not trying to get "the" orbit here but to forge any that match the parameters. If you see any missing parameter to achieve that, feel free to tell me; the direction of the orbit and the plan can be procedurally generated allong with the other parameters. Aside from that, do you feel like my approach is good?
Also, thank you for your comments!
Eol
CC BY-SA 4.0
2021-08-19T13:45:26.510Z
If there's still degrees of freedom, it can be not an issue; if I can pick one at random and it gives me a good intial velocity to make a realistic orbit corresponding to the eccentricity, then it's ok.
Eol
CC BY-SA 4.0
2021-08-19T17:33:51.690Z
For non-circular elliptical orbits, the only time the velocity vector is perpendicular to the position vector is at periapsis and apoapsis.  Hence my below answer.
Connor Garcia
<orbit><n-body-simulations><eccentricity>
Best way to procedurally generate an orbit given mass and eccentricity
Eol
# Best way to procedurally generate an orbit given mass and eccentricity

(Asked by: username_0 on 2021-08-19)

*As stated [here](https://astronomy.stackexchange.com/questions/45379/given-mass-position-and-eccentricity-is-there-a-way-to-get-initial-velocity-in#comment99213_45379), I've badly expressed my problem and have made what we call a [XY problem](https://xyproblem.info) (the question was well answered nonetheless) so I restate the question. Sorry for the inconvenience*

I have a 2D orbit simulator using Euler-Cromer method for now;
What I'm trying to do is to generate a central body with a given mass
and a body orbiting this body with the orbit having:

* a procedurally generated eccentricity ranged in $\mathbf{0≤e<1}$
* a procedurally generated semi-major axis

What I'm trying to achieve is to get the initial velocity for the simulator to make any orbit that correspond this mass, eccentricity and semi-major axis - no matter the plane of the orbit nor direction. For simplification, let's state everything appends on the same plane and goes clockwise.

So far, thanks to @username_2 's [answer](https://astronomy.stackexchange.com/a/45383/42546), I may guess that I can:

* use the eccentricity to find $\mathbf{c}$ - the distance between the focal and the center of the ellipse - like that: $\mathbf{c = a \cdot e}$
* place the body at any random distance to the orbited body equal to the distance between one focal and apogee using $\mathbf{a + c}$
* use the vis-viva equation to have speed, and
* set velocity vector using speed as magnitude and making its direction perpendicular to the semi-major axis.

Does it feels right? If I was building this generator in 3D, should I just have to add the plane to make it correct?

**Tags:** orbit, n-body-simulations, eccentricity

> **Comments:**
> **username_1**: The mass of the orbiting body is irrelevant unless the mass of that orbiting body is a significant fraction of the mass of the central body.  
> **username_1**: You still have an X-Y problem because you have not specified the size of the orbit, you have not specified the orbital plane, and in the case of a non-circular orbit, you have not specified the orientation of the ellipse. An orbit has at least six degrees of freedom, and at least seven if mass is a factor.  
> **username_1**: You appear to be conflating velocity and speed. Velocity is a vector. Speed is the magnitude of that velocity vector.  
> **username_0**: @username_1 I tried to edit but this is hard to explain. I'm not trying to get "the" orbit here but to forge any that match the parameters. If you see any missing parameter to achieve that, feel free to tell me; the direction of the orbit and the plan can be procedurally generated allong with the other parameters. Aside from that, do you feel like my approach is good? Also, thank you for your comments!  
> **username_0**: If there's still degrees of freedom, it can be not an issue; if I can pick one at random and it gives me a good intial velocity to make a realistic orbit corresponding to the eccentricity, then it's ok.  
> **username_2**: For non-circular elliptical orbits, the only time the velocity vector is perpendicular to the position vector is at periapsis and apoapsis.  Hence my below answer.  

---

## 1 answers

### Answer by username_2 on 2021-08-19. Score: 2 | Accepted answer

**ANSWER**

I would put the massive object at the origin. Then, I would calculate the velocity at periapsis, when $r=a(1-e)$ as $$v = \sqrt{GM\frac{1}{a}\left(\frac{1+e}{1-e}\right)}$$ (stolen from Uhoh's answer to [Calculating object velocity at perihelion](https://astronomy.stackexchange.com/questions/34041/calculating-object-velocity-at-perihelion?rq=1) ).

Put the periapsis on the positive y axis. Then the velocity vector at periapsis is $\vec{v}=[v,0]$. If you want the periapsis at a counter-clockwise angle $\omega$ from the positive y-axis, then multiply by your [standard angle rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions).

**EXAMPLE**

Let the mass of the central body be twice a solar mass $m=2M\_{\odot}$ with a planet with semi-major axis $a=1$ AU and highly eccentric with $e=0.5$. Then periapsis is at $r=0.5$ AU.

Here, the gravitational constant is $$G \approx 887.352 \frac{AU}{M\_{\odot}}(km/s)^2$$
Plugging these values into Uhoh's equation for velocity above, we get $|\vec{v}| \approx 72.97$ km/s. So, when the orbiting body's position at periapsis is at $\vec{p} = [0,0.5]$ AU, its velocity is $\vec{v} = [72.97,0]$ km/s.

**Want to do this in 3D?**

Then $\vec{p} = [0,0.5,0]$, $\vec{v} = [72.97,0,0]$, with an argument of periapsis $\omega$, inclination $i$, and longitude of ascending node $\Omega$, then we can calculate new position and velocity vectors as $$\vec{p}' = R\_z(\Omega)R\_x(i)R\_z(\omega)\vec{p}$$ and $$\vec{v}' = R\_z(\Omega)R\_x(i)R\_z(\omega)\vec{v}$$ where $R\_x(\theta)$ and $R\_z(\theta)$ are [the 3D rotation matrices around the x and z axes respectively](https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations). Note that if $i=\omega=\Omega=0$ then all of the rotation matrices are the identity matrix so no rotations take place.

[![enter image description here](https://i.sstatic.net/bUbnj.png)](https://i.sstatic.net/bUbnj.png)

> **Comments:**
> **username_0**: Aaaah, yes, this is perfect, thank you! It took me some time to understand because I'm a rookie, but it's crystal clear.  

---

