Alright, back with another one. I’ve been slowly making my way through understanding this…it’s been painful to say the least.
So here’s where I’m at:
- I start with creating an array of GameObjects…three to be exact
- I attach a VERY basic (it literally only has mass) nBody script to each of the GameObjects
- I attach a Rigidbody to and turn off gravity for each of the GameObjects
- I set the mass of the Rigidbody equal to the mass of the simple nBody script
- Since it’s a public class (for testing) and I’m simply manually placing the objects in the array from above, I set index 0 equal to the sun and then make it kinematic (since I’m wanting a fixed reference point for things to rotate around)
- I add an initial force (I ACTUALLY NEED HELP COMING UP WITH A WAY TO CALCULATE THIS) which I largely just…make up until things look ‘right’.
- Next in FixedUpdate() I start first with the first planet, determine distances between the objects and apply forces…then I start on the second planet object…
Here’s the code so far, and here’s the help I’m asking for:
As you can see in bold above, I haven’t hit on a method for calculating the initial force to establish a more or less uniform orbit, does anyone have any insights? I’ve got the following data (which actually isn’t implemented in this code):
for my planetary system. I know how to find the velocity, but the problem is…when I go to astronomical scales things tend to stop working…
Also, is my implementation (which is 100% brute force) also more or less correct? I need to incorporate time, which means I probably need to dig into integration…which I haven’t done a whole lot of, but I should be able to work my way through it.
Oh, one other thing that I’ve noticed, and I suspect it’s because of how the current code is implemented, when the bodies collide, they tend to go flying away from each other at tremendous speeds. I don’t think that’s quite right behaviorally…so with my implementation, what part of my rudimentary understanding of how this works is broken or causing that ‘freak out’ to occur? Or is that just something inherent in the rigidbody?
Anyway, code! This current implementation ignores the use of the constants currently also…and this almost gets a figure 8 around the second planet.
Masses for the bodies in my scene are:
Sun = 10
Planet 1 = 0.387
Planet 2 = 10
using UnityEngine;
using System.Collections;
public class NBODYCONSTANTS
{
public const float EARTHRADIUS = 6371000.0f; //m
public const float EARTHDENSITY = 5510.0f; //kg/m^3
public const float EARTHMASS = 5.972E24f; //kg
public const float EARTHGRAVITY = 9.806f; //m/s^2
public const float EARTHFORCE = 5.294E33f; //N
public const float SUNMASS = 1.989E30f; //kg
public const float GRAVCONSTANT = 6.667E-11f; //N m^2 / kg^2
public const float AU = 1.496E11f; //m
public const float SEC2YR = 3.171E-08f;
}
public class NBodyTest : MonoBehaviour
{
public GameObject[] nBody = new GameObject[3];
void Awake()
{
for (int i = 0; i < nBody.Length; i++)
{
nBody[i].AddComponent<Rigidbody>();
nBody[i].GetComponent<Rigidbody>().useGravity = false;
nBody[i].GetComponent<Rigidbody>().mass = nBody[i].GetComponent<nBodySimple>().nBodyMass;
}
nBody[0].GetComponent<Rigidbody>().isKinematic = true;
nBody[1].GetComponent<Rigidbody>().AddForce(Vector3.left * 32.25f);
nBody[2].GetComponent<Rigidbody>().AddForce(Vector3.left * 500.0f);
}
void Start()
{
}
void FixedUpdate()
{
//Sun and Planet 1
Vector3 line = nBody[0].transform.position - nBody[1].transform.position;
//Sun and Planet 2
Vector3 line2 = nBody[2].transform.position - nBody[1].transform.position;
float dist1 = Vector3.Distance(nBody[0].transform.position, nBody[1].transform.position);
float dist2 = Vector3.Distance(nBody[2].transform.position, nBody[1].transform.position);
line.Normalize();
line2.Normalize();
nBody[1].GetComponent<Rigidbody>().AddForce(line * (nBody[0].GetComponent<Rigidbody>().mass * nBody[1].GetComponent<Rigidbody>().mass) / (dist1 * dist1));
nBody[1].GetComponent<Rigidbody>().AddForce(line2 * (nBody[1].GetComponent<Rigidbody>().mass * nBody[2].GetComponent<Rigidbody>().mass) / (dist2 * dist2));
//Sun and Planet 2
Vector3 line3 = nBody[0].transform.position - nBody[2].transform.position;
//Planet 2 and Planet 1
Vector3 line4 = nBody[2].transform.position - nBody[1].transform.position;
float dist3 = Vector3.Distance(nBody[0].transform.position, nBody[2].transform.position);
float dist4 = Vector3.Distance(nBody[2].transform.position, nBody[1].transform.position);
line3.Normalize();
line4.Normalize();
nBody[2].GetComponent<Rigidbody>().AddForce(line3 * (nBody[0].GetComponent<Rigidbody>().mass * nBody[2].GetComponent<Rigidbody>().mass) / (dist3 * dist3));
nBody[2].GetComponent<Rigidbody>().AddForce(line4 * (nBody[2].GetComponent<Rigidbody>().mass * nBody[1].GetComponent<Rigidbody>().mass) / (dist4 * dist4));
}
}