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 Hawkeye91803 · Feb 21, 2021 at 04:33 AM · gravityorbitconversionforcesrealistic

Realistic gravity much too strong

I'm creating a game with realistic gravity and orbital mechanics, but when I plug in numbers for the mass of the planet, the gravity created is much much stronger than it realistically should be. I believe there is some kind of unit conversion error, but I can't figure out what that would be. Thanks for any help you guys could provide.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Gravity : MonoBehaviour
 {
 
     public GameObject planet;
     public GameObject ship;
 
     private Vector3 planetPosition;
     private Vector3 shipPosition;
 
     public Rigidbody shipRigidbody;
 
     private float shipMass = 2000.0f;
     private float planetMass = 86700000000000000000.0f;
 
     Vector3 startVelocity = new Vector3(0f, 0f, 0f);
 
 
 
     //Newtons Gravity Equation
     public Vector3 calculateForce()
     {
         //Find the positions of the objects
         planetPosition = planet.transform.position;
         shipPosition = ship.transform.position;
         //Distance between the objects (r)
         float distance = Vector3.Distance(planetPosition, shipPosition);
         //Distance squared (r^2)
         float distanceSquared = distance * distance;
         //Gravitational Constant (G)
         float G = 6.67f * Mathf.Pow(10, -11);
         //F = G*m*m / r^2
         float force = G * ((planetMass * shipMass) / (distanceSquared));
         //Get the heading
         Vector3 heading = (planetPosition - shipPosition).normalized;
         //Turn the force from just a value into a 3D vector with direction
         Vector3 forceWithDirection = (force * (heading/heading.magnitude));
         //Return Force
         return (forceWithDirection);
     }
 
     void Start()
     {
         //Give Ship initial velocity
         shipRigidbody.AddForce(startVelocity, ForceMode.VelocityChange);
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         //Calculate and add the force of gravity to the ship
         shipRigidbody.AddForce(calculateForce() * 0.1f, ForceMode.Impulse);
     }
 }
 
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
1

Answer by Vollmondum · Feb 21, 2021 at 06:05 AM

Seems like you're trying to fit 20-digit value into a float, so it cuts along the way. Try using double

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 Hawkeye91803 · Feb 21, 2021 at 06:37 AM 0
Share

Alright, I didn't know that was a thing. I'll let you know what the results are, thanks.

avatar image Hawkeye91803 · Feb 21, 2021 at 07:03 AM 0
Share

So I tried it out, and while the function returns the expected force value (aprox 60N), the acceleration I get is still much higher than I would have expected. I would have expected an acceleration of 0.029m/s^2, but I get more like 3ish m/s^2.

Could this be due to the rate at which the physics engine updates?

avatar image Vollmondum Hawkeye91803 · Feb 21, 2021 at 05:40 PM 0
Share

There's DRAG setting in rigidbody, which is air resistance. Physics settings is pretty tough to master. The other day I spent 4 hours adjusting a platformer. Just play around till you get expected behavior

avatar image Hawkeye91803 Vollmondum · Feb 22, 2021 at 05:26 PM 0
Share

Thanks, I eventually figured out the right numbers.

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

120 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

Related Questions

How to make an orbital movement as smoothly as possible? 0 Answers

How can I make an efficient, yet accurate planetary orbit using Apoapsis and Periapsis distance from an object I am orbiting 1 Answer

How to lock an objects orbiting focal point. 0 Answers

problems with single body Newtonian physics emulation 0 Answers

How to rotate a ship based on gravity around a planet? 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