- #1
SethFusion
- 7
- 0
Homework Statement:: I'm working on a personal project to convert objects from a simulation using state vectors for position and velocity to Keplerian orbital elements (semimajor axis, eccentricity, argument of periapsis, etc.). However, the equations I am using do not calculate the eccentricity correctly for binary objects.
Relevant Equations:: mu = gravitation parameter * mass of parent body
momentum = position x velocity
eccentricity = ((velocity x momentum) / mu) - (position / positionMagnitude)
where velocity, mommentum, and position are vectors.
Specifically I have been using this short paper:
https://downloads.rene-schwarz.com/...State_Vectors_to_Keplerian_Orbit_Elements.pdf
Now, I've been working on this on and off for a while, so thank you for taking the time to read through this. Hopefully someone can give me some insight as to what the problem is, because I'm a computer science major, not a physics major.
The program I am working on converts objects from a program called Universe Sandbox (US) into objects for another program called Space Engine (SE). More specifically, this is for solar systems, real or imagined, and deals with stars, planets, moons, and any celestial objects. US is a simulator and stores information about objects using cartesian state vectors, which can be exported to represent a snapshot of the simulation. SE is an emulator and only uses simple Keplerian elements.
The paper I linked above has worked near-perfect so far and I have had few problems. But eccentricity is not calculated correctly for any binary relationships, and I'm not sure why. Here is the relevant code:
Let me give a little context for this code: What happens here is that every object in a solar system is connected through vectors of "children" and every object has a parent. For instance, a star would be at the top of the hierarchy and a moon would be at the bottom. Objects are sent through this CalcOrbit function from the bottom of the hierarchy (this is why the parent's position/velocity are subtracted from the object's), and their Keplerian elements are calculated as necessary.
Just to be clear, this equation works fine for most objects. But when an object passes through that is in a binary relationship, its "parent" is the barycenter between it and its partner. The barycenter has a total mass of the combined mass of the two objects. When eccentricity is calculated, it often comes out wildly incorrect (often around 0.9).
So I have tried to use other formulas, but had no success (I may have implemented them wrong, though). I have also tried changing the value of mu to its partner's mass, the barycenter's parent's mass, and other values, but none have worked. Unfortunately, I think I don't have the background in physics needed to know what the actual problem is, which is why I'm posting here. Is this problem even possible? Or are binary relationships too complicated to derive an eccentricity from a single snapshot of position/velocity?
If anyone has any ideas about why this doesn't work, or if I haven't provided enough context, please let me know.
The full code can be found here: https://github.com/SethFusion/US2-to-SE-Converter
But be warned that most of the program is dedicated to identifying objects and their relationships, and much of it I didn't write myself. The relevant part will be the full CalcOrbit function around line 353.
Also, I apologize if this is not the correct forum to post this in. I was afraid there was too much code to post in the regular physics forum. Mods, please feel free to move this to the correct forum or to contact me about where would be the correct place for this post.
Relevant Equations:: mu = gravitation parameter * mass of parent body
momentum = position x velocity
eccentricity = ((velocity x momentum) / mu) - (position / positionMagnitude)
where velocity, mommentum, and position are vectors.
Specifically I have been using this short paper:
https://downloads.rene-schwarz.com/...State_Vectors_to_Keplerian_Orbit_Elements.pdf
Now, I've been working on this on and off for a while, so thank you for taking the time to read through this. Hopefully someone can give me some insight as to what the problem is, because I'm a computer science major, not a physics major.
The program I am working on converts objects from a program called Universe Sandbox (US) into objects for another program called Space Engine (SE). More specifically, this is for solar systems, real or imagined, and deals with stars, planets, moons, and any celestial objects. US is a simulator and stores information about objects using cartesian state vectors, which can be exported to represent a snapshot of the simulation. SE is an emulator and only uses simple Keplerian elements.
The paper I linked above has worked near-perfect so far and I have had few problems. But eccentricity is not calculated correctly for any binary relationships, and I'm not sure why. Here is the relevant code:
C++:
void CalcOrbit(Object& obj)
{
for (int i = 0; i < obj.child.size(); i++)
CalcOrbit(*obj.child.at(i));
if (&obj == root)
return;
double mu = G * obj.parent->mass;
obj.position = Subtract(obj.position, obj.parent->position);
obj.velocity = Subtract(obj.velocity, obj.parent->velocity);
StateVect momentVect;
// calculate the momentum vector h
momentVect = CrossProduct(obj.position, obj.velocity);
.
.
.
// calculate eccentricity vector and eccentricity
StateVect eccentVect, VcrossH = CrossProduct(obj.velocity, momentVect);
eccentVect.x = ((VcrossH.x / mu) - (obj.position.x / obj.position.magnitude()));
eccentVect.y = ((VcrossH.y / mu) - (obj.position.y / obj.position.magnitude()));
eccentVect.z = ((VcrossH.z / mu) - (obj.position.z / obj.position.magnitude()));
obj.eccentricity = eccentVect.magnitude();
.
.
.
}
Just to be clear, this equation works fine for most objects. But when an object passes through that is in a binary relationship, its "parent" is the barycenter between it and its partner. The barycenter has a total mass of the combined mass of the two objects. When eccentricity is calculated, it often comes out wildly incorrect (often around 0.9).
So I have tried to use other formulas, but had no success (I may have implemented them wrong, though). I have also tried changing the value of mu to its partner's mass, the barycenter's parent's mass, and other values, but none have worked. Unfortunately, I think I don't have the background in physics needed to know what the actual problem is, which is why I'm posting here. Is this problem even possible? Or are binary relationships too complicated to derive an eccentricity from a single snapshot of position/velocity?
If anyone has any ideas about why this doesn't work, or if I haven't provided enough context, please let me know.
The full code can be found here: https://github.com/SethFusion/US2-to-SE-Converter
But be warned that most of the program is dedicated to identifying objects and their relationships, and much of it I didn't write myself. The relevant part will be the full CalcOrbit function around line 353.
Also, I apologize if this is not the correct forum to post this in. I was afraid there was too much code to post in the regular physics forum. Mods, please feel free to move this to the correct forum or to contact me about where would be the correct place for this post.