public static double convertToWorldAngle(double localAngle, double globalRotationAngle, bool towardsPerigee) { if (towardsPerigee) { if (globalRotationAngle < 0) { localAngle = Math.Abs(localAngle) - Math.Abs(globalRotationAngle); } else { localAngle = Math.Abs(globalRotationAngle) - Math.Abs(localAngle); } } else { if (globalRotationAngle < 0) { localAngle = Math.Abs((2 * Math.PI) - Math.Abs(globalRotationAngle) - Math.Abs(localAngle)); } else { localAngle = -Math.Abs((2 * Math.PI) - globalRotationAngle - localAngle); } } localAngle = MiscHelperFuncs.wrapAngle(localAngle); return(localAngle); }
//<summary> //Takes in everything in global coordinates (relative to the origin(0, 0)) and returns the game object that is influencing the craft //</summary> //<param name="position"> Re-read the summary //<param name="asdf"> Here's to the little guys private GameObject findInfluencingCelestialBody(Vector2 position, Vector2 velocity, GameObject currentMassiveBody) { GameObject[] massiveBodies = GameObject.FindGameObjectsWithTag("MassiveBody"); List <GameObject> spheresOfInfluence = new List <GameObject>(); for (int i = 0; i < massiveBodies.Length; i++) { //quick and dirty calculation of altitude double mu = massiveBodies[i].GetComponent <MassiveBodyElements>().mass *GlobalElements.GRAV_CONST; Vector2 relativePosition = position - MiscHelperFuncs.convertToVec2(massiveBodies[i].transform.position); Vector2 relativeVelocity = velocity + massiveBodies[i].GetComponent <GravityElements>().velocity; Vector2 eccentricity = OrbitalHelper.calculateEccentricity(relativePosition, relativeVelocity, mu); OrbitTypes orbitType = OrbitalHelper.determineOrbitType(eccentricity); double mechanicalEnergy = OrbitalHelper.calculateMechanicalEnergy(relativePosition, relativeVelocity, mu, orbitType); double semiMajorAxis = OrbitalHelper.calculateSemiMajorAxis(mechanicalEnergy, mu, orbitType); Vector2 perigee = OrbitalHelper.calculatePerigee(semiMajorAxis, eccentricity, orbitType); double semiLatusRectum = OrbitalHelper.calculateSemiLatusRectum(semiMajorAxis, eccentricity, perigee, orbitType); //semiMajorAxis * (1 - Math.Pow(eccentricity.magnitude, 2)); double trueAnomaly = Vector2.Angle(relativePosition, eccentricity); trueAnomaly = MiscHelperFuncs.convertToRadians(trueAnomaly); trueAnomaly = Math.Abs(MiscHelperFuncs.wrapAngle(trueAnomaly)); double altitude = Vector2.Distance(massiveBodies[i].transform.position, transform.position);//semiLatusRectum / (1 + eccentricity.magnitude * Math.Cos(trueAnomaly)); if (massiveBodies[i].GetComponent <MassiveBodyElements>().SphereOfInfluence > altitude && massiveBodies[i].transform.GetInstanceID() != this.transform.GetInstanceID()) { if (altitude < 0) { Debug.Log("altitude: " + altitude); Debug.Log("semilatusrectum: " + semiLatusRectum); Debug.Log("Eccentricity: " + eccentricity.magnitude); Debug.Log("true anomaly: " + trueAnomaly); } spheresOfInfluence.Add(massiveBodies[i]); } } double smallestDistance = double.PositiveInfinity; GameObject returnGameObject = currentMassiveBody; foreach (GameObject massiveBody in spheresOfInfluence) { double distance = Vector2.Distance(massiveBody.transform.position, transform.position); if (distance < smallestDistance) { smallestDistance = distance; returnGameObject = massiveBody; } } return(returnGameObject); }
public static double calculateMeanAnomaly(Vector2 eccentricity, double semiMajorAxis, double anomalyAtEpoch, double timeStep, double timeAtEpoch, bool clockwise, double mu, OrbitTypes orbitType) { double orbitalSpeed; double returnMeanAnomaly = double.PositiveInfinity; //Calculate percentage of orbit being crossed orbitalSpeed = Math.Sqrt((mu) / Math.Pow(Math.Abs(semiMajorAxis), 3)); if (clockwise) { returnMeanAnomaly = anomalyAtEpoch - (orbitalSpeed * timeStep); } else { returnMeanAnomaly = anomalyAtEpoch + (orbitalSpeed * timeStep); } if (orbitType != OrbitTypes.hyperbolic && orbitType != OrbitTypes.parabolic) { returnMeanAnomaly = MiscHelperFuncs.wrapAngle(returnMeanAnomaly); } return(returnMeanAnomaly); }