public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
    {
        smoothTime = MathD.Max(0.0001d, smoothTime);
        double num1 = 2d / smoothTime;
        double num2 = num1 * deltaTime;
        double num3 = (1.0d / (1.0d + num2 + 0.479999989271164d * num2 * num2 + 0.234999999403954d * num2 * num2 * num2));
        double num4 = current - target;
        double num5 = target;
        double max  = maxSpeed * smoothTime;
        double num6 = MathD.Clamp(num4, -max, max);

        target = current - num6;
        double num7 = (currentVelocity + num1 * num6) * deltaTime;

        currentVelocity = (currentVelocity - num1 * num7) * num3;
        double num8 = target + (num6 + num7) * num3;

        if (num5 - current > 0.0 == num8 > num5)
        {
            num8            = num5;
            currentVelocity = (num8 - num5) / deltaTime;
        }
        return(num8);
    }
예제 #2
0
    // This method calculates part values such as mass, lift, drag and connection forces, as well as all intermediates.
    public void CalculateAerodynamicValues(bool doInteraction = true)
    {
        // Calculate intemediate values
        //print(part.name + ": Calc Aero values");
        b_2 = (double)tipPosition.z - (double)Root.localPosition.z + 1.0;

        MAC = ((double)tipScale.x + (double)rootScale.x + 2.0) * (double)modelChordLenght / 2.0;

        midChordSweep = (MathD.Rad2Deg * Math.Atan(((double)Root.localPosition.x - (double)tipPosition.x) / b_2));

        taperRatio = ((double)tipScale.x + 1.0) / ((double)rootScale.x + 1.0);

        surfaceArea = MAC * b_2;

        aspectRatio = 2.0 * b_2 / MAC;

        ArSweepScale = Math.Pow(aspectRatio / MathD.Cos(MathD.Deg2Rad * midChordSweep), 2.0) + 4.0;
        ArSweepScale = 2.0 + Math.Sqrt(ArSweepScale);
        ArSweepScale = (2.0 * MathD.PI) / ArSweepScale * aspectRatio;

        wingMass = MathD.Clamp((double)massFudgeNumber * surfaceArea * ((ArSweepScale * 2.0) / (3.0 + ArSweepScale)) * ((1.0 + taperRatio) / 2), 0.01, double.MaxValue);

        Cd = (double)dragBaseValue / ArSweepScale * (double)dragMultiplier;

        Cl = (double)liftFudgeNumber * surfaceArea * ArSweepScale;

        //print("Gather Children");
        GatherChildrenCl();

        connectionForce = MathD.Round(MathD.Clamp(MathD.Sqrt(Cl + ChildrenCl) * (double)connectionFactor, (double)connectionMinimum, double.MaxValue));

        // Values always set
        if (isWing)
        {
            wingCost = (float)wingMass * (1f + (float)ArSweepScale / 4f) * costDensity;
            wingCost = Mathf.Round(wingCost / 5f) * 5f;
        }
        else if (isCtrlSrf)
        {
            wingCost  = (float)wingMass * (1f + (float)ArSweepScale / 4f) * costDensity * (1f - modelControlSurfaceFraction);
            wingCost += (float)wingMass * (1f + (float)ArSweepScale / 4f) * costDensityControl * modelControlSurfaceFraction;
            wingCost  = Mathf.Round(wingCost / 5f) * 5f;
        }

        part.breakingForce  = Mathf.Round((float)connectionForce);
        part.breakingTorque = Mathf.Round((float)connectionForce);

        // Stock-only values
        if (!FARactive)
        {
            // numbers for lift from: http://forum.kerbalspaceprogram.com/threads/118839-Updating-Parts-to-1-0?p=1896409&viewfull=1#post1896409
            float stockLiftCoefficient = (float)(surfaceArea / 3.52);
            // CoL/P matches CoM unless otherwise specified
            part.CoMOffset = new Vector3(Vector3.Dot(Tip.position - Root.position, part.transform.right) / 2, Vector3.Dot(Tip.position - Root.position, part.transform.up) / 2, 0);
            if (isWing && !isCtrlSrf)
            {
                part.Modules.GetModules <ModuleLiftingSurface>().FirstOrDefault().deflectionLiftCoeff = stockLiftCoefficient;
                part.mass = stockLiftCoefficient * 0.1f;
            }
            else
            {
                ModuleControlSurface mCtrlSrf = part.Modules.OfType <ModuleControlSurface>().FirstOrDefault();
                if (mCtrlSrf != null)
                {
                    mCtrlSrf.deflectionLiftCoeff = stockLiftCoefficient;
                    mCtrlSrf.ctrlSurfaceArea     = modelControlSurfaceFraction;
                    part.mass = stockLiftCoefficient * (1 + modelControlSurfaceFraction) * 0.1f;
                }
            }
        }

        // FAR values
        // With reflection stuff from r4m0n
        if (FARactive)
        {
            if (part.Modules.Contains("FARControllableSurface"))
            {
                PartModule FARmodule = part.Modules["FARControllableSurface"];
                Type       FARtype   = FARmodule.GetType();
                FARtype.GetField("b_2").SetValue(FARmodule, b_2);
                FARtype.GetField("b_2_actual").SetValue(FARmodule, b_2);
                FARtype.GetField("MAC").SetValue(FARmodule, MAC);
                FARtype.GetField("MAC_actual").SetValue(FARmodule, MAC);
                FARtype.GetField("S").SetValue(FARmodule, surfaceArea);
                FARtype.GetField("MidChordSweep").SetValue(FARmodule, midChordSweep);
                FARtype.GetField("TaperRatio").SetValue(FARmodule, taperRatio);
                FARtype.GetField("ctrlSurfFrac").SetValue(FARmodule, modelControlSurfaceFraction);
                //print("Set fields");
            }
            else if (part.Modules.Contains("FARWingAerodynamicModel"))
            {
                PartModule FARmodule = part.Modules["FARWingAerodynamicModel"];
                Type       FARtype   = FARmodule.GetType();
                FARtype.GetField("b_2").SetValue(FARmodule, b_2);
                FARtype.GetField("b_2_actual").SetValue(FARmodule, b_2);
                FARtype.GetField("MAC").SetValue(FARmodule, MAC);
                FARtype.GetField("MAC_actual").SetValue(FARmodule, MAC);
                FARtype.GetField("S").SetValue(FARmodule, surfaceArea);
                FARtype.GetField("MidChordSweep").SetValue(FARmodule, midChordSweep);
                FARtype.GetField("TaperRatio").SetValue(FARmodule, taperRatio);
            }
            if (!triggerUpdate && doInteraction)
            {
                TriggerUpdateAllWings();
            }
            if (doInteraction)
            {
                triggerUpdate = false;
            }
        }
        //print("FAR Done");
        // Update GUI values
        if (!FARactive)
        {
            guiCd       = Mathf.Round((float)Cd * 100f) / 100f;
            guiCl       = Mathf.Round((float)Cl * 100f) / 100f;
            guiWingMass = part.mass;
        }

        guiMAC           = (float)MAC;
        guiB_2           = (float)b_2;
        guiMidChordSweep = (float)midChordSweep;
        guiTaperRatio    = (float)taperRatio;
        guiSurfaceArea   = (float)surfaceArea;
        guiAspectRatio   = (float)aspectRatio;

        StartCoroutine(updateAeroDelayed());
    }