commit f6ef69b52f62513115ec91e2060e2d96ab0ac695 Author: 61616 <33187059+61616@users.noreply.github.com> Date: Thu Mar 31 14:53:00 2022 -0400 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/GravityDev3.pde b/GravityDev3.pde new file mode 100644 index 0000000..6099e20 --- /dev/null +++ b/GravityDev3.pde @@ -0,0 +1,72 @@ + +PlanetManager pm; // Manages Planets +Gui gui; // Contains and manages gui +ActionFlags flags = new ActionFlags(); // Public global actions flags +Vector offset; + +float g = 0.05; // Gravity constant +float scrollDirection = 1; // Multiply by -1 to change scroll direction +float scaleMul = 1.0; // Scale multiplier +float guiScale = 1.0; // Seperate gui scale multiplier + +int focus = 1; // Planet in focus (-1 for no focus) +float focusTolerance = 10; // How much error can there be when clicking a planet + +void setup(){ + size(1000,800); + frameRate(60); + noStroke(); + offset = new Vector(width/2, height/2); + pm = new PlanetManager(); + gui = new Gui(); + + + pm.createPlanet(500.0, 400.0, 0.0, 0.0, 200.0); + pm.createPlanet(850.0, 400.0, 0.0, 3.2, 25.0); + pm.createPlanet(880.0, 400.0, 0.0, 4.3, 1.0); + pm.createPlanet(700.0, 400.0, 0.0, 6.0, 2.0); +} + + + + + + + +void draw(){ + background(205); + + pm.updateGravity(); + + // Subtract from offset the difference between current offset and the planet in focus + if(focus >= 0){ + if(flags.centerOnFocus){ + offset.sub(offset.x - pm.getPos(focus).x,offset.y - pm.getPos(focus).y); // Moves focused planet in center of screen + flags.centerOnFocus = false; + }else{ + offset.add(pm.getVel(focus).x,pm.getVel(focus).y); // Makes focused planet stationary + } + } + + /* PLANET MATRIX */ + pushMatrix(); + translate(width/2,height/2); + scale(scaleMul); + pm.drawPlanets(); + popMatrix(); + + /* DEFAULT MATRIX */ + + gui.display(); + + /* DEBUG PRINTS + println(); + //println(offset.x + " " + offset.y); + Vector mouse = mouseS(); + println(mouse.x + " " + mouse.y); + println(pm.getRenPos(0).x + " " + pm.getRenPos(0).y + " " + dist(pm.getRenPos(1).x,pm.getRenPos(1).y,mouse.x,mouse.y)); + /* + if (focus >=0){ + println(pm.getPos(focus).x + " " + pm.getPos(focus).y); + println((pm.getPos(focus).x - offset.x) + " " + (pm.getPos(focus).y - offset.y));}*/ +} diff --git a/eMouse.pde b/eMouse.pde new file mode 100644 index 0000000..5122a2b --- /dev/null +++ b/eMouse.pde @@ -0,0 +1,31 @@ +/* + +Mouse overrides and functions + +*/ + +void mouseDragged() { + float mx = pmouseX - mouseX; + float my = pmouseY - mouseY; + offset.add(mx / scaleMul, my / scaleMul); +} + +void mouseWheel(MouseEvent event){ + float e = event.getCount() * scrollDirection; + //offset.sub(mouseX,mouseY); + float delta = e < 0 ? 1.05 : e > 0 ? 1.0/1.05 : 1.0; + scaleMul *= delta; + //offset.add(mouseX,mouseY); +} + +void mouseClicked() { + boolean guiInteract = gui.clickManager(new Vector(mouseX,mouseY)); + if(!guiInteract){ + Vector mouse = mouseS(); + focus = pm.getCPlanet(mouse.x,mouse.y); + } +} + +Vector mouseS(){ + return new Vector((mouseX - width/2) * 1/scaleMul, (mouseY - height/2) * 1/scaleMul); +} diff --git a/fActionFlags.pde b/fActionFlags.pde new file mode 100644 index 0000000..69c7cbb --- /dev/null +++ b/fActionFlags.pde @@ -0,0 +1,10 @@ +/* + +Public flags to control certain actions, mostly for gui interaction with the simulation + +*/ + +class ActionFlags { + boolean centerOnFocus = false; + boolean nameTags = true; +} diff --git a/mGui.pde b/mGui.pde new file mode 100644 index 0000000..83d5706 --- /dev/null +++ b/mGui.pde @@ -0,0 +1,49 @@ +/* + +Actual GUI file that defines all gui elements + +*/ + +class Gui { + CButton cb; + + Gui(){ + cb = new CButton(); + } + + void display(){ + cb.display(); + } + + boolean clickManager(Vector mouse){ + return cb.clicked(mouse.x,mouse.y); + } +} + +class CButton extends Button{ + + CButton(){ + pos = new Vector(width/2,15); + dim = new Vector(70,24); + t = "center"; + tSize = 20; + } + + void display(){ + fill(col); + rectMode(CENTER); + rect(pos.x,pos.y,dim.x,dim.y,rad); + + fill(tCol); + textAlign(CENTER,CENTER); + textSize(tSize); + text(t,pos.x,pos.y - tSize/4); + } + + boolean clicked(float mx, float my){ + if(mouseOver(mx,my)){ + flags.centerOnFocus = true; + return true; + }else{return false;} + } +} diff --git a/wButton.pde b/wButton.pde new file mode 100644 index 0000000..205a111 --- /dev/null +++ b/wButton.pde @@ -0,0 +1,93 @@ +/* + +Button class + +*/ + +class Button { + Vector pos; + Vector dim; + float rad = 3; + color col = #413f54; + String t = ""; + color tCol = 0; + int tSize = 20; + + boolean mouseOver(float mx, float my){ + if (mx >= pos.x - dim.x/2 && mx <= pos.x + dim.x/2 && + my >= pos.y - dim.x/2 && my <= pos.y + dim.y/2) { + return true; + }else{ + return false; + } + } +} + +/* DEPRECATED +class Button { + Vector pos; + Vector dim; + float r = .5; + String t = ""; + color col = (#413f54); + color tCol = 0; + + boolean mouseOver = false; + + // INITIALIZERS + + Button(float _x, float _y, float _w, float _h){ + this.pos = new Vector(_x,_y); + this.dim = new Vector(_w,_h); + } + Button(float _x, float _y, float _w, float _h, float _r){ + this.pos = new Vector(_x,_y); + this.dim = new Vector(_w,_h); + this.r = _r; + } + Button(float _x, float _y, float _w, float _h, color _col){ + this.pos = new Vector(_x,_y); + this.dim = new Vector(_w,_h); + this.col = _col; + } + Button(float _x, float _y, float _w, float _h, float _r, color _col){ + this.pos = new Vector(_x,_y); + this.dim = new Vector(_w,_h); + this.r = _r; + this.col = _col; + } + + + // SETTERS + void setText(String _t){this.t = _t;} + void setRad(float _r){this.r = _r;} + void setColor(color _col){this.col = _col;} + + // Set Position + void setPos(Vector _pos){this.pos = _pos;} + void setPos(float _x, float _y){this.pos = new Vector(_x,_y);} + + // Set Dimensions + void setDim(Vector _dim){this.dim = _dim;} + void setDim(float _w, float _h){this.dim = new Vector(_w,_h);} + + // OTHER FUNCTIONS + void drawB(){ + fill(col); + rectMode(CORNER); + rect(pos.x,pos.y,dim.x,dim.y,r); + fill(tCol); + text(t,pos.x,pos.y); + } + + boolean mouseOver(float mx, float my){ + if (mx >= pos.x && mx <= pos.x + dim.x && + my >= pos.y && my <= pos.y + dim.y) { + return true; + }else{ + return false; + } + } +}\ + +*/ diff --git a/xPlanet.pde b/xPlanet.pde new file mode 100644 index 0000000..988e7e9 --- /dev/null +++ b/xPlanet.pde @@ -0,0 +1,59 @@ +/* + +Planet class + +*/ + +class Planet { + Vector pos; + Vector vel; + Vector ren = new Vector(0,0); + float mass; + color col = 100; + float dia; + String name; + + Planet(float x, float y, float mass){ + pos = new Vector(x,y); + vel = new Vector(0,0); + this.mass = mass; + this.dia = constrain( sqrt(mass) * 5 ,1,100); + } + + /* SETTERS */ + void setPos(Vector _pos) {this.pos = _pos;} + void setPos(float x, float y) {this.pos = new Vector(x,y);} + + void setVel(Vector _vel) {this.pos = _vel;} + void setVel(float x, float y) {this.vel = new Vector(x,y);} + + void setname(String _name){this.name = _name;} + + /* FUNCTIONS */ + void gravity(Planet p){ + float xDist = (p.pos.x - this.pos.x); + float yDist = (p.pos.y - this.pos.y); + float magsq = sq(xDist) + sq(yDist); + + float pMass = (this.mass * p.mass); + + float xGrav = xDist * g * (pMass/magsq); + float yGrav = yDist * g * (pMass/magsq); + + this.vel.add(xGrav/this.mass,yGrav/this.mass); + } + void updatePos(){ + this.pos.add(this.vel); + } + void display(){ + fill(this.col); + ren.set(pos.x - offset.x,pos.y - offset.y); + circle(ren.x,ren.y,dia); + if(flags.nameTags){ + fill(0); + textAlign(CENTER,BOTTOM); + textSize(20 * 1/scaleMul); + text(name,ren.x,ren.y - dia/2); + } + } +} diff --git a/yPlanetManager.pde b/yPlanetManager.pde new file mode 100644 index 0000000..6c7a22d --- /dev/null +++ b/yPlanetManager.pde @@ -0,0 +1,64 @@ +/* + +Planet manager is a container class for all planets and planet io + +*/ + +class PlanetManager { + ArrayList planets = new ArrayList(); + + /* FACTORIES */ + void createPlanet(float x, float y, float mass){ + planets.add(new Planet(x,y,mass)); + } + void createPlanet(float x, float y, float xSpeed, float ySpeed, float mass){ + planets.add(new Planet(x,y,mass)); + planets.get(planets.size() - 1).setVel(xSpeed, ySpeed); + planets.get(planets.size() - 1).setname(str(planets.size() - 1)); + } + + /* GETTERS */ + Vector getPos(int f){return planets.get(f).pos;} + Vector getRenPos(int f){return planets.get(f).ren;} + Vector getVel(int f){return planets.get(f).vel;} + + /* FUNCTIONS */ + void updateGravity(){ + for(int i = 0; i < planets.size(); i++){ + for(int j = 0; j < planets.size(); j++){ + if(i != j){ + planets.get(i).gravity(planets.get(j)); + } + } + } + for(int i = 0; i < planets.size(); i++){ + planets.get(i).updatePos(); + } + } + + void drawPlanets(){ + for(int i = 0; i < planets.size(); i++){ + planets.get(i).display(); + } + } + + int getCPlanet(float mx, float my){ + int out = -1;//new Vector(mx,my); + float smallestDist = focusTolerance * 10; + + for(int i = 0; i < pm.planets.size(); i++){ + float dist = dist(pm.getRenPos(i).x,pm.getRenPos(i).y,mx,my); + + // If no planets are within tolerance the focus will be set to -1, ie: no planet in focus + if(dist < (focusTolerance * 1/scaleMul) + (pm.planets.get(i).dia / 2)){//(focusTolerance * 1/scaleMul)){ + println(dist + " " + (focusTolerance * 1/scaleMul) + " " + (pm.planets.get(i).dia / 2)); + //println(pm.planets.get(i).dia); + if(smallestDist > dist){ + smallestDist = dist; + out = i; + } + } + } + return out; + } +} diff --git a/zVector.pde b/zVector.pde new file mode 100644 index 0000000..e7c881a --- /dev/null +++ b/zVector.pde @@ -0,0 +1,84 @@ +/* + +My own vector class so that I can see and control what is going on inside + +*/ + +class Vector { + float x; + float y; + + Vector(float _x, float _y){ + this.x = _x; + this.y = _y; + } + Vector(Vector v){ + this.x = v.x; + this.y = v.y; + } + void set(float _x, float _y){ + this.x = _x; + this.y = _y; + } + + // Add a float or vector + void add(Vector o){x += o.x; y += o.y;} + void add(float _c){x += _c; y += _c;} + void add(float _x, float _y){x += _x; y += _y;} + + // Subtract a float or vector + void sub(Vector o){x -= o.x; y -= o.y;} + void sub(float _c){x -= _c; y -= _c;} + void sub(float _x, float _y){x -= _x; y -= _y;} + + // multiply by a float or vector + void mul(Vector o){x *= o.x; y *= o.y;} + void mul(float _c){x *= _c; y *= _c;} + void mul(float _x, float _y){x *= _x; y *= _y;} + + // Divide by a float or vector + void div(Vector o){x /= o.x; y /= o.y;} + void div(float _c){x /= _c; y /= _c;} + void div(float _x, float _y){x /= _x; y /= _y;} + + // Return absolute distance to any point + float mag(Vector o){ + return sqrt(sq(o.x - this.x) + sq(o.y - this.y)); + } +} + +/* +class Vector3D extends Vector { + float z; + + Vector3D(float _x, float _y, float _z){ + super(_x,_y); + z = _z; + } + void set(float _x, float _y, float _z){ + x = _x; + y = _y; + z = _z; + } + + // Add a float or vector + void add(Vector3D o){x += o.x; y += o.y; z += o.z;} + void add(float _c){x += _c; y += _c; z += _c;} + void add(float _x, float _y, float _z){x += _x; y += _y; z += _z;} + + // Subtract a float or vector + void sub(Vector3D o){x -= o.x; y -= o.y; z -= o.z;} + void sub(float _c){x -= _c; y -= _c; z -= _c;} + void sub(float _x, float _y, float _z){x -= _x; y -= _y; z -= _z;} + + // multiply by a float or vector + void mul(Vector3D o){x *= o.x; y *= o.y; z *= o.z;} + void mul(float _c){x *= _c; y *= _c; z *= _c;} + void mul(float _x, float _y, float _z){x *= _x; y *= _y; z *= _z;} + + // Divide by a float or vector + void div(Vector3D o){x /= o.x; y /= o.y; z /= o.z;} + void div(float _c){x /= _c; y /= _c; z /= _c;} + void div(float _x, float _y, float _z){x /= _x; y /= _y; z /= _z;} +} +*/