# Position of Celestial Bodies at time T

Page 1 of 1

## 0 Replies - 582 Views - Last Post: 19 October 2009 - 01:39 PM

### #1 BetaWar

• #include "soul.h"

Reputation: 1264
• Posts: 7,514
• Joined: 07-September 06

# Position of Celestial Bodies at time T

Posted 19 October 2009 - 01:39 PM

I recently ran across this application:
and thought it looked cool so I decided to try and create one of my own.

Looking at some physics equations you find that
x(T) = .5aT^2+vT+x
v(T) = aT+v
F = ma
F = (GMm)/(r^2)

Given that you can solve for position at time like so:
ma = (GMm)/r^2
a = GM/r^2
v(T) = (GM/r^2)T+v
x(T) = .5(GM/r^2)T^2 + vT + x

And with that (with any luck) you should be able to calculate the position in space for the celestial bodies. GMm/r^2 is the equation for the gravitational force enacted on celestial bodies from eachother, where M is one planet's mass and m is another's. r is the distance between their centers.

I have devised this code:
```function drawCircle(radius:Number=5, bgColor:uint=0x000000):MovieClip{
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(bgColor);
mc.graphics.endFill();
return mc;
}
function distributeMasses(arr:Array, mass:Array):void{
for(var i:uint=0; i<arr.length; i++){
arr[i].mass = mass[i];
}
}
function distributeCoords(arr:Array, coords:Array):void{
for(var i:uint = 0; i<arr.length; i++){
arr[i].x = coords[i].x;
arr[i].ix = coords[i].x
arr[i].y = coords[i].y;
arr[i].iy = coords[i].y;
}
}
function distributeVelocities(arr:Array, vals:Array):void{
for(var i:uint = 0; i<arr.length; i++){
arr[i].vx = vals[i].x;
arr[i].vy = vals[i].y;
}
}
function sqr(n:Number):Number{
return n*n;
}
function distanceBetween(m1:MovieClip, m2:MovieClip):Number{
trace(Math.sqrt(sqr(m1.x-m2.x)+sqr(m1.y+m2.y)));
return Math.sqrt(sqr(m1.x-m2.x)+sqr(m1.y+m2.y));
}
function angleBetween(m1:MovieClip, m2:MovieClip):Number{
return Math.atan((m1.y-m2.y)/(m1.x-m2.x));
}
function doMotion(arr:Array):void{
var G:Number = 10;
for(var i:uint = 0; i<arr.length; i++){
arr[i].ax = 0;
arr[i].ay = 0;
for(var j:uint = 0; j < arr.length; j++){
if(i != j){
arr[i].ax += ((arr[j].mass*G)/sqr(distanceBetween(arr[i], arr[j])))*(Math.cos(angleBetween(arr[i], arr[j]))*(180/Math.PI));
arr[i].ay += ((arr[j].mass*G)/sqr(distanceBetween(arr[i], arr[j])))*(Math.sin(angleBetween(arr[i], arr[j]))*(180/Math.PI));
}
}
}
for(i = 0; i<arr.length; i++){
//trace("Planet "+i+" V("+arr[i].vx+", "+arr[i].vy+")");
//arr[i].vx += arr[i].ax*frame;
//arr[i].vy += arr[i].ay*frame;
arr[i].x = (.5*arr[i].ax*sqr(frame))+(arr[i].vx*frame)+arr[i].ix;
arr[i].y = (.5*arr[i].ay*sqr(frame))+(arr[i].vy*frame)+arr[i].iy;
//arr[i].y = arr[i].vy;
}
frame++;
}

var frame:uint = 0;
var holder:MovieClip = new MovieClip();
holder.x = 225;
holder.y = 200;
var mcs:Array = new Array();
mcs.push(drawCircle());
mcs.push(drawCircle(10, 0xff0000));
distributeMasses(mcs, new Array(10, 200));
distributeCoords(mcs, new Array(new Point(142, 0), new Point(0, 0)));
distributeVelocities(mcs, new Array(new Point(0, 1.40), new Point(0, 0)));
for each(var m:MovieClip in mcs){
}

function mainLoop(e):void{
doMotion(mcs);
}
//a=GM/r^2

```

But so far it seems to have a problem, one which I don't understand the reason for. The position at time equation should be able to give the planet's x and y coordinates in space at a given time, so I see no reason for the odd results I am receiving. If you take a look at it in Flash it starts by the small planet zooming off to the right and then the large planet following (slower). The issue I am having here is that I am using the same values that the application I linked to above are and am getting very different results (meaning of course that I am doing something wrong). Given that I have been looking at this for a while I decided it would be about time to have some fresh eyes look it over and see if I have done anything extremely stupid with the code.

I am also a bit annoyed that the large planet seems to spaz out after a little while of doing this, I see no reason for that to happen...

Any help would be appreciated.

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }