• Advertisement
Sign in to follow this  

Cant get a stable planet orbit...

This topic is 2452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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;[/code]



I cant seem to create a stable orbit. Can someone tell me what I'm missing?

Share this post


Link to post
Share on other sites
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. [url="http://en.wikipedia.org/wiki/Runge-kutta"]Runge Kutta[/url])

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You need a [url="http://en.wikipedia.org/wiki/Symplectic_integrator"]Symplectic Integrator[/url] to keep from losing or gaining energy in your orbits

Share this post


Link to post
Share on other sites
WHOO! :D

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

I will help you out.

[code]--------- 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;[/code]My code is very similar but does not use the vector from D3DX.

First I calculate force:

[code]
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[/code]

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

[code]
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)
[/code]

works pretty well for me. Good luck :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Change to:

[code]
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;

[/code]

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

Cheers,
Mike

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1297805606' post='4774657']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
That objection would make a lot more sense if you didn't yourself just suggest using a different integration method.

Share this post


Link to post
Share on other sites
Sorry, but I don't see how. I argue that integration algorithms designed for constant accelleration can be used for solving problems containing non-constant accelleration (pretty much every real-life physics problem does that) and exemplifies my point through action by applying such an algorithm on a problem at hand. What's the contradicition?

Cheers,
Mike

Share this post


Link to post
Share on other sites
I remember a problem like this cropping up when a friend of mine at univesity was modelling rubidium electron orbits. The issue was non-conservation of energy due to inadequate integration, and the solution was applying a more suitable integration method, to be specific a Runge-Kutta algorithm as suggested in the first reply.

Share this post


Link to post
Share on other sites
i just want to add some things:

const float G = 6.674 * 0.000001;

GravittForce = G*(M*m) / (distance*distance);




You should know the force for the planet (forceA) that moves in one point and then apply to sim and run




look at this

[url="http://upload.wikimedia.org/wikipedia/commons/c/c9/Centripetal_force_diagram.svg"]http://upload.wikime...rce_diagram.svg[/url]







Velocity is forceA




centripetal force is gravitforce then







i have some not mine src code




[quote]

//---------------------------------------------------------------------------


#pragma hdrstop

#include "galaxy.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

/*
* Galaxy Collision in OpenGL by Andrey Mirtchovski (aam396@mail.usask.ca)
*/

//void __fastcall TGLgalaxy:: process_frame()
//{
//
// elapsedtime = GetTickCount() - lasttime;
//
//
//
//
//
//}




void __fastcall TGLgalaxy::movegals() {
lastcenter = center;
center.x = 0.0;
center.y = 0.0;
center.z = 0.0;
int i, j;
float ep_t_mass;

for(i = 0; i < n; i++) {
for(j = 0; j< n; j++) {

if(j == i)
continue;
//jezeli cos do siebie dobija to: :)
// co ma mniejsza mase = acc czyli przyspieszenia :) accumulation :)
x1 = s[i].pos.x - s[j].pos.x;
x2 = s[i].pos.y - s[j].pos.y;
x3 = s[i].pos.z - s[j].pos.z;

d = sqrt (x1*x1 + x2*x2 + x3*x3);


if(d > 10) //-0.001
ep_t_mass = -0.001*(s[j].mass/(d*d));
else
ep_t_mass = 0;
// ep_t_mass = +(G*s[j].mass/(d*d));// / 2.00f;



s[i].acc.x = ep_t_mass * x1;
s[i].acc.y = ep_t_mass * x2;
s[i].acc.z = ep_t_mass * x3;

s[i].vel.x += s[i].acc.x;
s[i].vel.y += s[i].acc.y;
s[i].vel.z += s[i].acc.z;

s[i].pos.x += s[i].vel.x;
s[i].pos.y += s[i].vel.y;
s[i].pos.z += s[i].vel.z;

}



center = vectors_add(center,s[i].pos);

center = vector_multiple(center,1.0f/float(n));
}


}




void __fastcall TGLgalaxy::move() {

int i, j, k;
float ep_t_mass;


for(j = 0; j < n; j++) {
for(i = 0; i < gal; i++) {
for(k = 0; k < n; k++) {

x1 = glx[j][i].pos.x - s[k].pos.x;
x2 = glx[j][i].pos.y - s[k].pos.y;
x3 = glx[j][i].pos.z - s[k].pos.z;

d = sqrt (x1*x1 + x2*x2 + x3*x3);

if(d > 10)
ep_t_mass = -ep*s[k].mass/(d*d);
else
ep_t_mass = 0;

glx[j][i].acc.x = ep_t_mass * x1;
glx[j][i].acc.y = ep_t_mass * x2;
glx[j][i].acc.z = ep_t_mass * x3;

glx[j][i].vel.x += glx[j][i].acc.x;
glx[j][i].vel.y += glx[j][i].acc.y;
glx[j][i].vel.z += glx[j][i].acc.z;

glx[j][i].pos.x += glx[j][i].vel.x;
glx[j][i].pos.y += glx[j][i].vel.y;
glx[j][i].pos.z += glx[j][i].vel.z;

}
}
}

}



