I'm making a project here consisting of a 3D Solar System App. So my app has a main screen, and after touching the red box, it will redirect to the 3D Solar System Model. The problem here is that my codes are separated from each other. Here is my code: For the home screen:
StateInterface currentState = new StateHome();
// -------------------------------------------------
void setup() {
// init (runs only once)
fullScreen(OPENGL);
} // func
void draw() {
// runs on and on
currentState.showState();
}
// ----------------------------------------
// keyboard input
void keyPressed() {
currentState.keyPressedState();
}
//----------------------------------------
void mousePressed() {
currentState.mousePressedState();
}
// ===============================================================================================
class StateHome implements StateInterface {
// Game
void showState() {
// Game
background(11);
fill(244, 3, 3); // red
text ("Welcome to Solar System Generator", 850, 100);
//
noStroke();
fill(255, 2, 2) ;
rect(100, 100, 100, 100);
fill(255, 2, 255) ;
// rect(300, 100, 100, 100);
if (mouseX > 100 &&
mouseX < 100 + 100 &&
mouseY > 100 &&
mouseY < 100 + 100) {
println ("collide");
currentState = new statePause();
}
} // func
void keyPressedState() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
//
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
// Pause
currentState = new statePause();
} else {
// do nothing
} // else
} // else not CODED
} // func
void mousePressedState() {
// Game
println("mouse 1");
}//func
}//class
// ===========================================================================================
class statePause implements StateInterface {
// My Solar System Generator that will be here
}
interface StateInterface {
// Let's abstract
// Let's abstract display()
void showState();
void keyPressedState();
void mousePressedState();
}
For the Solar System Generator:
import peasy.*;
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);
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("Mercury", 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("Earth", 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]);
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;
}
}
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);
}
}
So, I tried to directly combine the codes together:
import peasy.*;
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;
StateInterface currentState = new StateHome();
// -------------------------------------------------
void setup() {
// init (runs only once)
fullScreen(OPENGL);
} // func
void draw() {
// runs on and on
currentState.showState();
}
// ----------------------------------------
// keyboard input
void keyPressed() {
currentState.keyPressedState();
}
//----------------------------------------
void mousePressed() {
currentState.mousePressedState();
}
// ===============================================================================================
class StateHome implements StateInterface {
// Game
void showState() {
// Game
background(11);
fill(244, 3, 3); // red
text ("Welcome to Solar System Generator", 850, 100);
//
noStroke();
fill(255, 2, 2) ;
rect(100, 100, 100, 100);
fill(255, 2, 255) ;
// rect(300, 100, 100, 100);
if (mouseX > 100 &&
mouseX < 100 + 100 &&
mouseY > 100 &&
mouseY < 100 + 100) {
println ("collide");
currentState = new statePause();
}
} // func
void keyPressedState() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
//
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
// Pause
currentState = new statePause();
} else {
// do nothing
} // else
} // else not CODED
} // func
void mousePressedState() {
// Game
println("mouse 1");
}//func
}//class
// ===========================================================================================
class statePause implements StateInterface {
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);
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("Mercury", 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("Earth", 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]);
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;
}
}
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);
}
}
}
interface StateInterface {
// Let's abstract
// Let's abstract display()
void showState();
void keyPressedState();
void mousePressedState();
}
//
Which is a cause a bunch of mess, and it didn't work. Hence, I would like to know how to link the files together in Processing IDE.
Here is the link to the pictures for the planets:
Perhaps the following source code will get you started. I added another function to your StateHome class called 'showGame()' and added a global boolean called 'startGame'. The boolean is triggered by a mouse press on your home window (red square is non-functional); draw() then checks to see if the boolean is true or false. If it's true the solar system is shown. The planet class needs to be under its own tab to simplify your code. OpenGL has been deprecated so I used P3D as the renderer. Note that I also used .png images instead of .jpg but you may use whatever you have; change the code if necessary.
import peasy.*;
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;
StateInterface currentState = new StateHome();
boolean startGame = false;
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 setup() {
fullScreen(P3D);
sun = loadImage("Sun.png");
mercury = loadImage("Mercury.png");
venus = loadImage("Venus.png");
earth = loadImage("Earth.png");
mars = loadImage("Mars.png");
jupiter = loadImage("Jupiter.png");
saturn = loadImage("Saturn.png");
uranus = loadImage("Uranus.png");
neptune = loadImage("Neptune.png");
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);
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("Mercury", 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("Earth", 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() {
if (startGame) {
currentState.showGame();
} else {
currentState.showState();
}
}
void keyPressed() {
currentState.keyPressedState();
if (key == 'w') {
p++;
if (p > planets.size()-1) p = 0;
}
if (key == 's') {
p--;
if (p < 0) p = planets.size()-1;
}
}
void mousePressed() {
currentState.mousePressedState();
}
class StateHome implements StateInterface {
void showState() {
background(11);
fill(244, 3, 3); // red
textSize(48);
text ("Welcome to Solar System Generator", 850, 100);
noStroke();
fill(255, 2, 2) ;
rect(100, 100, 100, 100);
fill(255, 2, 255) ;
if (mouseX > 100 &&
mouseX < 100 + 100 &&
mouseY > 100 &&
mouseY < 100 + 100) {
println ("button pressed.");
currentState = new StatePause();
}
}
void showGame() {
startGame = true;
background(0);
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]);
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 keyPressedState() {
if (key == CODED) {
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
//
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
// Pause
currentState = new StatePause();
} else {
// do nothing
} // else
} // else not CODED
} // func
void mousePressedState() {
// Game
println("mouse 1");
if(!startGame){
currentState.showGame();
}
}//func
}//class
class StatePause implements StateInterface {
StatePause() {
}
void showState() {
}
void showGame() {
}
void keyPressedState() {
}
void mousePressedState() {
}
}
interface StateInterface {
void showState();
void showGame();
void keyPressedState();
void mousePressedState();
}
Planet class under separate tab:
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);
}
}
Addendum: The following is source code for a simple app capable of changing screens for multiple games:
boolean startHome = true;
boolean startGame1 = false;
boolean startGame2 = false;
boolean startGame3 = false;
void setup() {
fullScreen(P3D);
}
void draw() {
if (startHome) {
showHome();
}
if (startGame1) {
showGame1();
}
if (startGame2) {
showGame2();
}
if (startGame3) {
showGame3();
}
}
void keyPressed() {
if (key == '0') {
startHome = true;
startGame1 = false;
startGame2 = false;
startGame3 = false;
}
if (key == '1') {
startHome = false;
startGame1 = true;
startGame2 = false;
startGame3 = false;
}
if (key == '2') {
startHome = false;
startGame1 = false;
startGame2 = true;
startGame3 = false;
}
if (key == '3') {
startHome = false;
startGame1 = false;
startGame2 = false;
startGame3 = true;
}
}
void showHome() {
background(209);
fill(255, 0, 0);
textSize(42);
textAlign(CENTER,CENTER);
text ("Welcome to Solar System Generator \n Home = key '0' \n Game 1 = key '1' \n"
+ " Game 2 = key '2' \n Game 3 = key '3' \n Cmd-Q to Quit", width/2, height/2);
}
void showGame1() {
background(255, 0, 0);
}
void showGame2() {
background(0, 255, 0);
}
void showGame3() {
background(0, 0, 255);
}