Beispiel #1
0
    //<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 bool towardsPerigeeOrbit(Vector2 velocity, Vector2 eccentricity, OrbitTypes orbitType)
    {
        switch (orbitType)
        {
        case OrbitTypes.circular:
            return(MiscHelperFuncs.convertToRadians(Vector2.Angle(Vector2.right, velocity)) < Math.PI / 2);

        case OrbitTypes.elliptical:
            return(MiscHelperFuncs.convertToRadians(Vector2.Angle(eccentricity, velocity)) < Math.PI / 2);

        case OrbitTypes.parabolic:
            return(true);

        case OrbitTypes.hyperbolic:
            return(MiscHelperFuncs.convertToRadians(Vector2.Angle(eccentricity, velocity)) < Math.PI / 2);
        }
        return(true);
    }
    public static double calculateTrueAnomaly(Vector2 eccentricity, Vector2 position, bool towardsPerigee, bool clockwise, OrbitTypes orbitType)
    {
        double returnTrueAnomaly = double.PositiveInfinity;

        switch (orbitType)
        {
        case OrbitTypes.circular:
            returnTrueAnomaly = Vector2.Angle(position, Vector2.right);
            returnTrueAnomaly = MiscHelperFuncs.convertToRadians(returnTrueAnomaly);
            break;

        case OrbitTypes.elliptical:
            returnTrueAnomaly = Vector2.Angle(eccentricity, position);
            returnTrueAnomaly = MiscHelperFuncs.convertToRadians(returnTrueAnomaly);
            if (clockwise)                                                          //Copied this section from the hyperbolic section, I havent fully tested it but it looks ok
            {                                                                       //
                if (towardsPerigee)                                                 //
                {
                    returnTrueAnomaly = Math.Abs(returnTrueAnomaly);                //
                }
                else
                {
                    returnTrueAnomaly = -Math.Abs(returnTrueAnomaly);               //
                }
            }
            else
            {
                if (towardsPerigee)
                {
                    returnTrueAnomaly = -Math.Abs(returnTrueAnomaly);               //
                }
                else
                {
                    returnTrueAnomaly = Math.Abs(returnTrueAnomaly);                //
                }
            }                                                                       //End

            break;

        case OrbitTypes.parabolic:
            break;

        case OrbitTypes.hyperbolic:
            returnTrueAnomaly = Vector2.Angle(eccentricity, position);
            returnTrueAnomaly = MiscHelperFuncs.convertToRadians(returnTrueAnomaly);
            if (clockwise)
            {
                if (towardsPerigee)
                {
                    returnTrueAnomaly = Math.Abs(returnTrueAnomaly);
                }
                else
                {
                    returnTrueAnomaly = -Math.Abs(returnTrueAnomaly);
                }
            }
            else
            {
                if (towardsPerigee)
                {
                    returnTrueAnomaly = -Math.Abs(returnTrueAnomaly);
                }
                else
                {
                    returnTrueAnomaly = Math.Abs(returnTrueAnomaly);
                }
            }
            break;
        }

        return(returnTrueAnomaly);
    }