For each planet/star have at least position, velocity and mass. Also have a gravitational constant, G (just arbitrarily set this to something that works).
Then you can update the objects like so (pseudocode):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| for i=1 to n: for j = 1 to n: if i==j: continue end dist = sqrt((planet[i].x - planet[j].x)^2 + (planet[i].y - planet[j].y)^2) accMag = G*planet[j].mass/(dist^2) accX = accMag*(planet[j].x - planet[i].x)/dist accY = accMag*(planet[j].y - planet[i].y)/dist planet[i].vx += accX planet[i].vy += accY end planet[i].x += planet[i].vx planet[i].y += planet[i].vy end |
dist = distance between the planets, accMag = acceleration magnitude, accX = x component of the acceleration, vx = x velocity
Remember, planet i is the planet you are calculating the acceleration of, and planet j is the planet it is being attracted to.
Also, I'm not sure what you're doing already, but x and y need to be floating point numbers, not integers.
Check out these wikipedia articles too:
http://en.wikipedia.org/wiki/Orbital_mechanicshttp://en.wikipedia.org/wiki/Orbit_equationhttp://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation