Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mattbarton.exe · Apr 25, 2012 at 10:49 PM · physicsrigidbodyspaceplanets

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Apr 25, 2012 at 11:00 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Available from the Unity Asset Store, here.

More information, here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Artificial Gravity 2 Answers

Player in Spaceship 2 Answers

Rigidbody gravity between planets 1 Answer

Child rigidbody acting strange when moving parent. 0 Answers

Drag & Drop GameObject with MovePosition shoots other Gameobject away in case of them colliding 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges