Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by toske_ · Feb 26, 2020 at 02:36 AM · 2d gamemath2d-physicsplanetary

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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
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 toske_ · Feb 26, 2020 at 05:40 PM 0
Share

Thank you so much for co$$anonymous$$g 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!

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

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

Getting rid of 2D collision jitter 1 Answer

Move game window like in rhythm doctor act 2 0 Answers

Player is bouncing when colliding with wall object 0 Answers

I was trying to make my game in unity but I got stuck while making "game over" part. please help! 0 Answers

How do I use the Physics2D.OverLapCollider function to collect and store the information in a ray in a array,How to use the Physics2D.OverlapCollider function 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