# 2D planetary body gravity visualization

Hello All !

I am making a little prototype with in 2D which involves simulating planetary gravity to some degree. I have implemented a function to calculate the force applied to the object based on the mass of both object and the distance between them which seems to work fine and gravity gets applied quite realistically.

However the problem that i encountered was when I tried to visualize the area of influence for the gravity for the bodies. As you can see from the screenshot there are some gizmo wire spheres that i attempted to use.

On start function of the orbital object I run this function to calculate the radius that then can used to draw the sphere. The way it works is that i run a while loop adding abit of distance to x and checking the strength of gravity at that position until its < 0.1f. while it seems to work if I change the starting location of the moons, the radius get bigger the further we get from the centre of the screen. I am perplexed as to why it happens. Opposite is also true the closer to the centre the smaller it gets.

this is the function to calculate radius.

```
private float CalculateRadius()
{
Vector3 gravityStrength = GravityStrengthAtPosition(1, furthestPointOfGravity);
while (gravityStrength.x > 0f)
{
furthestPointOfGravity += new Vector2(0.01f, 0);
gravityStrength = GravityStrengthAtPosition(1, furthestPointOfGravity);
}
float radius = Mathf.Abs(furthestPointOfGravity.x);
return radius;
}
```

this is to calculate the strength of gravity:

```
public Vector3 GravityStrengthAtPosition(int objectMass, Vector3 objectPosition)
{
Vector3 direction = objectPosition - this.transform.position;
float distance = direction.sqrMagnitude;
float gravityStrength = (G * objectMass * mass) / distance;
if(gravityStrength < 0.1f)
{
return Vector3.zero;
}
Vector3 force = direction.normalized * gravityStrength;
Debug.Log(force);
return force;
}
```

I would love any insight and or help with this strange issue.

Thank you very much !

**Answer** by Captain_Pineapple
·
Feb 26, 2020 at 08:57 AM

I'd guess that is because you do not initialize the `furthestPointOfGravity`

to the position of your celestial bodys but to the origin.

looking at this i would **really strongly** advise to dig a bit into math for your project. What you currently do numerically (unreliable result and so on) can be done in one step analytically as well.
you have an equation and want to check when it reaches a certain value so just set

`0.1f = (G * objectMass * mass) / distance`

and resolve that for distance which results in

`distance = (G*objectMass*mass)*10`

just do that calculation once and you have the distance in which your gravity value will become 0.1.

As another sidenote: In case you want to switch for a realistic model of gravity later on i can understand the numerical approach. Not sure if a analytical solution exists for that.

Thank you so much for coming back ! I was initializing `furthestPointOfGravity`

in the start method and i had it serialized at the top of the class(I wanted to see what the distance was without running debug logs). I moved it back to the function and it does the same thing.

However you are correct, you solution is much more robust. I need to get my math up to scratch. Thank you for the help!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.