Cant get a stable planet orbit...

Started by
14 comments, last by SiCrane 13 years ago
D3DXVECTOR3 dir = planet.pos - star.pos;

float d = D3DXVec3Length(&dir);

float dt = (float)period * timescale;

D3DXVec3Normalize(&dir, &dir);



planet.acc *= 0.0f;

planet.acc = -g*(star.mass)/(d*d)*dir;



planet.pos += planet.vel*dt + planet.acc*.5f*dt*dt;

planet.vel += planet.acc*dt;




I cant seem to create a stable orbit. Can someone tell me what I'm missing?
Advertisement
What are your initial conditions (i.e. distance from the star and velocity of the planet)?
If they are stable in theory, you could try a better integration method (e.g. Runge Kutta)
It does seem that your integration method is at fault. v*dt + .5*a*dt*dt is only appropriate if acceleration is constant. However, in your case, acceleration is non-constant because it depends on position.
I don't know what features you need, but if you are just looking to simulate a few non-interacting planets, you can compute the ellipses that the the planets follow using Kepler's laws of planetary motion. Then just move the planets around the ellipses. That way you will never run into stability problems caused by floating point or step size.
You need a Symplectic Integrator to keep from losing or gaining energy in your orbits
WHOO! :D

I have been working on this myself on my own project :)

I will help you out.

--------- Your Code -------------
D3DXVECTOR3 dir = planet.pos - star.pos;

float d = D3DXVec3Length(&dir);
float dt = (float)period * timescale;

D3DXVec3Normalize(&dir, &dir);

planet.acc *= 0.0f;
planet.acc = -g*(star.mass)/(d*d)*dir;

planet.pos += planet.vel*dt + planet.acc*.5f*dt*dt;
planet.vel += planet.acc*dt;
My code is very similar but does not use the vector from D3DX.

First I calculate force:


for i in PlanetList:
ydist = (sun.x - i.y)
xdist = (sun.y - i.x)

i.allforce = (grav * sun.mass * i.mass)/(ydist**2+xdist**2)

if math.sqrt(ydist*ydist+xdist*xdist) > (i.rads + sun.rads): # Make sure the circles are not in each other.
i.yforce = i.yforce + math.sin(math.atan2(ydist, xdist))*i.allforce
i.xforce = i.xforce + math.cos(math.atan2(ydist, xdist))*i.allforce


Then I apply that force per object to move the objects.


def move(self, yforce, xforce):
if self.mass == 0:
self.mass = 0.00001
self.time = self.time + 0.1
self.yspd = self.yspd + (yforce/self.mass)
self.xspd = self.xspd + (xforce/self.mass)

self.y = self.y + (self.yspd) + (0.5 * (yforce/self.mass)*.1**2)
self.x = self.x + (self.xspd) + (0.5 * (xforce/self.mass)*.1**2)


works pretty well for me. Good luck :)

My solution has been to create an invisible child pivot of a central pivot inside a star and move it out by a ceratin amount. Then by rotating the central pivot the child pivot rotates around the centre and by increasing the distance depending on the angle I can then get elliptical orbits. I then get the position of the child pivot and multiply it by the distance I want the planet to be and use this data to place my planet. The reason I have done it this way is because it is really fast, highly accurate and I can zoom forward or backward in time by any amount I want and know exactly where the planet will be without resorting to complex algorithms that I may make mistakes with. :cool: It works
Change to:


D3DXVECTOR3 dir = planet.pos - star.pos;

float d = D3DXVec3Length(&dir);

float dt = (float)period * timescale;

D3DXVec3Normalize(&dir, &dir);

planet.vel += planet.acc*dt*0.5;

planet.acc = -g*(star.mass)/(d*d)*dir;

planet.vel += planet.acc*dt*0.5;

planet.pos += planet.vel*dt + planet.acc*.5f*dt*dt;



Now you're using the velocity verlet symplectic integration algrithm. It's untested but should work. Have fun!

Cheers,
Mike

It does seem that your integration method is at fault. v*dt + .5*a*dt*dt is only appropriate if acceleration is constant. However, in your case, acceleration is non-constant because it depends on position.


Oh come on, man! You know perfectly well that using constant accelleration integration algorithms in small discrete timesteps can yield very good results, even for problems containing non-constant acceleration. Only a handfull of physics problems contains constant acceleration, and millions don't.

Cheers,
Mike
That objection would make a lot more sense if you didn't yourself just suggest using a different integration method.

This topic is closed to new replies.

Advertisement