Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Creating self contained gravity for multiple planets

Discussion in 'Physics' started by wrinklemonster, Feb 18, 2015.

  1. wrinklemonster

    wrinklemonster

    Joined:
    Feb 18, 2015
    Posts:
    1
    I'm trying to create a script that will create a gravity force on a planet, that can be applied to a prefab and then applied to whatever planet's i create, making it so each planet's gravity is not dependent on the existance or location of another planet for it to maintain gravity on the player.

    The end desire is to provide a seamless surface of planet to space to surface of other planet transition, staying away from Parent/Child relationships between Player and Planet.

    The Planet's may not rotate and they will definitley remain in static positions, so concern for orbit is unnecessary.

    any suggestions I will be highly interested in, thank you in advance!
     
  2. modegames

    modegames

    Joined:
    Dec 1, 2014
    Posts:
    37
    I would do this by using a sphere with a collider set to a trigger to then maintain a list of objects within the distance which gravity applies.

    Then I would calculate a direction vector by subtracting the position values between the orbiting object and the planet object. Then normalise to get a direction vector and multiply by a gravity factor, this could be based on the mass of the planet. Lastly use AddForce on the rigid body to apply this gravity force onto the orbiting object.
     
  3. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,567
    This is how you calculate gravitational acceleration:

    Code (CSharp):
    1. /// <summary>
    2.     /// The Newton's constant, an empirical physical constant involved in the calculation(s) of gravitational force between two bodies.
    3.     /// </summary>
    4.     private const float gravitationalConstant = 6.672e-11f;
    5.    
    6.     /// <summary>
    7.     /// Calculates gravitational acceleration for a Rigidbody under the influence of a large "mass" point at a "position".
    8.     /// Use this in each FixedUpdate(): rigidbody.velocity += GAcceleration(planetPosition, planetMass, rigidbody);
    9.     /// </summary>
    10.     /// <returns>The acceleration.</returns>
    11.     /// <param name="position">Position of the planet's center of mass.</param>
    12.     /// <param name="mass">Mass of the planet (kg). Use large values. </param>
    13.     /// <param name="r">The Rigidbody to accelerate.</param>
    14.     public static Vector3 GAcceleration(Vector3 position, float mass, Rigidbody r) {
    15.         Vector3 direction = position - r.position;
    16.        
    17.         float gravityForce = gravitationalConstant * ((mass * r.mass) / direction.sqrMagnitude);
    18.         gravityForce /= r.mass;
    19.        
    20.         return direction.normalized * gravityForce * Time.fixedDeltaTime;
    21.     }
    Units are m/kg/s
     
    Nanako likes this.