void __fastcall TGLgalaxy::Draw()
{
int i, j;
// glPushMatrix();
// glRotatef(spin_x, 0, 1, 0);
// glRotatef(spin_y, 1, 0, 0);

/* calculate new star positions */
// move();
/* calculate new galaxy positions */
movegals();
/* draw stars */
glPushMatrix();
glTranslatef(-center.x,-center.y,-center.z);
// glTranslatef(0, 0, spin_z);



// glBegin(GL_POINTS);
// glColor3ub(0, 150, 150);
glColor3f(1,1,1);
glDepthMask(GL_FALSE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glBindTexture(GL_TEXTURE_2D,STAR_TEXTURE);

for(i = 0; i < n; i++) {
if (s[i].is_electron == true) glColor3f(0,0,1);
if (s[i].is_electron == false) glColor3f(1,1,1);

if (s[i].is_electron == true) {
t3dpoint ab = vectorAB(center,s[i].pos);
ab = Normalize(ab);

ab = vector_multiple(ab,3000.0f);

if (n3ddistance(center,s[i].pos) < 10 )
DrawbillBoard(s[i].pos.x, s[i].pos.y, s[i].pos.z,440.0f);
else
DrawbillBoard(center.x+ab.x, center.y+ab.y,center.z+ ab.z,440.0f);


}
if (s[i].is_electron == false) DrawbillBoard(s[i].pos.x, s[i].pos.y, s[i].pos.z,140.0f);
}



// for(i = 0; i < n; i++) {
// if ( i == 0) glColor3f(1,0,0);
// if ( i == 0) glColor3f(0,0,1);
// if ( i == 1) glColor3f(0,1,0);
// if ( i == 2) glColor3f(1,1,1);
// if ( i == 3) glColor3f(1,1,0);
// if ( i == 4) glColor3f(1,0,1);
// if ( i == 5) glColor3f(0,1,1);
//
// for(j = 0; j < gal; j++)
// DrawbillBoard(glx[i][j].pos.x, glx[i][j].pos.y, glx[i][j].pos.z,4.0f);
// }
glDisable(GL_BLEND);


glDisable(GL_TEXTURE_2D);
glDepthMask(GL_TRUE);
glPopMatrix();

}


/*
void
mouse(int button, int state, int x, int y)
{

switch(button) {
case 0:
old_x = x - spin_x;
old_y = y - spin_y;
break;
case 2:
old_y = y - spin_z;
move_z = (move_z ? 0 : 1);
}




}

*/
//
//void __fastcall TGLgalaxy::motion(x, y) {
//
// if(!move_z) {
// spin_x = x - old_x;
// spin_y = y - old_y;
// } else {
// spin_z = y - old_y;
// }
//
//
//}


void __fastcall TGLgalaxy::initgal() {
int i;


s = NULL;
glx = NULL;

s = (star *)malloc(sizeof(star) * n);
glx = (star **)malloc(sizeof(star *) * n);

for(i = 0; i < n; i++)
glx[i] = (star*)malloc(sizeof(star) * gal);

}
// case '4':
// n = 4;
// initgal();
// init();
// break;


void __fastcall TGLgalaxy::init() {

int i, j;
float dy, dx, dist;

for(i = 0; i < n; i++) {
s[i].pos.x = float(Random(1000));
// s[i].pos.x = 1000.0f-900.0f*s[i].pos.x;
s[i].pos.y = float(Random(1000));
// s[i].pos.y = 1000.0f-900.0f*s[i].pos.y;
s[i].pos.z = float(Random(1000));//(rand()%2 ? -1 : 1)*rand()%w/2;
// s[i].pos.z = 1000.0f-900.0f*s[i].pos.z;


s[i].is_electron = false;
s[i].is_electron= false;
s[i].is_proton= false;
s[i].is_neutron= false;



s[i].vel.x = 0;
s[i].vel.y = 0;
s[i].vel.z = 0;
s[i].acc.x = 0;
s[i].acc.y = 0;
s[i].acc.z = 0;
s[i].mass = 5000.0;
}



for(j = 0; j < n; j++) {
for(i = 0; i < gal; i++) {

glx[j][i].pos.x = s[j].pos.x + (rand()%2 ? -1 : 1) * rand()%w/4;
glx[j][i].pos.y = s[j].pos.y + (rand()%2 ? -1 : 1) * rand()%h/4;
glx[j][i].pos.z = s[j].pos.z + (rand()%2 ? -1 : 1) * rand()%w;

dist = sqrt( pow(glx[j][i].pos.x - s[j].pos.x, 2) +
pow(glx[j][i].pos.y - s[j].pos.y, 2) +
pow(glx[j][i].pos.z - s[j].pos.z, 2));

if(dist > w/6 || dist > h/6) { // n3ddistance( glx[1].pos, glx[2].pos )
i--;
continue;
}

glx[j][i].pos.z = s[j].pos.z + (rand()%2 ? -1 : 1) * (rand()%w*50)/(dist*dist);

dx = glx[j][i].pos.x - s[j].pos.x;
dy = glx[j][i].pos.y - s[j].pos.y;

dist = sqrt(dx*dx + dy*dy);

glx[j][i].vel.x = -(dy*1.6 + s[j].vel.x)/dist;
glx[j][i].vel.y = (dx*1.6 + s[j].vel.y)/dist;
glx[j][i].vel.z = 0;

glx[j][i].acc.x = glx[j][i].acc.y = glx[j][i].acc.z = 0;
glx[j][i].mass = 1;
}
}

}

__fastcall TGLgalaxy::TGLgalaxy()
{

stage = 0;
ep = 0.001;

n = 2;
gal = 1000;
spin_x=0;
spin_y=0;
old_x = 0;
old_y = 0;
move_z = 0;



spin_z = -1500;
initgal();


h = w = 512;

init();

AnsiString HELLO = "E:\\star.jpg";
//ZAPISZ_WIDOK_DO_PLIKU(HELLO);
LoadTexture(HELLO, STAR_TEXTURE,false);

}


__fastcall TGLgalaxy::~TGLgalaxy()
{
}


void __fastcall TGLgalaxy::new_sim(int numofgalx)
{

n = numofgalx;
initgal();
init();
// break;

}

void __fastcall TGLgalaxy::make()
{

int i, j;
float ep_t_mass;

for(i = 0; i < n; i++) {


if (n3ddistance(center,s[i].pos) > 1000.0) s[i].is_electron = true;


}
int cnt = 0;

cnt = 1;

// for(i = 0; i < n; i++) {
//
//
//if (s[i].is_electron == false) {
// if (cnt == 1 )s[i].is_proton = true;
// if (cnt == 0 ) {
//
// cnt = 1;
// }
//
// if (cnt == 0 ) {
//
// cnt = 1;
// }
//
// cnt = 0;


// }



}








where the header is here




//---------------------------------------------------------------------------

#ifndef galaxyH
#define galaxyH
//---------------------------------------------------------------------------
#include "Math.h"
#include "Math.hpp"
#include "DxcMath.h"

#include "gl/glew.h"
#include "gl/gl.h"
#include "gl/glu.h"
#include "glut.h"
#include "OPENGL_ATTACH.h"
#include <Textures.hpp>


#define GAL_SIZE 0.05
#define GAL_BOUNDS 400
#define GAL_NUMGAL 2



typedef struct _star {
//float pos[3];
t3dpoint pos;
//float vel[3];
t3dpoint vel;
//float acc[3];
t3dpoint acc;
float mass;
bool is_electron;
bool is_proton;
bool is_neutron;
} star;






class TGLgalaxy {
public:
__fastcall TGLgalaxy();
__fastcall ~TGLgalaxy();
float ep;

float x1, x2, x3;

double d;
unsigned int STAR_TEXTURE;
int n;
int gal;

star **glx;
star *s;

double velocity;
double time;
double lasttime;
double elapsedtime;
t3dpoint center;
t3dpoint lastcenter;
int h, w;
int spin_x,spin_y, old_x, old_y;
int move_z;
int spin_z;
void __fastcall init();

int stage;
//void __fastcall motion(x, y);
void __fastcall move();
void __fastcall movegals();
void __fastcall Draw();
void __fastcall initgal();


//void __fastcall process_frame();

void __fastcall new_sim(int numofgalx);

void __fastcall make();
};





#endif





[/quote]







:/






please do not bother about is_proton or is_election etc i just wanted to check sth

Share this post


Link to post
Share on other sites
[quote name='h4tt3n' timestamp='1304442154' post='4806025']
Sorry, but I don't see how. I argue that integration algorithms designed for constant accelleration can be used for solving problems containing non-constant accelleration (pretty much every real-life physics problem does that) and exemplifies my point through action by applying such an algorithm on a problem at hand. What's the contradicition?[/quote]
Velocity verlet does not assume constant acceleration. The algorithm specifies [i]when acceleration should be updated[/i].

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1304476130' post='4806234']
[quote name='h4tt3n' timestamp='1304442154' post='4806025']
Sorry, but I don't see how. I argue that integration algorithms designed for constant accelleration can be used for solving problems containing non-constant accelleration (pretty much every real-life physics problem does that) and exemplifies my point through action by applying such an algorithm on a problem at hand. What's the contradicition?[/quote]
Velocity verlet does not assume constant acceleration. The algorithm specifies [i]when acceleration should be updated[/i].
[/quote]

Ah ok, now I understand what you mean. Of course you're right in the sense that acceleration is updated as a part of the integration algorithm, and thus contains a change in acceleration code wise. Still, I'm pretty sure the velocity verlet algorithm assumes constant acceleration. I can't think of any discrete numerical integration algorithms that doesn't. If you know of any that allows you to explicitly set the rate of change of acceleration (jerk), I'd really like to hear about it.

Cheers,
Mike

Share this post


Link to post
Share on other sites
What are you talking about? Velocity verlet does not assume constant acceleration. All these other integration techniques exist because they don't assume constant acceleration. If there was constant acceleration you could just use simple kinematics equations. If velocity verlet assumed constant acceleration [b]there would be no update of acceleration as part of it's algorithm[/b]. Velocity verlet works well when acceleration changes as a function of position and breaks down when acceleration changes as a function of other variables such as velocity.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement