- Home /
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.
Answer by Julien-Lynge · Apr 25, 2012 at 10:59 PM
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).
BTW: For the second option, the Unity functions you want to become familiar with are Transform, Vector2, and RigidBody, as well as Update vs FixedUpdate, and Time, especially Time.deltaTime.
Answer by bompi88 · Apr 26, 2012 at 12:52 AM
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.
Answer by aldonaletto · Apr 26, 2012 at 02:07 AM
Take a look at this question: http://answers.unity3d.com/questions/196749/objects-acting-under-each-others-gravity.html
Answer by pudd1nG · Apr 26, 2012 at 02:42 AM
Check out this script for a great example of simple planet gravity. http://www.unifycommunity.com/wiki/index.php?title=Simple_planetary_orbits
Answer by kaedmon · Jan 01, 2013 at 03:08 AM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613070749im_/https://answers.unity.com/themes/thub/images/avi.jpg)