private void AddMoon(float mass, float radius, float distance) { // Instantiate a moon and set appropriate variables CellestialPhysics obj = Instantiate(moonPrefab, planet.transform.position + new Vector3(distance / KilometersPerSquare, 0, 0), Quaternion.identity); obj.mass = mass; obj.radius = radius; obj.SetImageScale(KilometersPerSquare); // Apply starting speed perpendicular to gravitational force to achieve stable orbit Vector2 distanceVector = obj.transform.position - planet.transform.position; float speed = (float)Math.Sqrt(66.742f * planet.mass / distance); Vector2 directionVector = new Vector2(distanceVector.y, -distanceVector.x); obj.SetSpeed(directionVector.normalized * speed); // Add moon object to moons list cellObjs.Add(obj); Pair <CellestialPhysics> temp; // Add all new moon-moon combinations for (int i = cellObjs.Count - 2; i >= 0; i--) { temp = new Pair <CellestialPhysics>(obj, cellObjs[i]); objectCombos.Add(temp); } // Plus one planet-moon combination temp = new Pair <CellestialPhysics>(planet, obj); objectCombos.Add(temp); // Log what's been done Debug.Log(String.Format("Added new moon (mass: {0} Yg, radius: {1} km, distance: {2} km)", mass, radius, distance)); }
Projection FindProjection(ProjType type, CellestialPhysics obj, Vector3 axis) { // Finds min (ProjType.BEGIN) or max (ProjType.END) projection of // an object on the provided axis // -- mark the axis you're interested in with a 1 for that vector // component, leave all others on 0, i.e (0,1,0) checks the Y axis Projection proj = new Projection(); proj.body = obj; SpriteRenderer sr = obj.GetComponent <SpriteRenderer>(); float num; switch (type) { case ProjType.BEGIN: num = Vector3.Dot(axis, sr.bounds.min); proj.value = num; proj.type = ProjType.BEGIN; break; case ProjType.END: num = Vector3.Dot(axis, sr.bounds.max); proj.value = num; proj.type = ProjType.END; break; } return(proj); }
void Awake() { // Initialization objectCombos = new List <Pair <CellestialPhysics> >(); cellObjs = new List <CellestialPhysics>(); // Instantiate the earth in the middle of the scene planet = (CellestialPhysics)Instantiate(planetPrefab, new Vector3(0, 0, 0), Quaternion.identity); planet.mass = 5972.6f; planet.radius = 6371; planet.SetImageScale(KilometersPerSquare); }
Vector2 CalculateGravitationalForce(CellestialPhysics o1, CellestialPhysics o2) { // Calculates the gravitational force between two cellestial objects // return: Vector2 force - the grav. force vector pointing from o1 to o2 float mass1 = o1.mass; float mass2 = o2.mass; Vector2 distanceVector = o2.transform.position - o1.transform.position; float distance = distanceVector.magnitude * KilometersPerSquare; float gravScalar = 66.742f * (mass1 * mass2) / (distance * distance); // Gravitational force formula (result is in YN) Vector2 force = distanceVector.normalized * gravScalar; return(force); }
public Projection() { value = 0; body = null; type = ProjType.BEGIN; }
public Projection(float val, CellestialPhysics obj, ProjType t) { value = val; body = obj; type = t; }