using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UniversalGravitation : MonoBehaviour {

	private PhysicsEngine[] physicsEngineArray;
	private const float bigG = 6.67e-11f;
	// N * (m/kg)^2 // [m^3 /kg*s^2]



	// Use this for initialization
	void Start () {
		physicsEngineArray = GameObject.FindObjectsOfType<PhysicsEngine>();
	}


	void CalculateGravity () {
		foreach (PhysicsEngine peA in physicsEngineArray) {
			foreach (PhysicsEngine peB in physicsEngineArray) {
				if (peA != peB) {
					Vector3 offset = peA.transform.position - peB.transform.position;
					float rsquared = Mathf.Pow ((offset).magnitude, 2);
					float forceOfGravity = (bigG * peA.mass * peB.mass) / rsquared;
					Vector3 gravityFeltVector = forceOfGravity * offset.normalized;
					peA.AddForce (-gravityFeltVector);
				}
			}
		}

	}
		
	void FixedUpdate () {
		CalculateGravity();
	}
}
