# Best way to procedurally generate an orbit given mass and eccentricity

As stated here, I've badly expressed my problem and have made what we call a XY problem (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 @ConnorGarcia 's answer, 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?

• 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. Aug 19 at 12:18
• 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. Aug 19 at 12:23
• You appear to be conflating velocity and speed. Velocity is a vector. Speed is the magnitude of that velocity vector. Aug 19 at 12:25
• @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
Aug 19 at 13:38
• 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. Aug 19 at 17:33

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 ).

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.

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. Note that if $$i=\omega=\Omega=0$$ then all of the rotation matrices are the identity matrix so no rotations take place. • 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
Aug 20 at 8:36