- Home /
Need help rendering Orbits, with Orbital Elements
Hi there!
Currently I'm working a space-related project, which includes the solar system. I have run into a bit of halt. I want to show the orbits of the major bodies in our solar system, but I do not need to simulate velocity or gravity or anything like that. I just want the path rendered out in the scene (im using Kerbal space programs "static" body orbits as inspiration).
I do have some ellipse rendering code here, but i really need some help or tips how to use orbital elements to show the orbits. Instead of x,y,z,angle, i want to use the 6 keplarian elements.
Any help is appreciated! :)
public class OrbitHandler : MonoBehaviour {
public Vector3 radius = new Vector3(1f, 1f, 1f);
public float rotationAngle = 45;
public int resolution = 500;
private Vector3[] positions;
private LineRenderer lr;
void OnValidate()
{
UpdateEllipse();
}
public void UpdateEllipse()
{
if (lr == null)
lr = GetComponent<LineRenderer>();
lr.positionCount = resolution + 3;
AddPointToLineRenderer(0f, 0);
for (int i = 1; i <= resolution + 1; i++)
{
AddPointToLineRenderer((float)i / (float)(resolution) * 2.0f * Mathf.PI, i);
}
AddPointToLineRenderer(0f, resolution + 2);
}
void AddPointToLineRenderer(float angle, int index)
{
Quaternion pointQuaternion = Quaternion.AngleAxis(rotationAngle, Vector3.forward);
Vector3 pointPosition;
pointPosition = new Vector3(radius.x * Mathf.Cos(angle), radius.z * Mathf.Sin(angle), radius.y * Mathf.Sin(angle));
pointPosition = pointQuaternion * pointPosition;
lr.SetPosition(index, pointPosition);
}
}
Ohh that looks good, thank you ill check it out!
Answer by Generalfil · Jun 01, 2018 at 02:12 PM
Hi there i have been able to get a code going but i cant figure out the correct true anomaly, which seems to be the main problem. If anyone can help i would be very thankful! Attaching my code
public void UpdateEllipse()
{
lr.positionCount = resolution + 2;
lr.SetPosition(0,AddPointToLineRenderer( 0));
for (int i = 1; i <= resolution + 1; i++)
{
lr.SetPosition(i, AddPointToLineRenderer(i*Mathf.Deg2Rad));
}
}
Vector3 AddPointToLineRenderer(float index)
{
// pointQuaternion = Quaternion.AngleAxis(rotationAngle, Vector3.forward);
Vector3 pointPosition;
pointPosition = KeplerToCarthesian(index * Mathf.Rad2Deg, 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 , float a, float e, float w, float lan, float inc)
{
//Fix so inlication calculates properly
inc *= Mathf.Deg2Rad;
//Gets E and True Anomaly
double E = GetEccentricAnomaly(meanAnomaly, e);
double T = GetTrueAnomaly(e, E);
double r = a * (1 - e * Math.Cos(E));
double X = r * (Math.Cos(lan) * Math.Cos(w + T) - Math.Sin(lan) * Math.Sin(w + T) * Math.Cos(inc));
double Y = r * (Math.Sin(lan) * Math.Cos(w + T) + Math.Cos(lan) * Math.Sin(w + T) * Math.Cos(inc));
double Z = r * (Math.Sin(inc) * Math.Sin(w + T));
return new Vector3((float)X *10, (float) Z*10, (float) Y*10);
}
private double GetTrueAnomaly(float e, double E)
{
int dp = 6;
double S = Math.Sin(E);
double C = Math.Cos(E);
double fak = Math.Sqrt(1.0 - e * e);
double phi = Math.Atan2(fak * S, C - e) / Mathf.Deg2Rad;
return Math.Round(phi * Math.Pow(10, dp)) / Math.Pow(10, dp);
}
private double GetEccentricAnomaly(double meanAnomaly, float 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);
}
}
Your answer
Follow this Question
Related Questions
How to build a car script that allows for car to turn in full circles 1 Answer
Clamp a Vector above minimum angle from another Vector? 0 Answers
"StateMachineBehaviour" Not working for sub sates ? 0 Answers
How can I change ApiCompatibilityLevel before Unity start? 0 Answers
How to add feature to Standard.Shader? 0 Answers