Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import controlP5.*;
- Random randa = new Random();
- ControlP5 cp5;
- int button_height =30, pozX =2, button_width = 106, h = 0, hi = 1;
- int pozXSet = pozX + button_width, control = 10, currentIndex = 0;
- int control2 = control, MaxFar = 100, Multi = 10, start;
- float mass, radius, G_const = 0.6673, scrollMovement = 0, pozYSet[] = new float[2];
- boolean stopStart = false;//true - everything is working , false - everything is stoped
- boolean field = false;//true - central field , false - homogeneous field
- boolean information = false;
- boolean[] scrollMenuOpenByMouse = new boolean[5];
- ArrayList<Circum> cir = new ArrayList<Circum>(control);
- PVector grav = new PVector(0.00, 5.00);
- PVector air_re = new PVector(0.004, 0.004);
- PVector inic = new PVector(0.0005, 0.0005);
- PVector air;
- String buttonName;
- void setup() {
- //fullScreen(P2D, 1);
- size(1500, 800, P2D);
- stroke(255);
- background(255);
- cp5 = new ControlP5(this);
- //creating buttons
- cp5.addButton("Menu").setPosition(pozX, 1).setSize(button_width, button_height);
- cp5.addButton("Reset").setPosition(pozX, button_height).setSize(button_width, button_height);
- hi++;
- cp5.addButton("StartStop").setPosition(pozX, button_height * hi).setSize(button_width, button_height).setCaptionLabel("Start the program");
- hi++;
- cp5.addButton("Homogen").setPosition(pozX, button_height * hi).setSize(button_width, button_height).setCaptionLabel("Homogeneous field").setVisible(false).setValue(1);
- hi++;
- cp5.addButton("Central").setPosition(pozX, button_height * hi).setSize(button_width, button_height).setCaptionLabel("Central field").setVisible(false).setValue(1);
- //creating random circles
- for (int i = 0; i <control; i++)
- {
- mass = 20 * randa.nextFloat() + 4;
- radius = mass * 2;
- float pozX = width * new Random().nextFloat();
- float pozY = height * new Random().nextFloat();
- float border= 10;
- //Checking whether circles are overlapping or outside the screen boundary.
- if (i!=0)
- {
- for (int j = 0; j <i; j++)
- {
- while (PVector.sub(new PVector(pozX, pozY), cir.get(j).point).mag() <= cir.get(j).radius + radius + border || pozY - radius <= border || pozY + radius >= height - border || pozX - radius <= border || pozX + radius >= width - border) {
- pozX = (width * new Random().nextFloat()) - radius;
- pozY = (height * new Random().nextFloat()) - radius;
- }
- }
- }
- cir.add(new Circum(pozX, pozY, radius, mass));
- cir.get(i).velocity = new PVector(0, 0);
- cir.get(i).acceleration = new PVector(0, 0);
- cir.get(i).gre = new PVector(0, 0);
- }
- }
- void StartStop() {
- stopStart = !stopStart;
- if (stopStart)
- {
- cp5.getController("StartStop").setCaptionLabel("Stopping the program");
- } else
- {
- cp5.getController("StartStop").setCaptionLabel("Start the program");
- }
- }
- void ball_setup()
- {
- for (int i = control - 1; i >= 0; i--)
- {
- cir.remove(i);
- }
- for (int i = 0; i<control; i++)
- {
- mass = 8 * randa.nextFloat() + 4;
- radius = mass * 4;
- float pozX = width * new Random().nextFloat();
- float pozY = height * new Random().nextFloat();
- float border= 10;
- //Checking whether circles are overlapping or outside the screen boundary.
- if (i!=0)
- {
- for (int j = 0; j <i; j++)
- {
- while (PVector.sub(new PVector(pozX, pozY), cir.get(j).point).mag() <= cir.get(j).radius + radius + border || pozY - radius <= border || pozY + radius >= height - border || pozX - radius <= border || pozX + radius >= width - border) {
- pozX = (width * new Random().nextFloat());
- pozY = (height * new Random().nextFloat());
- }
- }
- }
- cir.add(new Circum( pozX, pozY, radius, mass));
- cir.get(i).velocity = new PVector(0,0);
- cir.get(i).acceleration = new PVector(0, 0);
- cir.get(i).gre = new PVector(0, 0);
- }
- }
- void checkForCollision()
- {
- for (int i = 0; i<cir.size() -1; i++)
- {
- for (int j = i + 1; j<cir.size(); j++)
- {
- //calculating distance between object
- PVector lengthFrom_i_to_j= PVector.sub( cir.get(j).point, cir.get(i).point);
- float oldDist = lengthFrom_i_to_j.mag();
- float min_dyst = cir.get(j).radius + cir.get(i).radius;
- //checking for collision
- if (oldDist <= min_dyst)
- {
- collision(cir.get(i), cir.get(j), oldDist, min_dyst, lengthFrom_i_to_j);
- }
- }
- }
- }
- void collision(Circum con1, Circum con2, float dist_, float min_, PVector lock)
- {
- float u1, u2, distance = dist_, min_dyst = min_;
- //static collision
- float distanceCorrection = (min_dyst-distance)/2.0;
- PVector correctionVector = lock.normalize().mult(distanceCorrection);
- con2.point.add(correctionVector);
- con1.point.sub(correctionVector);
- //dynamic collision
- // Defining the X axis
- PVector dirX = lock.copy();
- dirX.normalize();
- // Defining the Y axis
- PVector dirY = new PVector(dirX.y, -dirX.x);
- // X coordinates of velocities
- float vx1 = dirX.dot(con1.velocity);
- float vx2 = dirX.dot(con2.velocity);
- // Y coordinates of velocities
- float vy1 = dirY.dot(con1.velocity);
- float vy2 = dirY.dot(con2.velocity);
- // Applying the collision to X coordinates
- u1 = (2 * vx2 * con2.mass + vx1 * (con1.mass - con2.mass)) / (con1.mass + con2.mass);
- u2 = (2 * vx1 * con1.mass + vx2 * (con2.mass - con1.mass)) / (con1.mass + con2.mass);
- // Turning velocities back into vectors
- PVector vel1 = PVector.mult(dirX, u1);
- PVector vel2 = PVector.mult(dirX, u2);
- vel1.add(PVector.mult(dirY, vy1));
- vel2.add(PVector.mult(dirY, vy2));
- con1.velocity = vel1;
- con2.velocity = vel2;
- }
- void centralField()
- {
- float dem = 0, dividerx = 0, dividery = 0, Distance;
- PVector grav_atract = new PVector(0, 0);
- ArrayList<PVector> reverseGravity = new ArrayList<PVector>(cir.size());
- if (stopStart)
- {
- for (int i = 0; i < cir.size(); i++)
- {
- reverseGravity.add(new PVector(0, 0));
- }
- for (int i = 0; i<cir.size(); i++)
- {
- //Calculation of the mass center
- dem += cir.get(i).mass;
- dividerx += cir.get(i).mass * cir.get(i).point.x;
- dividery += cir.get(i).mass * cir.get(i).point.y;
- for (int j = i + 1; j<cir.size(); j++)
- {
- //calculating distance between object
- PVector lengthFrom_i_to_j= PVector.sub( cir.get(j).point, cir.get(i).point);
- PVector lengthFrom_j_to_i= PVector.sub( cir.get(i).point, cir.get(j).point);
- Distance = lengthFrom_i_to_j.mag();
- lengthFrom_i_to_j.normalize();
- lengthFrom_j_to_i.normalize();
- //adding gravity
- float strength = (G_const * cir.get(i).mass * cir.get(j).mass)/(Distance * Distance);
- lengthFrom_i_to_j.mult(strength );
- lengthFrom_j_to_i.mult(strength );
- reverseGravity.get(j).add(lengthFrom_j_to_i);
- grav_atract.add(lengthFrom_i_to_j);
- }
- grav_atract.add( reverseGravity.get(i));
- cir.get(i).setSpeed(grav_atract);
- grav_atract.mult(0);
- for (int k = 0; k<reverseGravity.size(); k++)
- {
- reverseGravity.get(k).mult(0);
- }
- //checkForCollision();
- }
- }
- checkForCollision();
- for (int i = 0; i<cir.size(); i++)
- {
- //cir.get(i).border();
- cir.get(i).drawing();
- }
- //Drawing a center of mass
- pushMatrix();
- strokeWeight(6);
- fill(50, 0, 255 );
- stroke(255, 0, 50);
- point((dividerx/dem), (dividery/dem));
- popMatrix();
- }
- void homogeneousField()
- {
- PVector air_replica = new PVector(0.004, 0.004);
- PVector grav = new PVector(0.00, 3.00);
- PVector air;
- if (stopStart)
- {
- for (int i = 0; i<cir.size(); i++)
- {//Adding gravity
- cir.get(i).setSpeed(grav);
- air = air_replica.copy();
- if (cir.get(i).velocity.x>0 ||cir.get(i).velocity.y>0)
- {
- air.mult(-1);
- }
- //adding motion resistance
- cir.get(i).setSpeed(air);
- }
- }
- checkForCollision();
- for (int j = 0; j<cir.size(); j++)
- {
- //cir.get(j).border();
- cir.get(j).drawing();
- }
- }
- void mouseWheel(MouseEvent event) {
- start = millis();
- scrollMovement = constrain(event.getCount()*Multi + scrollMovement, -MaxFar, MaxFar);
- }
- void mousePressed() {
- if (!stopStart)
- {
- if (field)
- {
- for (int i = 0; i<cir.size(); i++)
- {
- float d = dist(mouseX, mouseY, cir.get(i).point.x, cir.get(i).point.y);
- if (d <= cir.get(i).radius ) {
- currentIndex = i;
- if (mouseButton == CENTER)
- {
- scrollMenuOpenByMouse[0] = true;
- }
- }
- }
- } else
- {
- for (int j = 0; j<cir.size(); j++)
- {
- float d = dist(mouseX, mouseY, cir.get(j).point.x, cir.get(j).point.y);
- if (d <= cir.get(j).radius) {
- currentIndex = j;
- if (mouseButton == CENTER)
- {
- scrollMenuOpenByMouse[1] = true;
- }
- }
- }
- }
- }
- }
- void mouseReleased() {
- if (!stopStart)
- {
- if (field)
- {
- if (scrollMenuOpenByMouse[0]) {
- scrollMenuOpenByMouse[0] = false;
- }
- } else
- {
- if (scrollMenuOpenByMouse[1]) {
- scrollMenuOpenByMouse[1] = false;
- }
- }
- }
- }
- void changeVelocityByTheMouse(int currentIndex)
- {
- int i = currentIndex;
- PVector diff = new PVector(0, 0), position= new PVector(0, 0), result = new PVector(0, 0);
- if (!stopStart)
- {
- if (field)
- {
- if (scrollMenuOpenByMouse[0])
- {
- position = cir.get(currentIndex).point;
- diff = PVector.sub(new PVector(mouseX, mouseY), position);
- diff.normalize();
- diff.mult(scrollMovement);
- result.add(diff);
- } else
- {
- cir.get(currentIndex).acceleration.add(result);
- }
- } else
- {
- if (scrollMenuOpenByMouse[1])
- {
- position = cir.get(currentIndex).point;
- diff = PVector.sub(new PVector(mouseX, mouseY), position);
- diff.normalize();
- diff.mult(scrollMovement);
- result.add(diff);
- } else
- {
- cir.get(currentIndex).acceleration.add(result);
- }
- }
- }
- line(position.x, position.y, result.x + position.x, result.y + position.y);
- circle(diff.x, diff.y, 10);
- result.set(0, 0);
- }
- void Homogen() {
- CentralButton(false);
- }
- void Central() {
- CentralButton(true);
- }
- void Reset()
- {
- background(255);
- ball_setup();
- }
- void CentralButton(boolean i)
- {
- field = i;
- setting();
- }
- void Menu()
- {
- information = !information;
- }
- void Disclosures_set(String name)
- {
- pozYSet = cp5.getController(name).getPosition();
- cp5.getController("Set").setPosition(pozXSet, pozYSet[1]);
- cp5.getController("input").setPosition(pozXSet, pozYSet[1]+button_height);
- buttonName = name;
- cp5.getController("Set").show();
- cp5.getController("input").show();
- }
- void ButtonManagement()
- {
- if (information)
- {
- cp5.get(Button.class, "Homogen").setVisible(true);
- cp5.get(Button.class, "Central").setVisible(true);
- } else if (information == false)
- {
- cp5.get(Button.class, "Homogen").setVisible(false);
- cp5.get(Button.class, "Central").setVisible(false);
- }
- }
- void setting()
- {
- float pozX = 0, pozY = 0;
- for (int i = control - 1; i >= 0; i--)
- {
- cir.remove(i);
- }
- if ( field == true)
- {
- for (int i = 0; i <control2; i++)
- {
- mass = 8 * randa.nextFloat() + 4;
- radius = mass * 4;
- pozX = width * new Random().nextFloat();
- pozY = height * new Random().nextFloat();
- float border= 10;
- //Checking whether circles are overlapping or outside the screen boundary.
- if (i!=0)
- {
- for (int j = 0; j <i; j++)
- {
- while (PVector.sub(new PVector(pozX, pozY), cir.get(j).point).mag() <= cir.get(j).radius + radius + border || pozY - radius <= border || pozY + radius >= height - border || pozX - radius <= border || pozX + radius >= width - border) {
- pozX = (width * new Random().nextFloat());
- pozY = (height * new Random().nextFloat());
- }
- }
- }
- cir.add(new Circum(pozX, pozY, radius, mass));
- cir.get(i).velocity = new PVector(0, 0);
- }
- control = control2;
- } else if ( field == false)
- {
- for (int i = 0; i<control2; i++)
- {
- mass = 8 * randa.nextFloat() + 4;
- radius = mass * 2;
- pozX = (width * new Random().nextFloat()) - radius;
- pozY = (height * new Random().nextFloat()) - radius;
- cir.add(new Circum( pozX, pozY, radius, mass));
- cir.get(i).setSpeed(inic);
- }
- control = control2;
- }
- }
- void draw() {
- background(200);
- if (field)
- {
- centralField();
- } else
- {
- homogeneousField();
- }
- if (!stopStart)
- {
- changeVelocityByTheMouse(currentIndex);
- }
- ButtonManagement();
- }
- class Circum {
- int max_vel = 15;
- float radius, mass;
- float ref = 0.5;
- float springness = 0.9999;
- PVector point = new PVector(0, 0);
- PVector acceleration = new PVector(0, 0);
- PVector velocity = new PVector(new Random().nextInt(max_vel) + 0.05, -1 * (new Random().nextInt(max_vel) + 0.05));
- PVector gre = new PVector(0, 0);
- String[] fieldName = new String[3];
- float[] fieldValue = new float[3];
- Circum() {
- mass = 5 * new Random().nextFloat() + 1;
- radius = mass * 4;
- point.x = (width * new Random().nextFloat()) - radius;
- point.y = (height * new Random().nextFloat()) - radius;
- }
- Circum(float x_, float y_, float r_, float m_) {
- this();
- point.x = x_;
- point.y = y_;
- radius = r_;
- mass = m_;
- }
- void setSpeed(PVector force) {
- PVector f = force.copy();
- acceleration.add(PVector.div( f, mass));
- velocity.add(acceleration);
- point.add(velocity);
- gre = acceleration.copy();
- // If out of bounds
- if (point.y - radius <= 0)
- {
- velocity.y *= -springness;
- point.y = 0 + radius;
- } else if (point.y + radius >= height)
- {
- velocity.y *= -springness;
- point.y = height - radius ;
- }
- if (point.x - radius <= 0)
- {
- velocity.x *= -springness;
- point.x = 0 + radius;
- } else if (point.x + radius >= width)
- {
- velocity.x *= -springness;
- point.x = width - radius;
- }
- acceleration.mult(0);
- }
- void drawing() {
- fill((map(velocity.y, -max_vel, max_vel, 50, 255)), (map(gre.y, -2, 2, 50, 100)), (map(point.y, 0, width, 1, 255)) );
- stroke((map(point.x, 0, height, 100, 255)), (map(gre.x, -2, 2, 50, 100)), (map(velocity.x, -max_vel, max_vel, 50, 255)));
- circle(point.x, point.y, 2 * radius);
- strokeWeight(2);
- stroke(255, 255, 255);
- PVector dir = velocity.copy();
- dir.normalize().mult(radius);
- dir = PVector.add(dir, point);
- line(point.x, point.y, dir.x, dir.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement