How do you calculate gravity from planets in a solar system?

Hi, all. I’m trying to create a “Gravity Wars” type game where you have a solar system and all of the heavenly bodies (including the sun) have gravitational fields. So when you fire a missile in one location, its trajectory is affected by the heavenly bodies it passes nearby.

What functions of Unity should I be looking at to try to calculate multiple sources of gravity from all those different (and moving) bodies?

It’s a 2D game, which should (I hope) make this a lot simpler.

You have two options as I see it: work within Unity’s physics system or do the math yourself. If you use physics, you can’t use the physics gravity (turn off useGravity), since it’s one dimensional - you can’t have point gravity. What you can use are the RigidBody functions. Rigidbody forces are evaluated every FixedUpdate, so each fixedUpdate you’d have to figure out the force to apply based on the position of each gravity well, so something in a for loop.

If, on the other hand, you want to calculate it yourself, the process is largely the same - every FixedUpdate (or Update if you prefer) you figure out the change in velocity or position due to the acceleration of each active gravity field acting on the object, and then set the new velocity or position accordingly.

Sounds hard, but you can use the simple equation F=ma and derive your equations from there. Once you have those, and a loop for each source of gravity, you’re basically done. Remember that Unity is a discrete system, so at different framerates objects will travel slightly different paths (unless you want to get into differential equations and other fun math).

Additional: Search wikipedia for “Newton’s law of universal gravitation”. With an adjusted gravitational constant and mass (that fits your world´s metric system) you can loop between the nearest planets (and for example the sun with its high gravitational drag), and calculate the gravitational force, then the object´s acceleration and velocity. But you don`t want to calculate this for hundreds of objects each update, maybe per 5-10 th update? You can try and see what you get.

Take a look at this question:

Check out this script for a great example of simple planet gravity. unifycommunity.com

For any developers out there who would like to implement this really easily, try out:

World Physics System ~ http://nimbusgarden.com/worldphysicssystem

The World Physics System is a celestial body & point gravity scripting interface for Unity, intended as a replacement for the stock downward gravity. WPS can be used to simulate planetary orbits, body-body attraction, or “snowballing” effects. You may use it to create spherical worlds “out-of-the-box,” but World Physics System is implemented robustly and is lightweight, providing you complete freedom in expressing your point gravitation creativity, complex and repulsive forces, allowing you to use the system as a subcomponent for other effects, such as spells, powerups, or goal-based Artificial Intelligences for NPC’s. With the WPS, the sky is truly the limit!

Available from the Unity Asset Store, here.

More information, here.

It has been a long time since this has been answered, but maybe I can give some additional info.

The basic idea is to use Newton’s Gravitational pull, as mentioned by other comments. Other answers give some links to basic code on how to solve that.


Solution


I have created a blog entry that solves this issue: Physics-based solar system lessons. It explains:

  • The math used.
  • The problems found (precision issues, orbit prediction, Speeding up the system)
  • The implementation code (and basic setup)
  • Where to find accurate planetary data.

Here you can see an example of the applied code in the blog in a Earth-Moon system with a couple of satellites (orbiting Earth and Moon).
alt text