- #1
whatisreality
- 290
- 1
I am really, really stuck! I am trying to write java code to model the solar system, or at least currently just earth, the sun and mercury. I've made a big mistake somewhere, the planets are very unstable and either move in a line or spiral inwards! I just wanted to check that I'm doing things in the right order, I have so many loops.
I've set the sun to start at the origin, and the Earth's position is
$$x = 1.496 \times 10^{11}m$$ and Mercury is at $$x = 5.971 \times 10^{10}m$$
I want everything to be done in the centre of mass frame, so I then calculate the position of the centre of mass and subtract it from the initial positions, e.g. so the sun is at x - centreOfMass.
The velocity of each body is given by $$v = \frac{2\pi r}{T}$$ This is probably where I go wrong. I know the orbital period of mercury and Earth around the sun, so I can calculate their velocities around the centre of mass. But the sun will also move, even if only a tiny, tiny bit, and I don't have its orbital period around the centre of mass! So how can I work out its velocity?
Assuming I have the sun's velocity around the centre of mass, I then work out the velocity of the centre of mass. Because I want to stay in the centre of mass frame, I then subtract this velocity from the velocities of the planets.
My code is then written so that the position is recalculated, given a time step, then the centre of mass position is recalculated and subtracted from the new position. A new velocity for each planet is calculated, then the centre of mass velocity is recalculated and again subtracted from each velocity.
Is this the correct procedure? Do I need to recalculate the centre of mass position/velocity after each time step, or is that the problem?
I've set the sun to start at the origin, and the Earth's position is
$$x = 1.496 \times 10^{11}m$$ and Mercury is at $$x = 5.971 \times 10^{10}m$$
I want everything to be done in the centre of mass frame, so I then calculate the position of the centre of mass and subtract it from the initial positions, e.g. so the sun is at x - centreOfMass.
The velocity of each body is given by $$v = \frac{2\pi r}{T}$$ This is probably where I go wrong. I know the orbital period of mercury and Earth around the sun, so I can calculate their velocities around the centre of mass. But the sun will also move, even if only a tiny, tiny bit, and I don't have its orbital period around the centre of mass! So how can I work out its velocity?
Assuming I have the sun's velocity around the centre of mass, I then work out the velocity of the centre of mass. Because I want to stay in the centre of mass frame, I then subtract this velocity from the velocities of the planets.
My code is then written so that the position is recalculated, given a time step, then the centre of mass position is recalculated and subtracted from the new position. A new velocity for each planet is calculated, then the centre of mass velocity is recalculated and again subtracted from each velocity.
Is this the correct procedure? Do I need to recalculate the centre of mass position/velocity after each time step, or is that the problem?