- #1
Bane1998
- 11
- 0
I've coded a physics simulator that simulates the solar system. Essentially it just calculates gravity between bodies and attracts them together, the most basic simulation.
But obviously just feeding in positions for the sun and the planets will just have them all fall into directly each other. So I need to seed them in their proper orbits with the correct mass, position, and velocity. With those parameters I expect the orbits to behave correctly if my gravity calculations are correct.
For now I just place each planet at a random position on a plane with the sun at the center, at a distance equal to the orbital semimajor axis for that planet. I give it the correct mass, and I calculate a circular orbit speed via:
// v = sqrt(((m2 * m2) * g) / ((m1 + m2) * r)
// where:
// v == orbital velocity
// m1 == orbiter mass
// m2 == orbited mass
// g == gravitational constant
// r == distance between objects
Sending it off at that speed in a direction perpendicular to it's vector from the sun.
I've tested this with the Earth and a space-shuttle sized object, and I see a steady almost perfectly circular orbit. It's barely elliptical I assume due to rounding errors of floating points and negligible effects of gravity applied to the Earth and what not. But it's close enough I'd call it circular, so I've verified my basic simulation works.
Now however I want to seed it with more realistic elliptical orbits. I've done a lot more digging on the subject and the closest I can find to a solution is combining:
from http://en.wikipedia.org/wiki/Orbital_mechanics
and
μ = Gm1 + Gm2
from http://en.wikipedia.org/wiki/Standard_gravitational_parameter
into:
// v = sqrt(((g * m1) + (g * m2)) * ((2 / r) - (1 / a)))
// where:
// v == orbital velocity
// g == gravitational constant
// m1 == orbiter mass
// m2 == orbited mass
// r == distance between objects
// a == length of semi-major axis
But not only does it not quite seem to work for anything but perfectly circular orbits, I don't see how it possibly COULD work, given it seems to be missing inputs, like the eccentricity or the semi-minor axis.
By the way, I don't want to just cheat and look up the current position and speed of the planets because I am rolling this into a game where I want to randomly generate planetary systems with sensical input parameters(random reasonable ranges) about the orbits.
So, I am obviously not understanding how to do this. Any help would be appreciated. I know if I had a better understanding of the basics and ellipses I could probably derive a solution, but I'm a little bit over my head perhaps. There seems to be lots of information about finding the position on an ellipse given the angle, but I can't figure out how to turn this into a velocity like I need.
Sorry for the long and probably overly-verbose post. I hate even asking, I usually just spend days crawling wikipedia and struggling through it eventually coming up with an answer and then working out backwards why it works, but that doesn't seem to be working in this case for me.
(Was just looking around, this might be the wrong place for this actually. I apologize if so, not sure if this qualifies as a homework type question or not. I'll happily delete and repost in a more appropriate forum if needed.)
But obviously just feeding in positions for the sun and the planets will just have them all fall into directly each other. So I need to seed them in their proper orbits with the correct mass, position, and velocity. With those parameters I expect the orbits to behave correctly if my gravity calculations are correct.
For now I just place each planet at a random position on a plane with the sun at the center, at a distance equal to the orbital semimajor axis for that planet. I give it the correct mass, and I calculate a circular orbit speed via:
// v = sqrt(((m2 * m2) * g) / ((m1 + m2) * r)
// where:
// v == orbital velocity
// m1 == orbiter mass
// m2 == orbited mass
// g == gravitational constant
// r == distance between objects
Sending it off at that speed in a direction perpendicular to it's vector from the sun.
I've tested this with the Earth and a space-shuttle sized object, and I see a steady almost perfectly circular orbit. It's barely elliptical I assume due to rounding errors of floating points and negligible effects of gravity applied to the Earth and what not. But it's close enough I'd call it circular, so I've verified my basic simulation works.
Now however I want to seed it with more realistic elliptical orbits. I've done a lot more digging on the subject and the closest I can find to a solution is combining:
from http://en.wikipedia.org/wiki/Orbital_mechanics
and
μ = Gm1 + Gm2
from http://en.wikipedia.org/wiki/Standard_gravitational_parameter
into:
// v = sqrt(((g * m1) + (g * m2)) * ((2 / r) - (1 / a)))
// where:
// v == orbital velocity
// g == gravitational constant
// m1 == orbiter mass
// m2 == orbited mass
// r == distance between objects
// a == length of semi-major axis
But not only does it not quite seem to work for anything but perfectly circular orbits, I don't see how it possibly COULD work, given it seems to be missing inputs, like the eccentricity or the semi-minor axis.
By the way, I don't want to just cheat and look up the current position and speed of the planets because I am rolling this into a game where I want to randomly generate planetary systems with sensical input parameters(random reasonable ranges) about the orbits.
So, I am obviously not understanding how to do this. Any help would be appreciated. I know if I had a better understanding of the basics and ellipses I could probably derive a solution, but I'm a little bit over my head perhaps. There seems to be lots of information about finding the position on an ellipse given the angle, but I can't figure out how to turn this into a velocity like I need.
Sorry for the long and probably overly-verbose post. I hate even asking, I usually just spend days crawling wikipedia and struggling through it eventually coming up with an answer and then working out backwards why it works, but that doesn't seem to be working in this case for me.
(Was just looking around, this might be the wrong place for this actually. I apologize if so, not sure if this qualifies as a homework type question or not. I'll happily delete and repost in a more appropriate forum if needed.)
Last edited: