I have not done much with Processing, not more than 5 projects. This is, by far, the one I am most proud of. I have not followed any tutorials, used code from others (expect the PeasyCam library) nor have I looked at examples. I am far from good at Programming, I am just trying to convert my Ideas to Code. Have I made any major mistakes, anything I could do better or flawed code? Or leave your thoughts below.
PS: Giant Coding Train fan
Main Code:
import peasy.*;
PShape sky;
PImage skyTex;
PeasyCam cam;
ArrayList<Planet> planets;
float fmax = 0;
ArrayList<PVector> stars;
float rotate;
float radius = 250;
float camheight = 1000;
float fov;
float cameraZ;
float dis1;
int steps = 400000;
int p;
PImage sun, mercury, venus, earth, mars, jupiter, saturn, uranus, neptune;
void setup() {
//size(800, 600, OPENGL);
fullScreen(OPENGL);
frameRate(100);
sun = loadImage("sun.jpg");
mercury = loadImage("mercury.jpg");
venus = loadImage("venus.jpg");
earth = loadImage("earth.jpg");
mars = loadImage("mars.jpg");
jupiter = loadImage("jupiter.jpg");
saturn = loadImage("saturn.jpg");
uranus = loadImage("uranus.jpg");
neptune = loadImage("neptune.jpg");
p = 0;
fov = radians(70);
perspective(fov, float(width)/float(height), 1, 9999999);
cam = new PeasyCam(this, width/2, height/2, 0, 1000);
cam.setMinimumDistance(21);
skyTex = loadImage("sky.jpg");
sky = createShape(SPHERE, 500000);
sky.setTexture(skyTex);
sky.setStroke(0);
planets = new ArrayList<Planet>();
planets.add(new Planet("Sonne", 0, 0, 0, 0, 20, 333054, 255, 230, 0, 1, 1000, 0, sun));
planets.add(new Planet("Merkur", 62.543, 7, 0, 0.205, 8, 0.055, 132, 132, 132, 45, 13, 10, mercury));
planets.add(new Planet("Venus", 145.763, 3.4, 177, 0.007, 12, 0.8149, 197, 133, 40, 50, 28, 5, venus));
planets.add(new Planet("Erde", 200, 0, 23.4, 0.017, 13, 1, 84, 101, 130, 50, 48, 0.1, earth));
planets.add(new Planet("Mars", 280.9, 1.9, 6.7, 0.094, 9, 0.1069, 183, 99, 72, 60, 76, 0.1, mars));
planets.add(new Planet("Jupiter", 1006.798, 1.3, 25.2, 0.049, 40, 317, 167, 161, 150, 80, 350, 0.05, jupiter));
planets.add(new Planet("Saturn", 1839.02, 2.5, 3.1, 0.057, 32, 95, 208, 193, 163, 100, 700, 0.05, saturn));
planets.add(new Planet("Uranus", 3727.1, 0.8, 26.7, 0.046, 60, 14, 155, 203, 210, 132, 1450, 0.07, uranus));
planets.add(new Planet("Neptune", 6042.8, 1.8, 97.8, 0.011, 70, 17, 45, 52, 132, 183, 2000, 0.08, neptune));
//planets.add(new Planet("Pluto", -5032.3589395, 0, 0, 0, -0.19226, 0, 15, 0.10699933, 250, 200, 50));
background(0);
for (int i = 0; i < steps; i++) {
pre(i);
}
}
void draw() {
background(0);
//pushMatrix();
//translate(width/2, height/2, 0);
//stroke(0, 0, 255);
//line(300, 0, 0, -100, 0, 0);
//stroke(0, 255, 0);
//line(0, 330, 0, -0, -100, 0);
//stroke(255, 0, 0);
//line(0, 0, 300, 0, 0, -100);
//popMatrix();
float dis = (float) cam.getDistance();
if (dis > 50000) dis = 50000;
if (dis < 21) dis = 21;
dis1 = map(dis, 21, 50000, 0, 255);
fov = radians(log1(dis1));
perspective(fov, float(width)/float(height), 1, 9999999);
pushMatrix();
float[] campos = cam.getPosition();
translate(campos[0], campos[1], campos[2]);
shape(sky);
translate(-campos[0], -campos[1], -campos[2]);
popMatrix();
for (Planet planet1 : planets) {
planet1.grav(planet1, planets);
}
for (Planet planet1 : planets) {
planet1.updateVel();
}
Planet plan = planets.get(0);
Planet plan2 = planets.get(p);
//camera(width/2, /*(height/2)*/ + map(mouseY, 0, height, -1000, 1000), 1000, width/2, height/2, 0, 0, 0, 1);
pushMatrix();
translate((width/2) - plan2.pos.x, (height/2) - plan2.pos.y, 0 - plan2.pos.z);
for (int i = 0; i < planets.size(); i++) {
Planet plan1 = planets.get(i);
if (i != 0) plan1.light(plan);
plan1.show();
}
popMatrix();
}
void pre(int i) {
for (Planet planet1 : planets) {
planet1.grav(planet1, planets);
}
for (Planet planet1 : planets) {
planet1.updateVel1(i);
}
}
float log1(float a) {
float a2 = 2100 / (30 + 70 * exp(0.021 * -a));
return a2;
}
void keyPressed() {
if (key == 'w'){
p++;
if (p > planets.size()-1) p = 0;
}
if (key == 's'){
p--;
if (p < 0) p = planets.size()-1;
}
}
Planet Class:
class Planet {
PVector pos;
PVector vel;
PVector acc;
float radius;
float mass;
float r, g, b;
int mod;
PVector currVel;
String name;
ArrayList<PVector> posStore;
float dis;
int traillength;
float rotation;
float rot;
float winkelPole;
float Grav = 0.0002;
PShape planet;
Planet(String name, float dis, float incl, float winkelPole, float ecc, float r, float m, float re, float g, float b, int traillength, int mod, float rot, PImage img) {
float vz;
if (dis != 0) vz = sqrt((Grav * 333054) / dis);
else vz = 0;
vz = vz * (1 + (ecc/2));
this.pos = new PVector(dis, 0, 0);
this.vel = new PVector(0, 0, vz);
this.pos.x = dis * cos(radians(incl));
this.pos.y = (dis * sin(radians(incl))) *-1;
float hyp = vz / cos(radians(incl));
this.vel.y = sin(radians(incl)) * hyp;
this.vel.setMag(vz*-1);
this.winkelPole = winkelPole;
this.rotation = rot;
this.mod = mod;
this.radius = r;
this.mass = /*PI * sq(r)*/ m;
this.r = re;
this.g = g;
this.b = b;
this.currVel = new PVector(this.vel.x, this.vel.y, this.vel.z);
this.name = name;
this.traillength = traillength;
this.posStore = new ArrayList<PVector>(traillength);
for (int i = 0; i < traillength; i++) {
posStore.add(new PVector(this.pos.x, this.pos.y, this.pos.z));
}
planet = createShape(SPHERE, r);
planet.setTexture(img);
planet.setStroke(0);
}
void grav(Planet planet1, ArrayList<Planet> planets) {
for (Planet planet2 : planets) {
if (planet1 == planet2) continue;
PVector forceDir = PVector.sub(planet2.pos, planet1.pos);
float d = forceDir.magSq();
forceDir.normalize();
PVector f = forceDir.mult(Grav * planet2.mass / d);
currVel = planet1.vel.add(f);
}
}
void updateVel() {
this.pos.add(this.currVel);
if (frameCount % mod == 0) posStore.add(0, new PVector(this.pos.x, this.pos.y, this.pos.z));
if ( posStore.size() > traillength) posStore.remove(traillength);
}
void updateVel1(int i) {
this.pos.add(this.currVel);
if (i % mod == 0) posStore.add(0, new PVector(this.pos.x, this.pos.y, this.pos.z));
if ( posStore.size() > traillength) posStore.remove(traillength);
}
void show() {
for (int i = 0; i < posStore.size()-2; i++) {
PVector p1 = posStore.get(i);
PVector p2 = posStore.get(i+1);
stroke(this.r, this.g, this.b);
line(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
}
noStroke();
fill(r, g, b);
pushMatrix();
translate(pos.x, pos.y, pos.z);
if (rotation != 0) {
if (rot > 1000000) rot = 0;
rot = rot + (0.1 / rotation);
rotateX(radians(winkelPole));
rotateY(radians(rot));
}
//specular(r*1.5, g*1.5, b*1.5);
//shininess(5);
//emissive(r*0.2, g*0.2, b*0.2);
shape(planet);
popMatrix();
}
void light(Planet plan) {
lightSpecular(0, 0, 0);
pointLight(120, 120, 110, plan.pos.x, plan.pos.y, plan.pos.z);
}
}
Here is the data folder with all the textures:
https://anonfiles.com/B4X6W9P6o0/data_zip