- Home /
Mathf.Sin only returning positive values?
I'm using Orbital Elements to generate some planets and their orbits in Unity 2019.2.8f1, and it seems to work fine. However, when I try to use my functions to draw the orbit using the LineRenderer, Mathf.Sin only returns positive values.
void FindOrbitPath(int num)
{
Vector3[] points = new Vector3[num];
for(int i = 0; i < num; i++)
{
float tic = i * planet.OrbitalPeriod / num;
Vector4 Anoms = FindAnomaly(tic);
float tAnomaly = Anoms[2];
float eAnomaly = Anoms[1];
float radius = planet.SemimajorAxis * (1 - planet.Eccentricity * Mathf.Cos(Mathf.Deg2Rad *
eAnomaly));
float x = Mathf.Cos(Mathf.Deg2Rad * tAnomaly) * radius;
float z = Mathf.Sin(Mathf.Deg2Rad * tAnomaly) * radius;
Vector3 point = new Vector3(x, 0, z);
points[i] = point;
Debug.Log("Anomalies :" + Anoms.ToString() + " Radius :" + radius.ToString() +
" | Tic: " + tic.ToString());
}
LR.positionCount = num;
LR.SetPositions(points);
}
This is apparently the trouble function. True Anomaly appears to be returning correctly, but my "z" value almost always returns a positive when it should be negative. I'll include the FindAnomaly() function below as well.
Vector4 FindAnomaly(float t) //returns: MeanAnomaly, EccentricAnomaly, TrueAnomaly, DeltaTrueAnomaly
{
float mean_anomaly = 0;
float eccentric_anomaly = 0;
float true_anomaly = TrueAnomaly;
float d_true = 0;
mean_anomaly = planet.MeanAnomaly + meanAngularMotion * (t);
eccentric_anomaly = mean_anomaly + (planet.Eccentricity - (1 / 8) * Mathf.Pow(planet.Eccentricity, 3)) *
Mathf.Sin(Mathf.Deg2Rad * mean_anomaly);
float NewTrueAnomaly = Mathf.Acos((Mathf.Cos(Mathf.Deg2Rad * eccentric_anomaly) - planet.Eccentricity) /
(1 - planet.Eccentricity * Mathf.Cos(Mathf.Deg2Rad * eccentric_anomaly))) * Mathf.Rad2Deg;
if (NewTrueAnomaly > true_anomaly)
d_true = NewTrueAnomaly - true_anomaly;
else
d_true = true_anomaly - NewTrueAnomaly;
true_anomaly = NewTrueAnomaly;
Vector4 results = new Vector4(mean_anomaly, eccentric_anomaly, true_anomaly, d_true);
return results;
}
Answer by DehLeprechaun · Jan 03, 2020 at 05:01 AM
I have apparently solved the problem by using the following equation:
NewTrueAnomaly = 2 * Mathf.Atan(Mathf.Sqrt((1 - planet.Eccentricity * Mathf.Deg2Rad) / (1 - planet.Eccentricity *
Mathf.Deg2Rad)) * Mathf.Tan(eccentric_anomaly * Mathf.Deg2Rad / 2)) * Mathf.Rad2Deg;
And by using a more appropriate time system that keeps things more consistent. Things seem to be working alright now.
Answer by Bunny83 · Jan 03, 2020 at 12:22 AM
How did you come to this conclusion? Unity just uses the math functions of the .NET / Mono framework which directly map to the trigonometric methods implemented in the FPU / CPU hardware. You can be sure that Mathf.Sin does work exactly as it should be. If you do not get any negative values you simply do not pass in any angles which would cause a negative value. So your angle is between 0 - 180
and never between 180 - 360
or -180 - 0
which would return a negative value.
Actually, now that you mention it. I think my true anomaly calculation is set up to only return positives ... Actually, that's just the delta calc. I'll need to analyze my outputs
You were correct, after I checked my true anomaly output, it was swinging between 0 - 180.