public static float GetT0(GravitySystem gravitySystem) { GravitySystem parentSystem = gravitySystem.parentSystem; // No Central body, thus no prediction if (!parentSystem) { return(0); } float x = gravitySystem.localStartPosition.x; float y = gravitySystem.localStartPosition.y; float q = instance.gravityConstant * parentSystem.GetMass(); float a = gravitySystem.localStartPosition.magnitude; float T = 2 * Mathf.PI * Mathf.Sqrt(Mathf.Pow(a, 3) / q); float xt0 = -(Mathf.Acos(x / a) * T) / (2 * Mathf.PI); float yt0 = -(Mathf.Asin(y / a) * T) / (2 * Mathf.PI); string s = gravitySystem.name + "\n"; s += "X: " + x + " xT0: " + xt0 + "\n"; s += "Y: " + y + " yT0: " + yt0 + "\n"; print(s); float t0 = 0; t0 = y > 0 ? xt0 : yt0; t0 = y < 0 && x < 0 ? -xt0: t0; return(t0); }
/* * Circular Orbit Prediction * * x(t)=acos( (2π(t−t0)) / T) * y(t)=asin( (2π(t−t0)) / T) * * T=2π * sqrt( a^3 / q ) * * of the central body * q = G * M */ public static OrbitPrediction GetStaticOrbitPrediction(float t, GravitySystem gravitySystem) { GravitySystem parentSystem = gravitySystem.parentSystem; // No Central body, thus no prediction if (!parentSystem) { return(new OrbitPrediction(t, gravitySystem)); } float q = instance.gravityConstant * parentSystem.GetMass(); float a = gravitySystem.localStartPosition.magnitude; float T = 2 * Mathf.PI * Mathf.Sqrt(Mathf.Pow(a, 3) / q); float t0 = gravitySystem.t0; float x = a * Mathf.Cos((2 * Mathf.PI * (t - t0)) / T); float y = a * Mathf.Sin((2 * Mathf.PI * (t - t0)) / T); float xV = -a *Mathf.Sin((2 * Mathf.PI * (t - t0)) / T) * (2 * Mathf.PI) / T; float yV = a * Mathf.Cos((2 * Mathf.PI * (t - t0)) / T) * (2 * Mathf.PI) / T; Vector2 localPosition = new Vector2(x, y); Vector2 localVelocity = new Vector2(xV, yV); return(new OrbitPrediction(t, localPosition, localVelocity)); }
/// <summary> /// In Editor /// </summary> public void CheckSystem() { mass = 0; if (!renderer) { renderer = GetComponentInChildren <SpriteRenderer>(); } //Radius of Influence GravitySystem parentSystem = null; if (transform.parent) { parentSystem = transform.parent.GetComponent <GravitySystem>(); } if (parentSystem) { float distToParentSytem = ((Vector2)parentSystem.transform.position - (Vector2)transform.position).magnitude; radiusOfInfluence = OrbitMath.CircleOfInfluence(distToParentSytem, GetMass(), parentSystem.GetMass()); } else { radiusOfInfluence = 10000; } // Add Siblings in RadiusOfInfluence if (parentSystem) { GravitySystem[] siblingSystems = parentSystem.GetChildSystems(); foreach (GravitySystem siblingSystem in siblingSystems) { if (siblingSystem == this) { continue; } float distance = Vector2.Distance(transform.position, siblingSystem.transform.position); if (distance < radiusOfInfluence && siblingSystem.GetMass() < GetMass()) { siblingSystem.transform.SetParent(transform); } if (distance < siblingSystem.radiusOfInfluence && siblingSystem.GetMass() > GetMass()) { transform.SetParent(siblingSystem.transform); } } } // Check if this system exited another System if (parentSystem) { float distance = Vector2.Distance(transform.position, parentSystem.transform.position); if (distance > parentSystem.radiusOfInfluence) { transform.SetParent(parentSystem.transform.parent); } } }