# Need help with calculating state vectors from Orbital Elements - C#

I have been working on a project where I want to visualize the orbits in the solar system with the help of Unity Engine. I have been using different guides and similar questions on this forum to construct my code, and I'm closing in into the final solution but i have run into a halt.

When running my code, every variable seems to work properly except eccentricity, which ends up in creating visuals like this.

While running a simple codeblock to plot the 2D ellipse, create this which seemingly looks correct

Now this is the code, and if anyone could help me figure out what im missing in the 3D part i would appreciate it alot!

public void UpdateEllipse()
{
if (lr == null)
lr = GetComponent<LineRenderer>();

lr.positionCount = resolution + 2;

for (int i = 1; i <= resolution + 1; i++)
{
}
}

{
Vector3 pointPosition;

pointPosition = KeplerToCarthesian(index * Mathf.Deg2Rad , semiMajorAxis,eccentrity,argumentofP,longOfAccNode, inclination);

return pointPosition;
}

/// <summary>
/// Convert keplarian elements into vector3, needs (Mean Anomaly, Semi Majoris Axis, Eccentricity, Argument of Periapsis, Longitude of Ascending node, Inclination)
/// </summary>
/// <returns></returns>
Vector3 KeplerToCarthesian(double meanAnomaly , double a, double e, double w, double O, double inc)
{
//Fix so inlication calculates properly

//Gets E and True Anomaly
E = GetEccentricAnomaly(meanAnomaly, e);
T = GetTrueAnomaly(e, E) * Mathf.Deg2Rad;

r = a * (1 - e * Math.Cos(E));

Vector3 o = new Vector3((float)(r * Math.Cos(T)), (float)(r * Math.Sin(T)), 0);

double rx, ry, rz;
rx = o.x; ry = o.y; rz = o.z;

rx = (o.x * (Math.Cos(w) * Math.Cos(O) - Math.Sin(w) * Math.Cos(inc) * Math.Sin(O)) -
o.y * (Math.Sin(w) * Math.Cos(O) + Math.Cos(w) * Math.Cos(inc) * Math.Sin(O)));
ry = (o.x * (Math.Cos(w) * Math.Sin(O) + Math.Sin(w) * Math.Cos(inc) * Math.Cos(O)) +
o.y * (Math.Cos(w) * Math.Cos(inc) * Math.Cos(O) - Math.Sin(w) * Math.Sin(O)));
rz = (o.x * (Math.Sin(w) * Math.Sin(inc)) + o.y * (Math.Cos(w) * Math.Sin(inc)));

//2D Code
/*double C = Math.Cos(E);
double S = Math.Sin(E);

rx = r * (C - e);
ry = r * Math.Sqrt(1.0 - e * e) * S;
rz = 0;*/

return new Vector3((float)rx *10, (float) rz*10, (float) ry*10);
}

private double GetTrueAnomaly(double e, double E)
{
int dp = 6;
double phi = Math.Atan2(Math.Sqrt(1 - e) * Math.Cos(E / 2), Math.Sqrt(1 + e) * Math.Sin(E / 2)) / (Math.PI / 180);

return Math.Round(phi * Math.Pow(10, dp)) / Math.Pow(10, dp);
}

private double GetEccentricAnomaly(double meanAnomaly, double e)
{
//Solve kepler equation to get Ecentric anomaly
int tolerance = 6;
int maxIter = 30, i = 0;
double delta = Math.Pow(10, -tolerance);
double E, F;

meanAnomaly /= 360.0f;

meanAnomaly = 2.0 * Math.PI * (meanAnomaly - Math.Floor(meanAnomaly));

if (e < 0.8)
E = meanAnomaly;
else
E = Math.PI;

F = E - e * Math.Sin(meanAnomaly) - meanAnomaly;

while ((Math.Abs(F) > delta) && (i < maxIter))
{

E = E - F / (1.0 - e * Math.Cos(E));
F = E - e * Math.Sin(E) - meanAnomaly;
i++;
}

E /= (Math.PI / 180.0);

return Math.Round(E * Math.Pow(10, tolerance)) / Math.Pow(10, tolerance);
}
`
• This site really isn't for "what's wrong with my program" questions. Can you add a description of the math that you are using, and explain better what it is about your results that lead you to believe that it is wrong? You have specific values of Argument of Periapsis, Inclination, and Longitude of Ascending Node that should create an elliptical orbit tilted in 3D space, and these can look weird when viewed from some directions (see this answer). Maybe your program is working correctly! – uhoh Jun 5 '18 at 1:22
• @uhoh Sorry that was not my intention, however with your short answer i was able to figure out where i was missing something mathematically in my code. The faulty code was the distance to the central body (var r) was never calculated correctly leaving faulty behaviors when trying different eccentricity values. For anyone searching for my solution, this code helped: a = Semimajor Axis e = eccentricty T= True Anomaly r = a * ((1 - (e*e)) / (1 + e * Math.Cos(T))); – Generalfil Jun 5 '18 at 6:11
• Well that's good news, glad to hear it! Your question might end up being closed as off-topic. Don't worry too much about that, and if you've got further questions ask again. You could consider posting a short answer to your own question, and clicking accept Welcome to Stack Exchange! – uhoh Jun 5 '18 at 6:15
• Note that you might want to look into CSPICE, which has functions that do things like this: naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/conics_c.html – user7073 Jun 5 '18 at 17:33