- Home /

**Question**by DehLeprechaun · Jan 02, 2020 at 10:51 PM · scripting problembug-perhapsorbittrigonometry

# 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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Need help orbiting an object around another object based on rotation 1 Answer

1 out of 4 objects with the same script that reference the same transform list returning null? 1 Answer

How to create a changing orbit from elliptical to planar and back? 0 Answers

Script works in editor, but does not work in build (Android) 1 Answer