4
\$\begingroup\$

I would like to create a space simulation with realistic gravity.

This means planets in 3d space which act as central gravity sources and multiple objects which are attracted by those planets gravity so that they orbit them in physically correct orbits (like in Kerbal Space Program, for example).

How can I implement such gravity sources with C# script?

\$\endgroup\$
  • \$\begingroup\$ Do you want realistic gravity? Or pseudo-gravity which just makes the satellites orbit at a fixed distance from the planet? (ie. Do you want closer objects to spiral inwards and crash, or fast objects to be slung around the planet; that kind of stuff.) \$\endgroup\$ – theonlygusti Sep 1 '15 at 12:05
  • 1
    \$\begingroup\$ @theonlygusti I want what I described in my own answer :). Actually, this is a proxy-question for someone whose question got closed before I could answer it. But when you think that you can add value by providing an alternative answer, feel free to post. \$\endgroup\$ – Philipp Sep 1 '15 at 12:10
  • \$\begingroup\$ Whoops, I didn't notice that you answered yourself xP How long left until you can accept it? \$\endgroup\$ – theonlygusti Sep 1 '15 at 12:43
  • \$\begingroup\$ @theonlygusti The popup says I can accept it "tomorrow". But don't let that stop you from contributing. \$\endgroup\$ – Philipp Sep 1 '15 at 12:44
2
\$\begingroup\$

You can learn about the gravity formula, Fg = G((Mass1 * Mass2)/distance^2), the distance is

d = sqrt( (x1 -x2)^2 + (y1 -y2)^2 + (z1 -z2)^2 )

now you can calculate the distance and use it in the Gravity formula.

after that you need to calculate the acceleration for each axi,

float aX = (x / hip) * tmp; // tmp is the gravity force and x is the object                  
                            // to go less the origin object
float aY = (y / hip) * tmp; //the same
float aZ = (z / hip) * tmp; //the same

so now add this to a variable x, y and z that represent the speed and upload all of this every frame * Time.deltaTime

\$\endgroup\$
  • \$\begingroup\$ With Unity you don't need to calculate each axis manually. You can use the Unity Vector3 arithmetics to calculate all three axis at once. \$\endgroup\$ – Philipp Jan 14 '17 at 10:06
1
\$\begingroup\$

Here is a C# script for a GravitySource component.

It requires a Collider attached to the same game object with the "Is Trigger" checkbox set. This collider represents the zone of influence of the bodies gravity. Any Rigidbodys inside of that collider will be affected.

The public property gravity needs to be set in the inspector. It represents the strength of the gravity source. The ideal value depends on the time-scale and space-scale of your simulation.

using UnityEngine;
using System.Collections;

public class GravitySource : MonoBehaviour {

    public float gravity;

    void OnTriggerStay(Collider other) {
        Rigidbody otherRigidbody = other.gameObject.GetComponent<Rigidbody>()
        if (otherRigidbody) {
            Vector3 difference = this.gameObject.transform.position - other.gameObject.transform.position;

            float dist = difference.magnitude;
            Vector3 gravityDirection = difference.normalized;
            Vector3 gravityVector = (gravityDirection * gravity) / (dist * dist) ;

            otherRigidbody.AddForce(gravityVector, ForceMode.Acceleration);

        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.