Created
June 20, 2021 07:28
-
-
Save tk009999/dc646de5b7156afe9ac68c79ab20b280 to your computer and use it in GitHub Desktop.
Simulate Gravitational of Planet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class SimpleGravitational : MonoBehaviour | |
{ | |
public float planetMass; | |
public float asteroidMass; | |
Transform Planet; | |
Transform Asteroid; | |
MeshRenderer planetMesh; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
Planet = GameObject.FindGameObjectWithTag("Planet").transform; | |
planetMesh = Planet.GetComponent<MeshRenderer>(); | |
Asteroid = transform; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
private void OnTriggerEnter(Collider other) | |
{ | |
if (other.tag == Planet.tag) | |
{ | |
Destroy(gameObject); | |
} | |
} | |
private void FixedUpdate() | |
{ | |
Vector3 position = Asteroid.position; | |
Force(planetMass, asteroidMass, planetMesh.bounds.center, ref position); | |
Asteroid.position = position; | |
} | |
/// <summary> | |
/// The Newton's constant, an empirical physical constant involved in the calculation(s) of gravitational force between two bodies. | |
/// </summary> | |
private const float gravitationalConstant = 667.4f; // the starded value are 6.674×10^(-11) N where here is 667.4 (in our case is mutiply a big number) | |
public void Force(float planetMass, float asteroidMass, Vector3 planetPos, ref Vector3 asteroidPos) | |
{ | |
Vector3 direction = asteroidPos - planetPos; | |
direction = -direction; | |
float distance = direction.magnitude; | |
// F = GMm / r ^ 2 | |
// G = 6.674×10^(-11) N·m^2/kg^2 | |
// M = first mass as self | |
// m = second mass as target | |
// r = distance between two vector | |
float kg = 1; | |
float meter = 1; | |
float M = asteroidMass * kg; | |
float m = planetMass * kg; | |
float G = gravitationalConstant * Mathf.Pow(m, 2) / Mathf.Pow(kg, 2); | |
float r = distance * meter; | |
float F = G * M * m / Mathf.Pow(r, 2); | |
if (r <= 0f) | |
{ | |
return; | |
} | |
direction = direction.normalized; | |
AddForce(direction * F * Time.fixedDeltaTime, ref asteroidPos, asteroidMass); | |
} | |
/* | |
dv = v - v0 ... where v0 is the velocity in the beginning (usually is v0 = 0) | |
dt = t - t0 ... where t0 is the time in the beginning (usually is t0 = 0) | |
so ... our formula is | |
1. a = (v - v0) / (t-t0) | |
2. a = (v - 0) / (t - 0) | |
3. a = v/t (this is ONLY BECAUSE v0 = 0 and t0 = 0) | |
4. a = dv/dt | |
F = m*a | |
*** F = m * (v/t), where "m" is the mass of the object, "v" is the desired velocity and t = Time.fixedDeltaTime. | |
*/ | |
Vector3 F = Vector3.zero; | |
Vector3 a = Vector3.zero; | |
Vector3 v = Vector3.zero; | |
public void AddForce(Vector3 force, ref Vector3 position, float mass) | |
{ | |
F += force; | |
a += (F / mass); | |
v += a * Mathf.Pow(Time.deltaTime, 2); | |
position += v; | |
a = Vector3.zero; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment