View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Cant get a stable planet orbit...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1JeffNiko  Members

Posted 15 February 2011 - 03:00 PM

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?

### #2Phobon  Members

Posted 15 February 2011 - 03:10 PM

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)

### #3SiCrane  Moderators

Posted 15 February 2011 - 03:33 PM

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.

### #4EricTheRed  Members

Posted 15 February 2011 - 03:42 PM

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.

### #5KulSeran  Members

Posted 15 February 2011 - 04:50 PM

You need a Symplectic Integrator to keep from losing or gaining energy in your orbits

### #6Enders  Members

Posted 15 February 2011 - 07:17 PM

WHOO!

I have been working on this myself on my own project

--------- 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

### #7Spacer  Members

Posted 02 May 2011 - 12:20 PM

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. It works

### #8h4tt3n  Members

Posted 03 May 2011 - 05:32 AM

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

### #9h4tt3n  Members

Posted 03 May 2011 - 05:38 AM

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

### #10SiCrane  Moderators

Posted 03 May 2011 - 07:17 AM

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

### #11h4tt3n  Members

Posted 03 May 2011 - 11:02 AM

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

### #12forsandifs  Members

Posted 03 May 2011 - 11:31 AM

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.

### #13WiredCat  Members

Posted 03 May 2011 - 03:16 PM

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

Velocity is forceA

centripetal force is gravitforce then

i have some not mine src code

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

#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 = 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);
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);
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);

}

__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;

// }

}

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

#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

:/

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

### #14SiCrane  Moderators

Posted 03 May 2011 - 08:28 PM

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?

Velocity verlet does not assume constant acceleration. The algorithm specifies when acceleration should be updated.

### #15h4tt3n  Members

Posted 04 May 2011 - 05:31 AM

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?

Velocity verlet does not assume constant acceleration. The algorithm specifies when acceleration should be updated.

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

### #16SiCrane  Moderators

Posted 04 May 2011 - 05:39 AM

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 there would be no update of acceleration as part of it's algorithm. 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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.