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 just wanted to check that my actual physics equations are right, and my initial conditions!
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, 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 not need to recalculate the centre-of-mass position/velocity after each time step? I've made a big mistake somewhere; the planets are very unstable and either move in a line or spiral inwards!