• 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 /
  • Help Room /
avatar image
Question by toske_ · Feb 26, 2020 at 02:36 AM · 2d game2d-physicsmathplanetary

2D planetary body gravity visualization

Hello All !

I am making a little prototype with in 2D which involves simulating planetary gravity to some degree. I have implemented a function to calculate the force applied to the object based on the mass of both object and the distance between them which seems to work fine and gravity gets applied quite realistically.

However the problem that i encountered was when I tried to visualize the area of influence for the gravity for the bodies. As you can see from the screenshot there are some gizmo wire spheres that i attempted to use.

alt text

On start function of the orbital object I run this function to calculate the radius that then can used to draw the sphere. The way it works is that i run a while loop adding abit of distance to x and checking the strength of gravity at that position until its < 0.1f. while it seems to work if I change the starting location of the moons, the radius get bigger the further we get from the centre of the screen. I am perplexed as to why it happens. Opposite is also true the closer to the centre the smaller it gets.

this is the function to calculate radius.

  private float CalculateRadius()
     {
         Vector3 gravityStrength = GravityStrengthAtPosition(1, furthestPointOfGravity);
 
         while (gravityStrength.x > 0f)
         {   
             furthestPointOfGravity += new Vector2(0.01f, 0);
             gravityStrength = GravityStrengthAtPosition(1, furthestPointOfGravity);
         }
 
         float radius = Mathf.Abs(furthestPointOfGravity.x);
         return radius;
     }

this is to calculate the strength of gravity:

 public Vector3 GravityStrengthAtPosition(int objectMass, Vector3 objectPosition)
     {
         Vector3 direction = objectPosition - this.transform.position;
         float distance = direction.sqrMagnitude;
 
         float gravityStrength = (G * objectMass * mass) / distance;
 
         if(gravityStrength < 0.1f)
         {
             return Vector3.zero;
         }
 
         Vector3 force = direction.normalized * gravityStrength;
         Debug.Log(force);
         return force;
     }


I would love any insight and or help with this strange issue.

Thank you very much !

planet-gravity-radius.png (6.9 kB)
Comment

People who like this

0 Show 0
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 Reply

  • Sort: 
avatar image
Best Answer

Answer by Captain_Pineapple · Feb 26, 2020 at 08:57 AM

I'd guess that is because you do not initialize the furthestPointOfGravity to the position of your celestial bodys but to the origin.


looking at this i would really strongly advise to dig a bit into math for your project. What you currently do numerically (unreliable result and so on) can be done in one step analytically as well. you have an equation and want to check when it reaches a certain value so just set

0.1f = (G * objectMass * mass) / distance

and resolve that for distance which results in

distance = (G*objectMass*mass)*10


just do that calculation once and you have the distance in which your gravity value will become 0.1.


As another sidenote: In case you want to switch for a realistic model of gravity later on i can understand the numerical approach. Not sure if a analytical solution exists for that.

Comment

People who like this

0 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 toske_ · Feb 26, 2020 at 05:40 PM 0
Share

Thank you so much for coming back ! I was initializing furthestPointOfGravity in the start method and i had it serialized at the top of the class(I wanted to see what the distance was without running debug logs). I moved it back to the function and it does the same thing.

However you are correct, you solution is much more robust. I need to get my math up to scratch. Thank you for the help!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

240 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 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

newbie 2d gameplay 1 Answer

How do I get my character to go the the next level? 1 Answer

[2D] Slide object after collision 2 Answers

Using AddForce for horizontal movement doesn't quite work the way I want it to (2D) 1 Answer

How to cancel the force caused by collision? So the player is not pushed away when it hits a corner? 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