Gravitational Simulation doesn't work

edited March 2015 in Questions about Code

I'm new to Processing and after writing some "Bouncing Ball" programs I wanted to do something bigger,so I decided to write a "Gravitational Simulation" which simulates Gravitational Force and its effects between two objects(A and B).

The program reads data from txt files and the uses them to calculate.

Because the real numbers would be too big,I scale them down by 100000000.

The scaling is NOT done by the program that's why the numbers in the txts are scaled down by me.

I think everything should work fine,but when I give real life numbers(Sun and Mercury) somewhy the trajectory got messed up.The time acceleration of the program is done by maxing out the framerate.

Here are my values(taken from Wikipedia and divided by 100000000):

Ax = 300 
Ay = 300 
Bx =879.09176 (Ax + 579...) 
By = 300 
Am = 19890000000000000000000 
Bm = 3302000000000000
Ad = 20 
Bd = 10 (diameters are not accurate because they aren't needed for any calculations) 
Bvy = 4.763(don't ask why)
Line = 0

Here is my code:

public float Ax,Ay,Bx,By,Avx,Avy,Bvx,Bvy,Aax,Aay,Bax,Bay,Aex,Aey,Bex,Bey,d,dx,dy,Am,Bm,F,Ad,Bd;
void setup(){
  frameRate(250);
size (displayWidth,displayHeight);
Ax = 300;  Bx = 879.09176;//Position X
Ay = 300;  By = 300;//Position Y
Avx =0;  Bvx= 0;//Velocity X
Avy =0;  Bvy= 4.763;//Velocity Y
Aax =0;  Bax= 0;//Acceleration X
Aay =0;  Bay= 0;//Acceleration Y
Aex =0;  Bex= 0;//Direction X
Aey =0;  Bey= 0;//Direction Y
Am = 19890000000000000000000 ;  Bm = 3302000000000000;//Mass
Ad = 20;  Bd = 10;//Diameter
}
void draw(){  
if(Ax < Bx){Aex = 1;Bex = -1;dx = (Bx - Ax);}else{//Setting Direction-X and Distance-X(dx)
if(Ax > Bx){Aex = -1;Bex = 1;dx = (Ax - Bx);}else{
if(Ax == Bx){Aex = 0;Bex = 0;dx = 0;}}}
if(Ay < By){Aey = 1;Bey = -1;dy = (By - Ay);}else{//Setting Direction-Y and Distance-Y(dy)
if(Ay > By){Aey = -1;Bey = 1;dy = (Ay - By);}else{
if(Ay == By){Aey = 0;Bey = 0;dy = 0;}}}
d = sqrt((dx*dx)+(dy*dy));//Calculating the distance
d = (d * d);//Squaring the distance(needed for further calculations)
F = ((6.67384*((Am*Bm)/(d)))/100000000000.0)/100000000.0;// "6.67384/100000000000.0" is the G constant. "/100000000.0" SCALES down the force
if(Ax != Bx){Aax = F/Am;Bax = F/Bm;}else{Aax = 0;Bax = 0;}//Calculating Acceleration-X
if(Ay != By){Aay = F/Am;Bay = F/Bm;}else{Aay = 0;Bay = 0;}//Calculating Acceleration-Y
Avx = Avx + (Aax * Aex);//Calculating Velocity-X using the CURRENT Velocity-X , the Acceleration-X and the Direction of the Acceleration-X(Aex or Bex)
Avy = Avy + (Aay * Aey);
Bvx = Bvx + (Bax * Bex);//Calculating Velocity-Y using the CURRENT Velocity-Y , the Acceleration-Y and the Direction of the Acceleration-Y(Aey or Bey)
Bvy = Bvy + (Bay * Bey);
Ax = Ax + Avx;//Calculating Position-X...
Ay = Ay + Avy;//...and Y
Bx = Bx + Bvx;//And for the... 
By = By + Bvy;//...B oject too
println("Speed-X:" + Bvx + " Speed-Y:" + Bvy + " F:" + F);
println("Dist:" + sqrt(d)/1000 + " Accel X:" +Bax + " Y:" + Bay);
println("Fps:" + frameRate);
if (int(loadStrings("Line.txt")[0]) == 1){//Draws the trajectory of the objects or hides them. BTW:I know it would be easier using the "background()" function
  fill(255);
  stroke(255);
  rect(0,0,displayWidth,displayHeight);
}
fill(0);
stroke(0);
ellipse(Ax,Ay,Ad,Ad);//Drawing Object A
fill(0,0,255);
stroke(0,0,255);
ellipse(Bx,By,Bd,Bd);//Drawing Object B

}

Thanks For Any Help

Answers

Sign In or Register to comment.