Rafpast

Circle1

Aug 7th, 2020
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.07 KB | None | 0 0
  1. import java.util.Random;
  2. import controlP5.*;
  3.  
  4. Random randa = new Random();
  5. ControlP5 cp5;
  6.  
  7. PVector  grav = new PVector(0.00, 5.00);
  8. PVector  air_re = new PVector(0.004, 0.004);
  9. PVector  inic = new PVector(0.0005, 0.0005);
  10. PVector air;
  11. float mass, radius;
  12. float time = 0.00;
  13. float time_step = 0.01;
  14. int button_height =30, pozX =2, button_width = 106, he = button_height, hi = 1;
  15. int pozXSet = pozX + button_width, control = 10, currentIndex = 0;
  16. boolean stopStart = false;//true - everything is working , false - everything is stoped
  17. boolean changePositionByMouse = false;
  18. boolean contextMenuOpenByMouse = false;
  19. ArrayList<Circum> cir = new ArrayList<Circum>(control);
  20. ArrayList<controlP5.Button> but = new ArrayList<controlP5.Button>(control);
  21. ArrayList<controlP5.Textfield> textField = new ArrayList<controlP5.Textfield>(control);
  22.  
  23. void setup() {
  24.   //fullScreen(P2D, 1);
  25.   size(1500, 800, P2D);
  26.   stroke(255);
  27.   background(255);
  28.   cp5 = new ControlP5(this);
  29.  
  30.   //creating buttons
  31.   cp5.addButton("Reset").setPosition(pozX, 1).setSize(button_width, button_height);
  32.   cp5.addButton("StartStop").setPosition(pozX, he * hi).setSize(button_width, button_height).setCaptionLabel("Uruchomienie programu");
  33.  
  34.  
  35.   //creating random circles
  36.   for (int i = 0; i <control; i++)
  37.   {
  38.     mass = 20 * randa.nextFloat() + 4;
  39.     radius = mass * 2;
  40.     float pozX = width * new Random().nextFloat();
  41.     float pozY = height * new Random().nextFloat();
  42.     float border= 10;
  43.  
  44.     //Checking whether circles are overlapping or outside the screen boundary.
  45.     if (i!=0)
  46.     {
  47.       for (int j = 0; j <i; j++)
  48.       {      
  49.         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) {
  50.           pozX = (width * new Random().nextFloat()) - radius;
  51.           pozY = (height * new Random().nextFloat()) - radius;
  52.         }
  53.       }
  54.     }
  55.  
  56.     cir.add(new Circum(pozX, pozY, radius, mass));
  57.     cir.get(i).vel = new PVector(-2, 5 * new Random().nextFloat() - 2.5);
  58.     cir.get(i).acceleration = new PVector(0, 0);
  59.     cir.get(i).gre = new PVector(0, 0);
  60.   }
  61.  
  62.    for ( int j = 0; j< cir.get(1).showingData(); j++)
  63.     {
  64.      
  65.       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());
  66.     }
  67.    
  68. }
  69.  
  70. void drawBorders()
  71. {
  72.   strokeWeight(6);
  73.   stroke(200, 0, 10);
  74.   line(0, height, width, height);
  75.   line(0, 0, width, 0);
  76.   line(0, 0, 0, height);
  77.   line(width, 0, width, height);
  78.   strokeWeight(1);
  79. }
  80.  
  81. void contextMenu(int currentIndex)
  82. {
  83.   int i = currentIndex;
  84.  
  85.   if (contextMenuOpenByMouse)
  86.   {
  87.  
  88.     int numberOfFields = cir.get(i).showingData();
  89.  
  90.     for ( int j = 0; j< numberOfFields; j++)
  91.     {
  92.      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());
  93.      // 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]));
  94.     }
  95.   }
  96. }
  97.  
  98. void Reset()
  99. {
  100.   background(255);
  101.   ball_setup();
  102. }
  103.  
  104. void StartStop() {
  105.  
  106.   stopStart = !stopStart;
  107.  
  108.   if (stopStart)
  109.   {
  110.     cp5.getController("StartStop").setCaptionLabel("Zatrzymanie programu");
  111.   } else
  112.   {
  113.     cp5.getController("StartStop").setCaptionLabel("Uruchomienie programu");
  114.   }
  115. }
  116. void ball_setup()
  117. {
  118.   for (int i = control - 1; i >= 0; i--)
  119.   {
  120.     cir.remove(i);
  121.   }
  122.  
  123.   for (int i = 0; i<control; i++)
  124.   {
  125.     mass = 8 * randa.nextFloat() + 4;
  126.     radius = mass  * 4;
  127.     float pozX = width * new Random().nextFloat();
  128.     float pozY = height * new Random().nextFloat();
  129.     float border= 10;
  130.  
  131.     //Checking whether circles are overlapping or outside the screen boundary.
  132.     if (i!=0)
  133.     {
  134.       for (int j = 0; j <i; j++)
  135.       {      
  136.         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) {
  137.           pozX = (width * new Random().nextFloat());
  138.           pozY = (height * new Random().nextFloat());
  139.         }
  140.       }
  141.     }
  142.  
  143.     cir.add(new Circum( pozX, pozY, radius, mass));
  144.     cir.get(i).speed(inic);
  145.   }
  146. }
  147.  
  148. void mousePressed() {
  149.   if (!stopStart)
  150.   {
  151.     for (int i = 0; i<cir.size(); i++)
  152.     {
  153.       float d = dist(mouseX, mouseY, cir.get(i).point.x, cir.get(i).point.y);
  154.       if (d <= cir.get(i).radius ) {
  155.         if (mouseButton == LEFT)
  156.         {
  157.           changePositionByMouse = true;
  158.         } else if (mouseButton == RIGHT)
  159.         {
  160.           contextMenuOpenByMouse = true;
  161.           contextMenu(i);
  162.         }
  163.         currentIndex = i;
  164.       }
  165.     }
  166.   }
  167. }
  168.  
  169. void mouseReleased() {
  170.   if (!stopStart)
  171.   {
  172.     if (changePositionByMouse) {
  173.       changePositionByMouse = false;
  174.     }else if(contextMenuOpenByMouse)
  175.     {
  176.       contextMenuOpenByMouse = false;
  177.       textField.get(currentIndex).getTab().hide();
  178.     }
  179.   }
  180. }
  181.  
  182. void mouseDragged()
  183. {
  184.   if (mouseButton == RIGHT)
  185.   {
  186.     // currentIndex
  187.   }
  188. }
  189.  
  190. void changePositionByTheMouse(int currentIndex)
  191. {
  192.   int i = currentIndex;
  193.   float border1= 5, border2= 6;
  194.  
  195.   if (changePositionByMouse)
  196.   {
  197.  
  198.     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)
  199.     {
  200.       if (cir.get(i).point.y - radius <= 0)
  201.       {
  202.         cir.get(i).point.y = border2 + radius;
  203.         cir.get(i).point.x = mouseX;
  204.       } else if (cir.get(i).point.y + radius >= height)
  205.       {
  206.         cir.get(i).point.y = height - radius - border2;
  207.         cir.get(i).point.x = mouseX;
  208.       }
  209.       if (cir.get(i).point.x - radius <= 0)
  210.       {
  211.         cir.get(i).point.x = border2 + radius;
  212.         cir.get(i).point.y = mouseY;
  213.       } else if (cir.get(i).point.x + radius >= width)
  214.       {
  215.         cir.get(i).point.x = width - radius - border2;
  216.         cir.get(i).point.y = mouseY;
  217.       }
  218.     } else
  219.     {
  220.       cir.get(i).point.x = mouseX;
  221.       cir.get(i).point.y = mouseY;
  222.     }
  223.  
  224.  
  225.  
  226.     cir.get(i).vel.mult(0);
  227.   }
  228. }
  229.  
  230.  
  231. void collision(Circum con1, Circum con2, float dist_, float min_, PVector lock)
  232. {
  233.   float u1, u2, distance = dist_, min_dyst = min_;
  234.  
  235.   // Defining the X axis
  236.   PVector dirX = lock.copy();
  237.   dirX.normalize();
  238.   // Defining the Y axis
  239.   PVector dirY = new PVector(dirX.y, -dirX.x);
  240.  
  241.   // X coordinates of velocities
  242.   float vx1 = dirX.dot(con1.vel);
  243.   float vx2 = dirX.dot(con2.vel);
  244.   // Y coordinates of velocities
  245.   float vy1 = dirY.dot(con1.vel);
  246.   float vy2 = dirY.dot(con2.vel);
  247.  
  248.   // Applying the collision to X coordinates
  249.   u1 = (2 * vx2 * con2.mass + vx1 * (con1.mass - con2.mass)) / (con1.mass + con2.mass);
  250.   u2 = (2 * vx1 * con1.mass + vx2 * (con2.mass - con1.mass)) / (con1.mass + con2.mass);
  251.  
  252.   // Turning velocities back into vectors
  253.   PVector vel1 = PVector.mult(dirX, u1);
  254.   PVector vel2 = PVector.mult(dirX, u2);
  255.   vel1.add(PVector.mult(dirY, vy1));
  256.   vel2.add(PVector.mult(dirY, vy2));
  257.  
  258.   con1.vel = vel1;
  259.   con2.vel = vel2;
  260.  
  261.   float distanceCorrection = (min_dyst-distance)/2.0;
  262.   PVector correctionVector = lock.normalize().mult(distanceCorrection);
  263.   con2.point.add(correctionVector);
  264.   con1.point.sub(correctionVector);
  265. }
  266.  
  267.  
  268. void checkForCollision(int i, int j)
  269. {
  270.   PVector lock = PVector.sub( cir.get(j).point, cir.get(i).point);
  271.   float oldDist = lock.mag();
  272.   float min_dyst = cir.get(j).radius + cir.get(i).radius;
  273.  
  274.   if (oldDist <= min_dyst)
  275.   {    
  276.     collision(cir.get(i), cir.get(j), oldDist, min_dyst, lock);
  277.     println("Collision between: ", j, "and", i);
  278.   }
  279. }
  280.  
  281.  
  282.  
  283. void draw() {
  284.   background(200);
  285.   //translate(width / 2, height / 2);
  286.   drawBorders();
  287.  
  288.  
  289.   for (int k = 0; k<cir.size(); k++)
  290.   {
  291.     if (stopStart)
  292.     {
  293.       cir.get(k).speed(grav);
  294.       air = air_re.copy();
  295.  
  296.       if (cir.get(k).vel.x>0)
  297.       {
  298.         air.mult(-1);
  299.       }
  300.  
  301.       cir.get(k).speed(air);
  302.     }
  303.     for (int j = k + 1; j<cir.size(); j++)
  304.     {
  305.       if (k != cir.size() - 1)
  306.       {
  307.         checkForCollision(k, j);
  308.       }
  309.     }
  310.  
  311.     cir.get(k).border();
  312.     cir.get(k).drawing();
  313.   }
  314.  
  315.  
  316.   if (!stopStart)
  317.   {
  318.     changePositionByTheMouse(currentIndex);
  319.    
  320.   }
  321. }
  322.  
  323.  
  324. class Circum {
  325.   int max_vel = 15;
  326.   float radius, mass;
  327.   float ref = 0.5;
  328.   float springness = 0.9999;
  329.   PVector  point = new PVector(0, 0);
  330.   PVector  acceleration = new PVector(0, 0);
  331.   PVector  vel = new PVector(new Random().nextInt(max_vel) + 0.05, -1 * (new Random().nextInt(max_vel) + 0.05));
  332.   PVector gre;
  333.   String[] fieldName = new String[3];
  334.   float[] fieldValue  = new float[3];
  335.   ;
  336.  
  337.   Circum() {
  338.     mass = 5 * new Random().nextFloat() + 1;
  339.     radius = mass * 4;
  340.     point.x = (width * new Random().nextFloat()) - radius;
  341.     point.y =  (height * new Random().nextFloat()) - radius;
  342.   }
  343.  
  344.   Circum(float x_, float y_, float r_, float m_) {
  345.     this();
  346.     point.x = x_;
  347.     point.y = y_;
  348.     radius = r_;
  349.     mass = m_;
  350.   }
  351.  
  352.   void border() {
  353.     if (point.y - radius <= 0 || point.y + radius >= height) vel.y *= -springness;
  354.     if (point.x - radius <= 0 || point.x + radius >= width) vel.x *= -springness;
  355.   }
  356.  
  357.   void speed(PVector force) {
  358.     PVector f = force.copy();
  359.     acceleration.add(PVector.div( f, mass));
  360.     vel.add(acceleration);
  361.     point.add(vel);
  362.     gre = acceleration.copy();
  363.  
  364.     // If out of bounds
  365.     if (point.y - radius <= 0)
  366.     {
  367.       point.y = 0 + radius;
  368.     } else if (point.y + radius >= height)
  369.     {
  370.       if(vel.y<0.5)
  371.       {
  372.         vel.y = 0;
  373.       }
  374.       point.y = height - radius ;
  375.     }
  376.     if (point.x - radius <= 0)
  377.     {
  378.       point.x = 0 + radius;
  379.     } else if (point.x + radius >= width)
  380.     {
  381.       point.x = width - radius;
  382.     }
  383.     acceleration.mult(0);
  384.   }
  385.  
  386.   int showingData()
  387.   {
  388.  
  389.     fieldName[0]  = "radius: ";
  390.     fieldValue[0] = radius;
  391.     fieldName[1]  = "mass: ";
  392.     fieldValue[1] = mass;
  393.     fieldName[2]  = "springness: ";
  394.     fieldValue[2] = springness;
  395.  
  396.     return 3;
  397.   }
  398.  
  399.   void drawing() {
  400.  
  401.     fill((map(vel.y, -max_vel, max_vel, 50, 255)), (map(gre.y, -2, 2, 50, 100)), (map(point.y, 0, width, 1, 255)) );
  402.     stroke((map(point.x, 0, height, 100, 255)), (map(gre.x, -2, 2, 50, 100)), (map(vel.x, -max_vel, max_vel, 50, 255)));
  403.     circle(point.x, point.y, 2 * radius);
  404.  
  405.     strokeWeight(2);
  406.     stroke(255, 255, 255);
  407.     PVector dir = vel.copy();
  408.     dir.normalize().mult(radius);
  409.     dir = PVector.add(dir, point);
  410.     line(point.x, point.y, dir.x, dir.y);
  411.   }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment