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 Major · Apr 21, 2013 at 06:23 PM · gravityuniverse

Newtonian Gravity

I am attempting to make a realistic gravity simulation that can use the mass of an object to then calculate how much gravity it has using Newton's law of universal gravity. Here is what I have so far:

 function gravity(float m1, m2, r)
 {
     f = 6.674 e -11 * m1 * m2 / (r * r);    
     
     return f;
 }

Of course this is only a start, and I have no idea what to do from here. Any help is welcome!

Comment
Add comment · Show 5
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 DESTRUKTORR · Apr 21, 2013 at 06:29 PM 0
Share

The one suggestion I will make is that you avoid using rigidbody mass, as this variable does not function like normal mass, and becomes unstable with values exceeding 10. You can find more about how rigidbody handles physics here.

avatar image Major · Apr 21, 2013 at 06:31 PM 0
Share

I want to try to use a variable in the script that states how much the object weighs, however I have no idea how.

avatar image DESTRUKTORR · Apr 21, 2013 at 06:58 PM 0
Share
 @Range (0.1, 10000.0)
 var mass : float;
 
 function gravity(float m1, m2, r)
 {
     f = 6.674 e -11 * m1 * m2 / (r * r);   
  
     return f;
 }

This should give you a proper start :) The @Range (0.1, 10000.0) bit will more or less just limit the field between 0.1 and 10000.0 units, in the inspector. You can set the upper limit higher, if you want, all the way up to javascript's max value for floating point numbers (the exact value of which I'm not certain, but it's pretty big :P).

avatar image Major · Apr 21, 2013 at 07:02 PM 0
Share

Right but how would I find m1, m2, r, and then I would need to intergrate the mass? (Sorry this is way outside my realm)

avatar image arjunxxxl · Jun 21, 2018 at 04:03 AM 0
Share

Hi, I have a video addressing your problem. Have a look here

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by nenorse · Jun 26, 2014 at 01:20 PM

Cool - for those of you who need the full class listing - here it is (in a class called GameManager)

-(this also gives a orbit between the two objects)

using UnityEngine; using System.Collections;

public class GameManager : MonoBehaviour {

 // Use this for initialization
 void Start () {
     GameObject[] Objects = GameObject.FindGameObjectsWithTag ("Planet");
     
     //the gravity between each couple of object is calculated
     foreach (GameObject ObjectA in Objects) 
     {
         ObjectA.rigidbody.AddForce(new Vector3(100,0,0));
     }
 }


 void ApplyGravity(Rigidbody A, Rigidbody B)
 {
     //This is how to get the distance vector between two objects.
     Vector3 dist = B.transform.position - A.transform.position; 
     float r = dist.magnitude;
     dist /= r;
     
     //This is the Newton's equation
     //G = 6.67 * 10^-11 N.m².kg^-2
     double G =  6.674f * (10 ^ 11);
     float force = ((float)G * A.mass * B.mass) / (r * r);
     
     //Then, just apply the forces
     A.AddForce (dist * force);
     B.AddForce (-dist * force);
 }

 void FixedUpdate () 
 {
     //Get every object 
     GameObject[] Objects = GameObject.FindGameObjectsWithTag ("Planet");
     
     //the gravity between each couple of object is calculated
     foreach (GameObject ObjectA in Objects) 
     {
         foreach (GameObject ObjectB in Objects)
         {
             //Objects must not self interact 
             if(ObjectA == ObjectB)
                 continue;
             
             ApplyGravity(ObjectA.rigidbody, ObjectB.rigidbody);
         }
         
     }
 }

}

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
3

Answer by Megasyl20 · Jan 19, 2014 at 01:22 AM

Hello ! I had the same issue, and even if the topic is old, it could help someone.. So here's a code. It's pretty simple, but it works.

 void ApplyGravity(Rigidbody A, Rigidbody B)
     {
         //This is how to get the distance vector between two objects.
         Vector3 dist = B.transform.position - A.transform.position; 
         float r = dist.magnitude;
            dist /= r;

         //This is the Newton's equation
         //G = 6.67 * 10^-11 N.m².kg^-2
         float force = ((float)G * A.mass * B.mass) / (r * r);

         //Then, just apply the forces
         A.AddForce (dist * force);
         B.AddForce (-dist * force);
     }

Every "planet" or whatever must have the tag "Object"

 void FixedUpdate () 
    {
         //Get every object 
         Objects = GameObject.FindGameObjectsWithTag ("Object");

         //the gravity between each couple of object is calculated
         foreach (GameObject ObjectA in Objects) 
         {
             foreach (GameObject ObjectB in Objects)
             {
                 //Objects must not self interact 
                 if(ObjectA == ObjectB)
                     continue;

                 ApplyGravity(ObjectA.rigidbody, ObjectB.rigidbody);
             }
 
         }
     }

I'm sorry, i'm not into Javascript, this is C# Hope this will help ! Have a nice day.

Comment
Add comment · Show 4 · 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 caperneoignis · Jun 07, 2015 at 02:49 AM 0
Share

I know this post is old, but you'll just saved me a but load of time, trying to figure out how to get the objects to interact with each other. The math part is easy (already have C# experience), but I'm new to unity. This saved me so much time.

Thank you,

avatar image Ahapid · Jun 01, 2016 at 04:55 PM 0
Share

Its old yes... but how to exactly apply the scipt?

avatar image Major Ahapid · Jun 02, 2016 at 06:25 PM 0
Share

It depends on what your particular project is. When looking at these posts for help, what you will get is an outline of something you want, but not exactly what you need. It is up to you do decide how to implement it into your own project.

avatar image Ahapid Major · Jun 02, 2016 at 06:34 PM 0
Share

ah ok.. thx

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

17 People are following this question.

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

Related Questions

Universe function on gravity script not working 1 Answer

Newtonian physics in a space game 1 Answer

How to make Object1 be upfront Object2 even though it is inside of it? 0 Answers

How can I implement Planet Gravity in my game? 2 Answers

Movement with character controller on different walls 0 Answers


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