What is wrong with my 2D orbit calculation?

I am working on a 2D game, and I am working on adding some space physics, so I need to find a way to create a circular orbit.

I want to make my game object move on a circular way around a planet. I'm starting with the old famous formula:

V = sqrt(G * ((m1 + m2 ) / r))


This formula is the classic real world one, V is the velocity of the game object.

Once I code it, it doesn't move on a circular way, it just being attracted by the planet, like normal gravity. Here's my code (I'm using Unity):

dist = new Vector2(
Planet.renderer.bounds.center.x - renderer.bounds.center.x,
Planet.renderer.bounds.center.y - renderer.bounds.center.y
);
r = dist.magnitude;
rigidbody2D.velocity = dist.normalized * (Mathf.Sqrt (0.5f * ((mBody + mPlanet) / r)));


Dist is a 2D vector and r is a float, The G value is 0.5 (I used this one because 6.673E-11 is very small and it's not what I want)

So the question is is how can I make my GameObject move on a circular orbit like a satellite around earth ?

One thing to note, is that my GameObject is kicked by the player, so it get a speed of 12. (I am using addForce()).

• We prefer that you ask one question per post here, so I have removed your second question.
– user1430
Commented Aug 31, 2015 at 15:20
• Also note that a perfectly circular orbit is very unstable - you are likely to rapidly get a slightly eccentric ellipse if only due to rounding errors in yor math/physics engine. Commented Oct 1, 2015 at 3:57

From what it seems, you are approaching your problem backwards :)

The first problem you are facing is that you try to give an physics object, that is kicked by something else, a hard-coded trajectory, which means exterior influences (the kick) will not make it move.

Instead, you could implement the gravity simulation from the second problem first. Then, if you give the body the correct initial velocity (the one you calculated with V = sqrt(G * ((m1 + m2 ) / r))), it will follow an orbit trajectory (and since the masses of satellites are usually very small compared to that of the planet, you can even remove mBody from the formula).

The second problem should then be easy to solve. If nothing else than gravity is applied, the satellite will stay in its orbit. If you want it to decay slowly in a spiral, you could slow it down each frame. Just multiply the velocity vector by something smaller than 1, or apply a small force opposed to the speed.

The code called each frame would then look like this:

void update() // called before each frame is rendered
{
// Simulate gravity around a point
float r = distance.magnitude;
float force = 0.5f * mPlanet / (r*r);