OpenGl inner solar system
              in 
             Share your Work 
              •  
              10 months ago    
            
 
           
              Gidday , First time here.
            
            
             I just wanted to share this example of a solar system with planets and comets.
            
            
             While it uses some real world data, its not accurate....may be later.  Regards Alan.
            
            
             // new orbit example 
             
// nplanets and ncomets
import processing.opengl.*;
//
            // nplanets and ncomets
import processing.opengl.*;
//
             float timestep = 200.0 ;
             
float theta = 0.0;
int scl = 1;
            float theta = 0.0;
int scl = 1;
// array of orbital info
String[] pname = {"SUN", "MER", "VEN", "STA", "EAR", "STB", "MAR", "P/2012 A3\nSOHO", "C/2012 S1\nISON", "C/2011 W3\nLovejoy"};
float[] pnum = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float[] ecc = {0, 0.206, 0.007, 0.009562656, 0.017, 0.004440, 0.093, 0.776, 0.995, 0.995}; // eccenticity
float[] au = {0, 0.39, 0.72, 0.95539896, 1.0, 1.051887, 1.52, 2.568, 10.0, 78.68 }; // mean distance 1au
float[] rot = {0, 0.24, 0.62, 0.93326171, 1, 1.078152, 1.88, 4.12, 10.0, 10.0}; // period of rotation 1yr
float[] eqtr = {3.0, 0.382, 0.949, 0.1, 1, 0.1, 0.532, 0.1, 0.1, 0.1}; // 1 earth diam
float[] angl = {0.0, 29.124, 55.186, 259.41452171, 114.20783, 18.37796930629710, 286.537, 217.6673, 295.7576, 332.02321}; // perri
             //
             
PFont fontA = createFont("SanSerif", 12);
PFont fontB = createFont("SanSerif", 16);
//
            PFont fontA = createFont("SanSerif", 12);
PFont fontB = createFont("SanSerif", 16);
//
             //   
             
void setup() {
size(1024, 1024, OPENGL);
}
            void setup() {
size(1024, 1024, OPENGL);
}
void draw()
{
background(0);
// Translate the origin point to the center of the screen
translate(width/2, height/2) ;
ellipseMode(RADIUS);
               nplanets();  // draw planets
             
ncomets() ; // draw comets
            ncomets() ; // draw comets
                 theta +=(0.00004109*(1+timestep));
             
   
             
}
            }
             void keyReleased() {
             
//
if (key =='x') { exit(); }
if (key =='c') { background(0); }
if (key =='[') { scl-=1 ; if (scl <= 0) {scl = 1;}}
if (key ==']') { scl+=1 ; if (scl >= 100) { scl = 100;}}
if (key =='-') { timestep-=100 ; if (timestep <= -1000) {timestep = -1000;}}
if (key =='=') { timestep+=100 ; if (timestep >= 1000) { timestep = 1000;}}
}
            //
if (key =='x') { exit(); }
if (key =='c') { background(0); }
if (key =='[') { scl-=1 ; if (scl <= 0) {scl = 1;}}
if (key ==']') { scl+=1 ; if (scl >= 100) { scl = 100;}}
if (key =='-') { timestep-=100 ; if (timestep <= -1000) {timestep = -1000;}}
if (key =='=') { timestep+=100 ; if (timestep >= 1000) { timestep = 1000;}}
}
             void nplanets() {
             
// draw planets
for ( int np = 6 ; np > -1 ; np-=1 ) {
float e1 = ecc[np];
float a1 = (au[np]*300 );
float b1 = a1*sqrt(1-(e1*e1)) ;
float c1 = a1*e1 ;
float l1 = (b1*b1)/a1 ;
            // draw planets
for ( int np = 6 ; np > -1 ; np-=1 ) {
float e1 = ecc[np];
float a1 = (au[np]*300 );
float b1 = a1*sqrt(1-(e1*e1)) ;
float c1 = a1*e1 ;
float l1 = (b1*b1)/a1 ;
                 float r1 = l1/(1.0+(e1*cos(angl[np]+theta/rot[np])));
             
float x1 = r1*cos(angl[np]+theta/rot[np]);
float y1 = -r1*sin(angl[np]+theta/rot[np]);
            float x1 = r1*cos(angl[np]+theta/rot[np]);
float y1 = -r1*sin(angl[np]+theta/rot[np]);
                 //draw orbit
             
stroke(32); fill(255/(np+1),255/(np+1),16/(np+1),16);strokeWeight(2); // transparent orbit plane
ellipse(0, 0, r1/scl, r1/scl);
            stroke(32); fill(255/(np+1),255/(np+1),16/(np+1),16);strokeWeight(2); // transparent orbit plane
ellipse(0, 0, r1/scl, r1/scl);
                 // draw planet at orbit angle
             
stroke(200, 200, 200); fill(36*(np+1),36*(np+1),16*(np+1),255);
ellipse(int(x1)/scl, int(y1)/scl, (3*eqtr[np])/scl, (3*eqtr[np])/scl);
//sphere((3*eqtr[np])/scl );
stroke(255); fill(255);
textFont(fontA, 12); textAlign(LEFT);
text( np+" "+pname[np], (1+int(x1)/scl), (-1+int(y1)/scl));
}
}
            stroke(200, 200, 200); fill(36*(np+1),36*(np+1),16*(np+1),255);
ellipse(int(x1)/scl, int(y1)/scl, (3*eqtr[np])/scl, (3*eqtr[np])/scl);
//sphere((3*eqtr[np])/scl );
stroke(255); fill(255);
textFont(fontA, 12); textAlign(LEFT);
text( np+" "+pname[np], (1+int(x1)/scl), (-1+int(y1)/scl));
}
}
             void ncomets() {
             
// draw comets
for ( int nc = 9 ; nc > 6 ; nc-=1 ) {
float ec = ecc[nc];
float ac = (au[nc]*300 );
float bc = (ac*sqrt(1-(ec*ec))) ;
float cc = ac*ec ;
float lc = (bc*bc)/ac ;
            // draw comets
for ( int nc = 9 ; nc > 6 ; nc-=1 ) {
float ec = ecc[nc];
float ac = (au[nc]*300 );
float bc = (ac*sqrt(1-(ec*ec))) ;
float cc = ac*ec ;
float lc = (bc*bc)/ac ;
                 float rc = lc/(1.0+(ec*cos(angl[nc]+theta/rot[nc])));
             
float xc = (rc*cos(angl[nc]+theta/rot[nc]))+(2*cc);
float yc = (-rc*sin(angl[nc]+theta/rot[nc]));
// draw at orbit angles
stroke(200, 200, 200); fill(200, 200, 200);
ellipse(int(xc)/scl, int(yc)/scl, (3*eqtr[nc]), (3*eqtr[nc]));
stroke(255); fill(255);
textFont(fontA, 12); textAlign(LEFT);
text( pname[nc], (1+int(xc)/scl), (-1+int(yc)/scl));
}
}
            
            float xc = (rc*cos(angl[nc]+theta/rot[nc]))+(2*cc);
float yc = (-rc*sin(angl[nc]+theta/rot[nc]));
// draw at orbit angles
stroke(200, 200, 200); fill(200, 200, 200);
ellipse(int(xc)/scl, int(yc)/scl, (3*eqtr[nc]), (3*eqtr[nc]));
stroke(255); fill(255);
textFont(fontA, 12); textAlign(LEFT);
text( pname[nc], (1+int(xc)/scl), (-1+int(yc)/scl));
}
}
