I am working on some basic physics simulation for a game and need to simulate gravity. I have a system working that is behaving more or less correctly so far, but I want to see if I can send a projectile into a stable orbit by giving it an initial velocity.

I understand that the following equation expresses the velocity of a stable orbit around a mass,

$$v = \sqrt{\frac{GM}{r}}$$

I am able to use this equation to create a stable orbit but not from setting a starting velocity and then letting the gravity do the work. It only works if I calculate the {x, y} components of v and set that vector as the projectiles velocity each time I render the scene. The direction of the vector is always calculated to be perpendicular in direction to the line representing the gravitational force towards the center of the mass.

So my question is essentially whether this equation is even intended for finding an appropriate initial velocity, and if not, is there another way? I am not clear on whether there is any meaningful distinction between calculating v as a stable orbital velocity versus v as an initial velocity that resolves into a stable orbit.

Or alternatively might it be more appropriate to say that I need to give the projectile and initial force or acceleration that resolves into a stable orbit (like an escape velocity, sort of).


EDIT -- here is how I calculate the gravitational acceleration vector at a certain point. Let (x, y) be the point of interest, (x1, y1) be the center of mass, dx be the x-component acceleration, and dy be the y-component acceleration:

        1) seperation of the points

        sep_x = x1 - x;
        sep_y = y1 - y;

        2) effect of gravity given distance between points

        gravity = (G * mass) / radialDistance ^ 2

        3) break down into components

        dx = (gravity * sep_x) / radialDistance ^ 3
        dy = (gravity * sep_y) / radialDistance ^ 3
  • $\begingroup$ What are you imagining as the initial position? Are you working with an object being launched into orbit? or something falling into orbit? $\endgroup$ – AdamRedwine Oct 12 '11 at 18:04
  • $\begingroup$ More likely falling into orbit. I'd like to be able to take any initial position and calculate the necessary starting velocity as a vector {x, y}. My suspicion is that that is an entirely different calculation that just using the equation above, but I'm not sure. $\endgroup$ – Sean Thoman Oct 12 '11 at 18:17
  • $\begingroup$ Part of your problem might be that there isn't a vector that will result in stable orbit, there are an infinite number of them. It might be possible to determine inequalities that will result in a stable orbit, but unless you specify the orbit more exactly, that's probably the best you can do. $\endgroup$ – AdamRedwine Oct 12 '11 at 18:49
  • $\begingroup$ I see. Take the example of a satellite -- it requires a force provided by thrusters in order to remain in orbit, doesn't it? It seems I should add a function to apply force to the projectile, then calculate the force needed to keep it at stable orbital velocity..which would be realistic, I think. $\endgroup$ – Sean Thoman Oct 12 '11 at 18:57
  • $\begingroup$ Yes, satellites use thrusters to remain in orbit. In the short term they are used for keeping the satellite where it is "supposed" to be and in the long term they are to counteract the cumulative drag of air friction. In a ideal system, a stable orbit does not require any applied force. If you gave more details about what you include in your model, it would help. $\endgroup$ – AdamRedwine Oct 12 '11 at 19:29

Satellite will remain on stable orbit if its initial speed is less than $v_{crit}=\sqrt{2GM/r}$ unless orbit pericenter is too low in which case satellite will crash into central body. In particular if satellite speed is $\sqrt{GM/r}$ and perpendicular to $\vec{r}$ it will remain on stable circular orbit.

It's very easy to see that from energy balance. Let write down kinetic and potential energy of satellite.

$$U = -\frac{GMm}{r}; \qquad T = \frac{1}{2}mv^2 $$

If total energy of satellite is negative it can't go to the infinity and is confined to neighbourhood of central body and will reside on elliptical orbit. If total energy is positive it will fly away.

I suspect that you solve equations of motion wrongly. For any initial coordinate and speed satellite will either circle on elliptic (or circular orbit) or fly away on hyperbolic orbit.

Solve equations

In order to calculate trajectory of satellite your need to solve motion equations. Analytical solution exists for two body case. But if you want to add more more massive bodies or take into account thrusters or athmospheric drag you'll have to use numerical integration. Let write them down. I wrote them in form suitable for numerical integration.

