• 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 FredOld · Aug 19, 2018 at 04:47 PM · physicsgravityforcemode

Gravity simulation not accurate. Can't understand why

Hello

What I am doing is creating a simple, moderately accurate simulation where you can control a spacecraft. Anyway, to test my physics i created the Earth and the ISS, but made them 100000 times smaller, so instead of the earth being 12742000 meters in diameter, i have made it 127.42 and the distance between earth and ISS is 4 in stead of 400000 meters. Anyway, to make this scaling work in the universal law of gravitation, I have scaled the mass of the of the earth down by 10000000000 because the distance is squared in the equation of universal gravitation (Fg = G(Mm/r^2)) and then so is the scaling. Which means that instead of being 100000 times smaller it's 100000^2 times smaller. Anyway, I scale the mass of the earth down by that amount because the distance is dividing.

Instead of using the rigidbody mass of the earth i made a separate value in the script attached to it because the rigidbody doesn't allow such large values. anyway, I use the formula and the mass of the earth scaled down, the real value of G, the distance scaled down and I don't use m in the equation because since it mass doesn't affect the gravitational attraction on the spacecraft i just set the spacecraft's rigidbody mass to 1 that way when it applies the force i calculated it won't matter that i didn't use mass of the spacecraft.

Also, to keep the ISS in orbit, i give it it's real life velocity scaled down by the same amount> 0.0766

     void Attract(GameObject bodyToAttract)
     {
         Rigidbody rbToAttract = bodyToAttract.GetComponent<Rigidbody>();
 
         direction = transform.position - rbToAttract.position;
         distance = direction.magnitude;
 
         if (distance == 0)
             return;
 
         double forceMagnitude = G * (mass / Mathf.Pow(distance, 2));
 
         force = direction.normalized * float.Parse(forceMagnitude.ToString());
         rbToAttract.AddForce(force, ForceMode.Force);
     }


This is the part of the code that deals with the actual attracting. Anyway, the problem lies in that, despite me going over this a thousand times and spending hours trying to figure it out, it just doesn't work, the spacecraft just goes almost straight against the planet. It maintains of course it's initial velocity, but the force just seems way too strong, and it shouldn't be. I have tried using different ForceMode s but nothing works. I don't know if it's a problem with my math or with my code.

I may have explained this poorly so please ask any question you have.

Any help would be infinitely appreciated. This is quite important.

Thanks in advance

Comment
Add comment · Show 2
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 JVene · Aug 19, 2018 at 05:22 PM 1
Share

I've not taken time yet to study more about your inquiry, but what strikes me first is to ask about time. Have you scaled time?


In a reverse scenario, many students of Unity try their first stack of blocks with unit blocks (dimension is 1.0 to each side). For some reason they tend to think the simulation is too slow, because they're expecting 1.0 to be, perhaps, inches when it is actually meters. In their case the reverse of what I suspect you're observing happens, where ins$$anonymous$$d of the blocks moving like dice on a table, they move like 1 meter boxes on the floor. These students eventually discover how to scale time so the speed of falling looks right to them, but now time is moving too fast for other situations in their work, until they learn to work at more realistic scale.


The physics engine doesn't really "understand" scale, which is to say when you do actively choose to scale objects so, for example, the dimensions of space can fit into the engine's features, one must usually scale time as well. $$anonymous$$y thinking here is that an orbital balance may require a scale of time, because it may be that gravity vs time ends up crashing into the planet. I need more coffee and time to think, and this is one of those tuning efforts that may require more vision (for those of us trying to help) than snippets might reveal.

Show more comments

1 Reply

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

Answer by FredOld · Aug 20, 2018 at 09:32 AM

I have solved the issue. It turned out to be rather simple: I had scaled all the distances down by 100000, so 1 unit was 100000 meters and I had adjusted the mass by the same proportion to make up for that, as I wrote in my question. However, the force appeared to be out of proportion despite my rigorous examination of all the scaling I has done. And finally I found of course that I have to divide the force magnitude by 100000 as well, because 1 N = 1 kg⋅m⋅s^−2 So if I have made 1 unity meter into 100000 real meters, I also have to apply this scaling here.

Thanks for your help @JVene !

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 20, 2018 at 01:34 PM 0
Share

Actually you argued the wrong way ^^. What you did no scale is "G". G tells you how much force you get for a given mass and a given distance squared. It actually links meters with kg. However not in a 1:1 ratio.

G is m^3 * kg^-1 * s^-2. Since you scaled mass and distance both by your scale factor you need scale G by your scale factor squared. btw: You need scale mass only once by your scale factor.


It would be much easier when you simply go the other way round. Just work with the original values and just scale the input and output accordingly. So multiply your worldspace distance by your scalefactor, do the math and scale down the end result by your scale factor. Since we deal with floating point numbers and only multiplication / division the number of significant digits doesn't really change so precision is not an issue.


Btw: Why do you convert the double value to string and parse the float from that string again? You can simply cast the double to float. This is much more performant, doesn't create any garbage and is much safer.


Another optimisation would be to not use "magnitude" and Pow but just use sqr$$anonymous$$agnitude. You essentially calculate the square root and then square it again.


Finally keep in $$anonymous$$d that a simulation like that is only stable for a short time due to floating point precision

avatar image FredOld Bunny83 · Aug 20, 2018 at 02:32 PM 0
Share

Okay thank you. Though I am now super confused. I had it working accurately with what I said, so could you please explain only the scaling part for me? It would be really helpful, because I now have a complete mental block around this whole thing and can't get my head around what you mean.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

How to walk around a 3D sphere? 0 Answers

Physics not applying to the animated object after the animation ends ?! 0 Answers

Constant force, with gravity, and correct collisions. 2 Answers

Physics and gravity of my 3D model 0 Answers

How can i rotate player when player jump ? 2 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