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;
- 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;
- float mass, radius;
- float time = 0.00;
- float time_step = 0.01;
- int button_height =30, pozX =2, button_width = 106, he = button_height, hi = 1;
- int pozXSet = pozX + button_width, control = 10, currentIndex = 0;
- boolean stopStart = false;//true - everything is working , false - everything is stoped
- boolean changePositionByMouse = false;
- boolean contextMenuOpenByMouse = false;
- ArrayList<Circum> cir = new ArrayList<Circum>(control);
- ArrayList<controlP5.Button> but = new ArrayList<controlP5.Button>(control);
- ArrayList<controlP5.Textfield> textField = new ArrayList<controlP5.Textfield>(control);
- void setup() {
- //fullScreen(P2D, 1);
- size(1500, 800, P2D);
- stroke(255);
- background(255);
- cp5 = new ControlP5(this);
- //creating buttons
- cp5.addButton("Reset").setPosition(pozX, 1).setSize(button_width, button_height);
- cp5.addButton("StartStop").setPosition(pozX, he * hi).setSize(button_width, button_height).setCaptionLabel("Uruchomienie programu");
- //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).vel = new PVector(-2, 5 * new Random().nextFloat() - 2.5);
- cir.get(i).acceleration = new PVector(0, 0);
- cir.get(i).gre = new PVector(0, 0);
- }
- for ( int j = 0; j< cir.get(1).showingData(); j++)
- {
- textField.add(cp5.addTextfield(cir.get(1).fieldName[j]).setPosition(cir.get(1).point.x - cir.get(1).radius + j * button_width, cir.get(1).point.y + cir.get(1).radius ).setSize(button_width, button_height).setText(Float.toString(cir.get(1).fieldValue[j])).hide());
- }
- }
- void drawBorders()
- {
- strokeWeight(6);
- stroke(200, 0, 10);
- line(0, height, width, height);
- line(0, 0, width, 0);
- line(0, 0, 0, height);
- line(width, 0, width, height);
- strokeWeight(1);
- }
- void contextMenu(int currentIndex)
- {
- int i = currentIndex;
- if (contextMenuOpenByMouse)
- {
- int numberOfFields = cir.get(i).showingData();
- for ( int j = 0; j< numberOfFields; j++)
- {
- textField.add(cp5.addTextfield(cir.get(1).fieldName[j]).setPosition(cir.get(1).point.x - cir.get(1).radius + j * button_width, cir.get(1).point.y + cir.get(1).radius ).setSize(button_width, button_height).setText(Float.toString(cir.get(1).fieldValue[j])).hide());
- // textField.get(currentIndex).setPosition(cir.get(i).point.x - cir.get(i).radius + j * button_width, cir.get(i).point.y + cir.get(i).radius ).setSize(button_width, button_height);//.setText(Float.toString(cir.get(i).fieldValue[j]));
- }
- }
- }
- void Reset()
- {
- background(255);
- ball_setup();
- }
- void StartStop() {
- stopStart = !stopStart;
- if (stopStart)
- {
- cp5.getController("StartStop").setCaptionLabel("Zatrzymanie programu");
- } else
- {
- cp5.getController("StartStop").setCaptionLabel("Uruchomienie programu");
- }
- }
- 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).speed(inic);
- }
- }
- void mousePressed() {
- if (!stopStart)
- {
- 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 ) {
- if (mouseButton == LEFT)
- {
- changePositionByMouse = true;
- } else if (mouseButton == RIGHT)
- {
- contextMenuOpenByMouse = true;
- contextMenu(i);
- }
- currentIndex = i;
- }
- }
- }
- }
- void mouseReleased() {
- if (!stopStart)
- {
- if (changePositionByMouse) {
- changePositionByMouse = false;
- }else if(contextMenuOpenByMouse)
- {
- contextMenuOpenByMouse = false;
- textField.get(currentIndex).getTab().hide();
- }
- }
- }
- void mouseDragged()
- {
- if (mouseButton == RIGHT)
- {
- // currentIndex
- }
- }
- void changePositionByTheMouse(int currentIndex)
- {
- int i = currentIndex;
- float border1= 5, border2= 6;
- if (changePositionByMouse)
- {
- if (cir.get(i).point.y - radius <= border1 || cir.get(i).point.y + radius >= height - border1 || cir.get(i).point.x - radius <= border1 || cir.get(i).point.x + radius >= width - border1)
- {
- if (cir.get(i).point.y - radius <= 0)
- {
- cir.get(i).point.y = border2 + radius;
- cir.get(i).point.x = mouseX;
- } else if (cir.get(i).point.y + radius >= height)
- {
- cir.get(i).point.y = height - radius - border2;
- cir.get(i).point.x = mouseX;
- }
- if (cir.get(i).point.x - radius <= 0)
- {
- cir.get(i).point.x = border2 + radius;
- cir.get(i).point.y = mouseY;
- } else if (cir.get(i).point.x + radius >= width)
- {
- cir.get(i).point.x = width - radius - border2;
- cir.get(i).point.y = mouseY;
- }
- } else
- {
- cir.get(i).point.x = mouseX;
- cir.get(i).point.y = mouseY;
- }
- cir.get(i).vel.mult(0);
- }
- }
- void collision(Circum con1, Circum con2, float dist_, float min_, PVector lock)
- {
- float u1, u2, distance = dist_, min_dyst = min_;
- // 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.vel);
- float vx2 = dirX.dot(con2.vel);
- // Y coordinates of velocities
- float vy1 = dirY.dot(con1.vel);
- float vy2 = dirY.dot(con2.vel);
- // 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.vel = vel1;
- con2.vel = vel2;
- float distanceCorrection = (min_dyst-distance)/2.0;
- PVector correctionVector = lock.normalize().mult(distanceCorrection);
- con2.point.add(correctionVector);
- con1.point.sub(correctionVector);
- }
- void checkForCollision(int i, int j)
- {
- PVector lock = PVector.sub( cir.get(j).point, cir.get(i).point);
- float oldDist = lock.mag();
- float min_dyst = cir.get(j).radius + cir.get(i).radius;
- if (oldDist <= min_dyst)
- {
- collision(cir.get(i), cir.get(j), oldDist, min_dyst, lock);
- println("Collision between: ", j, "and", i);
- }
- }
- void draw() {
- background(200);
- //translate(width / 2, height / 2);
- drawBorders();
- for (int k = 0; k<cir.size(); k++)
- {
- if (stopStart)
- {
- cir.get(k).speed(grav);
- air = air_re.copy();
- if (cir.get(k).vel.x>0)
- {
- air.mult(-1);
- }
- cir.get(k).speed(air);
- }
- for (int j = k + 1; j<cir.size(); j++)
- {
- if (k != cir.size() - 1)
- {
- checkForCollision(k, j);
- }
- }
- cir.get(k).border();
- cir.get(k).drawing();
- }
- if (!stopStart)
- {
- changePositionByTheMouse(currentIndex);
- }
- }
- 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 vel = new PVector(new Random().nextInt(max_vel) + 0.05, -1 * (new Random().nextInt(max_vel) + 0.05));
- PVector gre;
- 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 border() {
- if (point.y - radius <= 0 || point.y + radius >= height) vel.y *= -springness;
- if (point.x - radius <= 0 || point.x + radius >= width) vel.x *= -springness;
- }
- void speed(PVector force) {
- PVector f = force.copy();
- acceleration.add(PVector.div( f, mass));
- vel.add(acceleration);
- point.add(vel);
- gre = acceleration.copy();
- // If out of bounds
- if (point.y - radius <= 0)
- {
- point.y = 0 + radius;
- } else if (point.y + radius >= height)
- {
- if(vel.y<0.5)
- {
- vel.y = 0;
- }
- point.y = height - radius ;
- }
- if (point.x - radius <= 0)
- {
- point.x = 0 + radius;
- } else if (point.x + radius >= width)
- {
- point.x = width - radius;
- }
- acceleration.mult(0);
- }
- int showingData()
- {
- fieldName[0] = "radius: ";
- fieldValue[0] = radius;
- fieldName[1] = "mass: ";
- fieldValue[1] = mass;
- fieldName[2] = "springness: ";
- fieldValue[2] = springness;
- return 3;
- }
- void drawing() {
- fill((map(vel.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(vel.x, -max_vel, max_vel, 50, 255)));
- circle(point.x, point.y, 2 * radius);
- strokeWeight(2);
- stroke(255, 255, 255);
- PVector dir = vel.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