$$ \frac{d\vec x}{dt} = \vec v; \qquad \frac{d\vec v}{dt} = \vec a = -\frac{GM}{r^3}\vec{r} + \vec{F}_{whatever}/m$$

Solution must also satisfy initial conditions

$$ \vec v(t_0) = \vec v_0; \qquad \vec x(t_0) =x_0 $$

Here is very simple rule for numeric integration. $\Delta t$ is time step for integration and $\vec{a}(t_i)$ is acceleration at i'th moment of time.

$$ \vec{x}_{i+1} = \vec{x}_i + \vec{v}_i \Delta t$$ $$ \vec{v}_{i+1} = \vec{v}_i + \vec{a}(t_i) \Delta t$$

This particular method is poor choice for simulation. There much better methods, for example Runge-Kutta.

  • $\begingroup$ Since the initial velocity has to be directed perpendicular to the line pointing towards the mass, I have to re-calculate the correct direction each time. In a computer simulation setting, every velocity has to be expressed as an {x, y} vector otherwise its meaningless -- that is how I tell the particle where to move next. I believe my simulation is correct otherwise, because when I set the velocity to the result of Vcrit each time it maintains a perfect orbit. The value of vis the velocity required <i>after</i> factoring in gravity, not the value necessary <i>in spite of</i> gravity, is it? $\endgroup$ – Sean Thoman Oct 12 '11 at 19:57
  • $\begingroup$ At this point I don't really understand what do you try to accomplish. If you want to just set initial conditions and let gravity do the work you have to solve equations of motions. Either numerically or use exact solutions for two-body case. To be correct simulation must be able to reproduce not only circular but elliptic and hyperbolic orbits as well. $\endgroup$ – Shimuuar Oct 12 '11 at 20:20
  • $\begingroup$ I want to set initial conditions and let gravity do the work, but more importantly for the simulation to be realistic. My question is whether the aforementioned equation is really the correct one to achieve that. I presume that if were talking about a small projectile or something of a satellites size, 'initial conditions' have to include more than just a starting velocity and also an initial force or constant adjustments from some force such as thrusters? Also, in my model I am assuming that the satellites / projectiles mass is negligible as compared to the star or planet it is orbiting. $\endgroup$ – Sean Thoman Oct 12 '11 at 20:27
  • 1
    $\begingroup$ Initial position and initial speed completely determine trajectory of satellite (ignoring thrusters' firing) $\endgroup$ – Shimuuar Oct 12 '11 at 20:42
  • $\begingroup$ Right, but if I want to maintain a stable orbit (at relatively close range) -- wouldn't I need thrust? It may be that my model is not scaled in a way where I can see a stable orbit for a small satellite of negligible mass without any external forces. Is that possible? Or should it be possible to have a stable orbit without external forces at any distance? $\endgroup$ – Sean Thoman Oct 12 '11 at 21:03

On reading the many comments I have several concerns

  • On the matter of how gravity and orbits work

    I'm a little bit concerned by the use of the phrase "falling into orbit", which is convenient but philosophically dicey. A body on a parabolic or hyperbolic approach (i.e. literally falling in) does not obtain a closed path (i.e. orbit) without thrust or the intervention of a outside force (e.g. gravitational perturbations from a third body). In that sense there is no difference between the initial velocity in a particular orbit and the evolving velocity on that same orbit.

    Injecting bodies into orbit is a important problem, but I don't get the sense that this is what Sean is up to.

  • On the matter of what is meant by a vector

    It is not true that vector must be expressed as $(x, y)$ to be meaningful, $(V, \phi)$ is just as meaningful. It is generally more convenient to use Cartesian representations in a simulation, but that is not fundamental

  • On the matter of how simulations work

    If you are simulating gravity in the naive way (and that is how you should do it until you are comfortable with simulation), then you should not be recalculating the orbit from $$ v= \sqrt{\frac{GM}{r} }$$ on each iteration. You should be updating the behavior of each body in the simulation with Newton's second law and universal gravitation (albeit in a discretized form).

    Orbits turn out to be a tricky cases, so I would suggest that you alternate between updating positions on one step and velocities on the next (it's well known that this leap-frog approach preserves orbital energy when other naive methods do not). $$ v_{x,n+1} = v_{x,n-1} + \frac{F_{x,n}}{m}\Delta t, v_{y,n+1} = v_{y,n-1} + \frac{F_{y,n}}{m}\Delta tt $$ and $$ x_{n+2} = x_n + v_{x,n+1} \Delta t, y_{n+2} = y_n + v_{y,n+1} \Delta t $$ where the $F$'s come from Newton's Law of Gravitation and the $n$'s represent the steps of you simulation. The $\Delta t$s here represent the time between step $n$ and step $n+2$ as I've numbered them. (You are free to eliminate the $F/m$ but by finding $a$ directly.)

  • $\begingroup$ Thanks. I think I am simulating the way you stated (the naive way) -- by updating the behavior of each body dynamically. The thing is that when I set the starting velocity of my projectile based on v = sqrt(GM/R) it does not produce a stable orbit, it begins an orbit but ultimately collides with the mass fairly quickly. Its only when I abandon the naive way that it works. However, I was originally unclear about whether v = sqrt(GM/R) even should work correctly within the context of my 'naive' simulation. The leap frog approach looks interesting, does it require that I add external force? $\endgroup$ – Sean Thoman Oct 12 '11 at 21:34
  • $\begingroup$ Gravity should be the only force in the simulation. My early versions were stable enough for me to watch the system wander off the screen tens of orbits later because I had neglected to give the primary a initial velocity and the system therefore had non-zero total momentum. $\endgroup$ – dmckee --- ex-moderator kitten Oct 12 '11 at 21:39
  • $\begingroup$ So if setting my starting velocity (then letting gravity work the naive way) as v = sqrt(GM/R) doesn't work in forming a stable orbit do you think there is something else wrong with the code? $\endgroup$ – Sean Thoman Oct 12 '11 at 21:46
  • $\begingroup$ Probably, but there are other tests you can do...if you start your test body with no velocity does it fall straight in? Does it's initial acceleration vary linearly with the mass of the primary? And other such simple questions. I seem to recall that it is easy to get you trigonometric functions confused in decomposing the force/acceleration. $\endgroup$ – dmckee --- ex-moderator kitten Oct 12 '11 at 21:49
  • $\begingroup$ Hmm, on both those tests my simulation looks fine. Collapses straight into the mass just as you'd expect and the initial acceleration is linearly proportional to the mass. I think the problem may be a scaling issue. Not sure. Isn't it possible that I can't see stable orbits if I am operating at too close / small a scale? Its just not adding up to me...everything seems fine about the model. $\endgroup$ – Sean Thoman Oct 12 '11 at 21:59

You screwed up your code. You are taking the components of gravity by dividing by 1/r^2 a second time, when you already did it in the original step of calculating the gravitational force.

You should set G to 1. This just redefines the unit of mass, but it prevents confusions because in ordinary units G is enormously small.

Here is the correct algorithm. It just removes the cubes from the acceleration components.

        1) seperation of the points

        sep_x = x1 - x;
        sep_y = y1 - y;

        2) effect of gravity given distance between points

        gravity = (G * mass) / radialDistance ^ 2

        3) break down into components

        dx = (gravity * sep_x) / radialDistance 
        dy = (gravity * sep_y) / radialDistance

Your original simulation is still interesting--- it is calculating the motion in a 1/r^4 force field, a 1/r^3 potential. This type of thing is not physically relevant for the solar system, but it is a nice mathematical exercise. You can see that you didn't do it right because the orbits precess in such a field, instead of making closed ellipses.

To get the new velocities, I assume you multiply dx and dy by a small quantity and subtract them from v_x and v_y, the x and y components of the velocity, as you should, because you do not have a minus sign in your force formula. You then add v_x and v_y times the same small quantity to the current position. All this is assuming a unit mass, which is fine, because I assume you don't move your gravitational center at all during the simulation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.