2
\$\begingroup\$

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 ?

enter image description here

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

\$\endgroup\$
2
  • \$\begingroup\$ We prefer that you ask one question per post here, so I have removed your second question. \$\endgroup\$
    – user1430
    Commented Aug 31, 2015 at 15:20
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 1, 2015 at 3:57

1 Answer 1

3
\$\begingroup\$

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);
    satellite.addForce(distance.normalized * force);

    // brake the satellite slightly to make it spiral towards the planet
    float brakeFactor = 0.01f; // example, you will have to fiddle with that
    Vector2 satSpeed = satellite.velocity; // get the velocity vector
    satellite.addForce(-satSpeed.normalized * brakeFactor) // apply the braking force, opposed to speed
}

Hope it's clear enough, ask any questions you have otherwise! :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .