Solar System

Ever since the midterm project I’ve been thinking about ways how to continue with the universe theme, which was going on in that project. This week’s assignment finally seemed like a good opportunity, so I decided to recreate the Solar System in Processing. There are 8 planets (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus and Neptune) revolving around the Sun. Since Pluto was excluded from the Solar System in 2006, it is not included in my project either.

 

For creating the Solar System I only had to use one class. It is possible to create both the planets and the orbit circles for the planets with the same class.

1.The function planet creates all the planets as well as the Sun in the middle of the sketch. The function is defined by 4 variables – orbit size, the image which is around the planet, planet size and planet’s speed. The planet itself is a sphere. After creating this class it was easy to quickly add new planets by just changing the variables mentioned above. One of the most interesting parts about this project was setting a texture for the spheres. This is done in functions initializeSphere and textureSphere. Then I found pictures of the textures of these planets, added them to the sketch and assigned them to the corresponding planets. It gives the whole Solar System a more realistic look.

 

2.The function orbit creates the orbit circles for the planets. The orbit is an ellipse with the diameter twice as big as the obit size of the planet. There is one orbit circle for each planet rotating around the Sun. Because orbit circles are obviously not visible in nature, I added a feature that by pressing the key “o” it is possible to turn the orbit circles on and off.

orbit circles are on
orbit circles are off

 

3.There is also a Moon that I added to the Solar System. Because the Moon is not always visible it is also possible to make it appear or disappear by pressing the key “m”. The Moon is located between the Earth and Venus and it is not rotating around the Sun.

 

4.Lastly, I am using the camera built into Processing to be able to navigate around the Solar System by changing the perspective, zooming in and out, etc. That way it is possible to look at the Solar System from different angles.

from above
from the side

 

Here is the code for my Solar System:

import peasy.*;
PeasyCam cam;

Planet earth;
Planet mercury;
Planet venus;
Planet mars;
Planet jupiter;
Planet saturn;
Planet uranus;
Planet neptune;
Planet sun;
Planet moon;

boolean orbitSwitch = true;
boolean moonSwitch=true;


void setup() {
 size(1280,720, P3D);
 float fov = PI/3.0;
 float aspect=float(width)/float(height);
 perspective(fov,aspect,height,0);
 cam = new PeasyCam(this, 100);
 cam.setMinimumDistance(50);
 cam.setMaximumDistance(5000);
 
 
 earth = new Planet(70, "earth.jpg", 6, .012);
 mercury = new Planet(30, "pl_mercury.jpg", 5, .016);
 venus = new Planet(50, "ven0aaa2.jpg", 6, .015);
 mars = new Planet(90, "2k_mars.jpg", 6, .01);
 jupiter = new Planet (110, "Jupiter.jpg", 12, .009);
 saturn = new Planet (130, "2k_saturn.jpg", 10, .008);
 uranus = new Planet (150, "2k_uranus.jpg", 8, .007);
 neptune = new Planet (170, "preview_neptune.jpg", 8, .006);
 sun = new Planet (0, "texture_sun.jpg", 11, 0);
 moon = new Planet (60, "moon.jpg", 2, 0);
 
}

void draw() {
 background(0);
 
 //planet
 earth.planet();
 mercury.planet();
 venus.planet();
 mars.planet();
 jupiter.planet();
 saturn.planet();
 uranus.planet(); 
 neptune.planet();
 sun.planet();
 
 //orbit circles
 if (orbitSwitch==true) {
 earth.orbit();
 mercury.orbit();
 venus.orbit();
 mars.orbit();
 jupiter.orbit();
 saturn.orbit();
 uranus.orbit();
 neptune.orbit();
 }
 
 //moon
 if (moonSwitch==true) {
 moon.planet(); 
 }
 
}

void keyPressed() {
 if (key=='o'){
 orbitSwitch=!orbitSwitch;
 }
 
 if (key=='m'){
 moonSwitch=!moonSwitch; 
 }
 
}

class Planet {
 int orbit;
 float x, z;
 PImage img;
 int planetSize;
 float angle;
 float speed;
 int numPointsW;
 int numPointsH_2pi; 
 int numPointsH;

float[] coorX;
float[] coorY;
float[] coorZ;
float[] multXZ;
 
 Planet(int _orbit, String _imageName, int _planetSize, float _speed){
 orbit=_orbit;
 x=0;
 z=0;
 img = loadImage(_imageName);
 planetSize=_planetSize;
 angle=0.;
 speed=_speed;
 initializeSphere(30,30);
 }
 
 
 void planet(){
 //planet
 pushMatrix();
 x = cos(angle)*orbit;
 z = sin(angle)*(orbit+7);
 translate(x,0,z);
 noStroke();
 textureSphere(planetSize, planetSize, planetSize, img);
 popMatrix();
 angle+=speed;
 }
 
 void orbit() {
 stroke(255,120);
 noFill();
 pushMatrix();
 rotateX(radians(90));
 ellipse(0, 0, orbit*2, orbit*2+14);
 popMatrix();
 
 
 }
 
 void initializeSphere(int numPtsW, int numPtsH_2pi) {

 // The number of points around the width and height
 numPointsW=numPtsW+1;
 numPointsH_2pi=numPtsH_2pi; // How many actual pts around the sphere (not just from top to bottom)
 numPointsH=ceil((float)numPointsH_2pi/2)+1; // How many pts from top to bottom (abs(....) b/c of the possibility of an odd numPointsH_2pi)

 coorX=new float[numPointsW]; // All the x-coor in a horizontal circle radius 1
 coorY=new float[numPointsH]; // All the y-coor in a vertical circle radius 1
 coorZ=new float[numPointsW]; // All the z-coor in a horizontal circle radius 1
 multXZ=new float[numPointsH]; // The radius of each horizontal circle (that you will multiply with coorX and coorZ)

 for (int i=0; i<numPointsW ;i++) { // For all the points around the width
 float thetaW=i*2*PI/(numPointsW-1);
 coorX[i]=sin(thetaW);
 coorZ[i]=cos(thetaW);
 }
 
 for (int i=0; i<numPointsH; i++) { // For all points from top to bottom
 if (int(numPointsH_2pi/2) != (float)numPointsH_2pi/2 && i==numPointsH-1) { // If the numPointsH_2pi is odd and it is at the last pt
 float thetaH=(i-1)*2*PI/(numPointsH_2pi);
 coorY[i]=cos(PI+thetaH); 
 multXZ[i]=0;
 } 
 else {
 //The numPointsH_2pi and 2 below allows there to be a flat bottom if the numPointsH is odd
 float thetaH=i*2*PI/(numPointsH_2pi);

 //PI+ below makes the top always the point instead of the bottom.
 coorY[i]=cos(PI+thetaH); 
 multXZ[i]=sin(thetaH);
 }
 }
}

void textureSphere(float rx, float ry, float rz, PImage t) { 
 // These are so we can map certain parts of the image on to the shape 
 float changeU=t.width/(float)(numPointsW-1); 
 float changeV=t.height/(float)(numPointsH-1); 
 float u=0; // Width variable for the texture
 float v=0; // Height variable for the texture

 beginShape(TRIANGLE_STRIP);
 texture(t);
 for (int i=0; i<(numPointsH-1); i++) { // For all the rings but top and bottom
 // Goes into the array here instead of loop to save time
 float coory=coorY[i];
 float cooryPlus=coorY[i+1];

 float multxz=multXZ[i];
 float multxzPlus=multXZ[i+1];

 for (int j=0; j<numPointsW; j++) { // For all the pts in the ring
 normal(-coorX[j]*multxz, -coory, -coorZ[j]*multxz);
 vertex(coorX[j]*multxz*rx, coory*ry, coorZ[j]*multxz*rz, u, v);
 normal(-coorX[j]*multxzPlus, -cooryPlus, -coorZ[j]*multxzPlus);
 vertex(coorX[j]*multxzPlus*rx, cooryPlus*ry, coorZ[j]*multxzPlus*rz, u, v+changeV);
 u+=changeU;
 }
 v+=changeV;
 u=0;
 }
 endShape();
}
}

 

Here are some pictures I took throughout the progress of creating the Solar System. One of the most challenging parts is to determine the distance between the planets so they wouldn’t overlap when rotating, however, depending from the angle that you look at, it is not completely inevitable.

 

Finally here are some videos of the rotation of the planets from different angles with or without the orbits:

solar system

solar system 2

from above

without orbits

Luize Rieksta

1 thought on “Solar System”

  1. What a fantastic view to the Solar system from different sides and angles. I wish I had so many pictures available at school at the end of 80ies when learning about planets.
    Very interesting and exciting!

    Proud of you Luize!
    Mammi

Comments are closed.