Orbital Mechanics Question
by Jack Stone · in General Discussion · 04/16/2013 (10:28 am) · 25 replies
I don't know if this is the right forum to post this, there doesn't sem to be anything more specific.
I am working on a Space Simulation, for which I need relatively realistic orbital mechanics.
I have used this link:
http://www.arachnoid.com/ruby/gravity/index.html
To implement a simple system, but it doesn't seem to be working at all, my Moon is moving in a straight line away from the Earth with no signs of slowing down or orbiting.
My code is pretty simple, I have global variables:
and then in a loop called from script, I have:
This is very little code, and so it seems simple enough to work. I have tried scaling down the position of the object in script to make it "fit" into my game world, but it didn't seem to change the results.
I would appreciate any help.
I am working on a Space Simulation, for which I need relatively realistic orbital mechanics.
I have used this link:
http://www.arachnoid.com/ruby/gravity/index.html
To implement a simple system, but it doesn't seem to be working at all, my Moon is moving in a straight line away from the Earth with no signs of slowing down or orbiting.
My code is pretty simple, I have global variables:
F64 grav_const = 6.6742e-11; F64 earth_mass = 5.972e24; F64 moon_mass = 7.34767309e22; F64 dt = 50; F64 velX, velY = 0; F64 orbitposX = 0; F64 orbitposY = 356700;//000;
and then in a loop called from script, I have:
ConsoleFunction( testorbit, void, 1, 1, "none")
{
F64 distanceradius = 384400;//assume constant for now
//f = G M m
// ------
// r^2
F64 force = (grav_const * earth_mass * moon_mass)/(distanceradius*distanceradius);
//a = f/m
F64 acceleration = force/moon_mass; //moon is moving, so use it's mass
velX = 0;
velY = 0;
velX = velX + acceleration * dt;
velY = velY + acceleration * dt;
orbitposX = orbitposX + velX * dt;
orbitposY = orbitposY + velY * dt;
//send values to script function to move Moon object:
char value2[256];
dSprintf(value2,sizeof(value2),"%f",orbitposX);
char value3[256];
dSprintf(value3,sizeof(value3),"%f",orbitposY);
//Con::printf("OUTPUT: %f %f ", body_x, body_y);
Con::executef("moveorbits",value2,value3);
}This is very little code, and so it seems simple enough to work. I have tried scaling down the position of the object in script to make it "fit" into my game world, but it didn't seem to change the results.
I would appreciate any help.
#22
I have the following:
I am getting values that vary with distance, but they don't seem correct to me?
If I change the code to this:
I get scaling values between 0 and 1, and that seems to work, but I don't know if I have used the algorothm correctly?
I have also locked the position of the planet in script, to test the scaling. How would I scale the distance too?
I have a real-world distance value in script, (a very large number) would I then multiply by the scalefactor, assuming the scalefactor was less than one?
Thanks for your help on this!
05/02/2013 (3:59 pm)
@Daniel, I am having some issues integrating that scaling function in T3D.I have the following:
F64 dist = sqrt(pow((rocket.position.x -earthPos.x),2)+pow((rocket.position.y -earthPos.y),2)); F64 maxDist = 100000; F64 diam = 6378150.0*6378150.0; if( dist < maxDist / 2) scalefactor = 1; else scalefactor = 1 - 1 / pow(2,(dist/diam+1)) * diam / dist;
I am getting values that vary with distance, but they don't seem correct to me?
If I change the code to this:
F64 dist = sqrt(pow((rocket.position.x -earthPos.x),2)+pow((rocket.position.y -earthPos.y),2)); F64 maxDist = 100000; F64 diam = 6378150.0*6378150.0; //F64 mag = dist/maxDist; F64 f = dist/diam; scalefactor = 1 - (1 / pow(2,(f+1)) / f); scalefactor /= -1000000; if(scalefactor > 1) scalefactor = 1;
I get scaling values between 0 and 1, and that seems to work, but I don't know if I have used the algorothm correctly?
I have also locked the position of the planet in script, to test the scaling. How would I scale the distance too?
I have a real-world distance value in script, (a very large number) would I then multiply by the scalefactor, assuming the scalefactor was less than one?
Thanks for your help on this!
#23
Second, I think your diameter should be 2r, not r^2.
Third, this is applying the wrong order of operations:
See how that goes!
Oh - yes, the idea is that you multiply all object distances and diameters (from the camera!) by the scale factor. I think this scaling has to be done relative to the camera, or it will look fake.
05/02/2013 (5:43 pm)
First tip - simplify the distance calculation like so:F64 dist = (rocket.position - earthPos).len();
Second, I think your diameter should be 2r, not r^2.
Third, this is applying the wrong order of operations:
1 - 1 / pow(2,(dist/diam+1)) * diam / dist;It should be:
(1 - 1 / pow(2, dist/diam+1)) * diam / dist;(I removed some parentheses that weren't necessary.)
See how that goes!
Oh - yes, the idea is that you multiply all object distances and diameters (from the camera!) by the scale factor. I think this scaling has to be done relative to the camera, or it will look fake.
#24
05/02/2013 (6:31 pm)
I can't wait to see this is action. This sounds so cool!
#25
@demolishun, thank you very much! I should have something to show for it fairly soon, hopefully!
05/05/2013 (12:02 am)
@Daniel, thank you, that code seems to work really well! I don't know what I was thinking squaring the radius, I must have been tired or something, that's basic high school stuff!@demolishun, thank you very much! I should have something to show for it fairly soon, hopefully!
Torque Owner Demolishun
DemolishunConsulting Rocks!
Do Keplers laws depend on instantaneous communication? If they do why are physicists propagating the idea that gravity works on a particle/wave principle? Anyway, food for thought.