Hello

I wrote my own gravity implementation to simulate some space behaviors, like Earth orbiting the Sun. The problem is I don’t know why it’s not precise.

Here is the most important code part (runs in fixed update):

```
//Gravitational force direction
Vector3 Fg_dir = oSource.transform.position - oAffected.transform.position;
float distance = Fg_dir.magnitude;
//Computing scalar value of gravitational force
float GMmR2 = (GConstant * oSource.Mass * oAffected.Mass) / (distance * distance);
Fg_dir.Normalize();
//Aplying force by using Constant force component
oAffected.constantForce.force =
new Vector3(GMmR2 * Fg_dir.x, GMmR2 * Fg_dir.y, GMmR2 * Fg_dir.z);
```

As you can see, it’s only gravitational force equation implementation.

I decided to run simple simulation:

Gravitational constant = 150f

gameobject A:

postion: {0; 0; 0}

mass: 100

gameobject B:

position: {30; 0; 0}

mass: 1

initial velocity: {0; 0; 22.36068f}

Initial velocity was computed from equation: Fg = Fc

Fg = Gravitational force

Fc = Centrifugal force

GMm/r^2 = mV^2/r

GM/r = V^2

V = sqrt( GM/r )

V = sqrt( 150f * 100f / 30f )

V = 22.36068f

Looks good, but after one orbit object B is at {35; 0; 0}.

After two orbits it is {40; 0; 0}

After three {45; 0; 0} and so on…

There is something wrong but I don’t know where

I turned off default engine gravity in rigidbodies. There is also no drag.