Rafpast

changing_speed_of_balls_with_scroll_eng

Nov 12th, 2020
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.14 KB | None | 0 0
  1. import java.util.Random;
  2. import controlP5.*;
  3.  
  4. Random randa = new Random();
  5. ControlP5 cp5;
  6.  
  7. int button_height =30, pozX =2, button_width = 106, h = 0, hi = 1;
  8. int pozXSet = pozX + button_width, control = 10, currentIndex = 0;
  9. int control2 = control, MaxFar = 100, Multi = 10, start;
  10. float mass, radius, G_const = 0.6673, scrollMovement = 0, pozYSet[] = new float[2];
  11. boolean stopStart = false;//true - everything is working , false - everything is stoped
  12. boolean field = false;//true -  central field , false -  homogeneous field
  13. boolean information = false;
  14. boolean[] scrollMenuOpenByMouse = new boolean[5];
  15. ArrayList<Circum> cir = new ArrayList<Circum>(control);
  16. PVector  grav = new PVector(0.00, 5.00);
  17. PVector  air_re = new PVector(0.004, 0.004);
  18. PVector  inic = new PVector(0.0005, 0.0005);
  19. PVector air;
  20. String buttonName;
  21.  
  22. void setup() {
  23.   //fullScreen(P2D, 1);
  24.   size(1500, 800, P2D);
  25.   stroke(255);
  26.   background(255);
  27.  
  28.   cp5 = new ControlP5(this);
  29.  
  30.   //creating buttons
  31.  
  32.   cp5.addButton("Menu").setPosition(pozX, 1).setSize(button_width, button_height);
  33.   cp5.addButton("Reset").setPosition(pozX, button_height).setSize(button_width, button_height);
  34.   hi++;
  35.   cp5.addButton("StartStop").setPosition(pozX, button_height * hi).setSize(button_width, button_height).setCaptionLabel("Start the program");
  36.   hi++;
  37.   cp5.addButton("Homogen").setPosition(pozX, button_height * hi).setSize(button_width, button_height).setCaptionLabel("Homogeneous field").setVisible(false).setValue(1);
  38.   hi++;
  39.   cp5.addButton("Central").setPosition(pozX, button_height * hi).setSize(button_width, button_height).setCaptionLabel("Central field").setVisible(false).setValue(1);
  40.  
  41.   //creating random circles
  42.   for (int i = 0; i <control; i++)
  43.   {
  44.     mass = 20 * randa.nextFloat() + 4;
  45.     radius = mass * 2;
  46.     float pozX = width * new Random().nextFloat();
  47.     float pozY = height * new Random().nextFloat();
  48.     float border= 10;
  49.  
  50.     //Checking whether circles are overlapping or outside the screen boundary.
  51.     if (i!=0)
  52.     {
  53.       for (int j = 0; j <i; j++)
  54.       {      
  55.         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) {
  56.           pozX = (width * new Random().nextFloat()) - radius;
  57.           pozY = (height * new Random().nextFloat()) - radius;
  58.         }
  59.       }
  60.     }
  61.  
  62.     cir.add(new Circum(pozX, pozY, radius, mass));
  63.     cir.get(i).velocity = new PVector(0, 0);
  64.     cir.get(i).acceleration = new PVector(0, 0);
  65.     cir.get(i).gre = new PVector(0, 0);
  66.   }
  67. }
  68.  
  69. void StartStop() {
  70.  
  71.   stopStart = !stopStart;
  72.  
  73.   if (stopStart)
  74.   {
  75.     cp5.getController("StartStop").setCaptionLabel("Stopping the program");
  76.   } else
  77.   {
  78.     cp5.getController("StartStop").setCaptionLabel("Start the program");
  79.   }
  80. }
  81.  
  82. void ball_setup()
  83. {
  84.   for (int i = control - 1; i >= 0; i--)
  85.   {
  86.     cir.remove(i);
  87.   }
  88.  
  89.   for (int i = 0; i<control; i++)
  90.   {
  91.     mass = 8 * randa.nextFloat() + 4;
  92.     radius = mass  * 4;
  93.     float pozX = width * new Random().nextFloat();
  94.     float pozY = height * new Random().nextFloat();
  95.     float border= 10;
  96.  
  97.     //Checking whether circles are overlapping or outside the screen boundary.
  98.     if (i!=0)
  99.     {
  100.       for (int j = 0; j <i; j++)
  101.       {      
  102.         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) {
  103.           pozX = (width * new Random().nextFloat());
  104.           pozY = (height * new Random().nextFloat());
  105.         }
  106.       }
  107.     }
  108.  
  109.     cir.add(new Circum( pozX, pozY, radius, mass));
  110.     cir.get(i).velocity = new PVector(0,0);
  111.     cir.get(i).acceleration = new PVector(0, 0);
  112.     cir.get(i).gre = new PVector(0, 0);
  113.   }
  114. }
  115.  
  116. void checkForCollision()
  117. {
  118.   for (int i = 0; i<cir.size() -1; i++)
  119.   {
  120.     for (int j = i + 1; j<cir.size(); j++)
  121.     {
  122.       //calculating distance between object
  123.       PVector lengthFrom_i_to_j= PVector.sub( cir.get(j).point, cir.get(i).point);
  124.       float oldDist = lengthFrom_i_to_j.mag();
  125.       float min_dyst = cir.get(j).radius + cir.get(i).radius;
  126.       //checking for collision
  127.       if (oldDist <= min_dyst)
  128.       {    
  129.         collision(cir.get(i), cir.get(j), oldDist, min_dyst, lengthFrom_i_to_j);
  130.       }
  131.     }
  132.   }
  133. }
  134.  
  135. void collision(Circum con1, Circum con2, float dist_, float min_, PVector lock)
  136. {
  137.   float u1, u2, distance = dist_, min_dyst = min_;
  138.  
  139.   //static collision
  140.   float distanceCorrection = (min_dyst-distance)/2.0;
  141.   PVector correctionVector = lock.normalize().mult(distanceCorrection);
  142.   con2.point.add(correctionVector);
  143.   con1.point.sub(correctionVector);
  144.  
  145.   //dynamic collision
  146.  
  147.   // Defining the X axis
  148.   PVector dirX = lock.copy();
  149.   dirX.normalize();
  150.   // Defining the Y axis
  151.   PVector dirY = new PVector(dirX.y, -dirX.x);
  152.  
  153.   // X coordinates of velocities
  154.   float vx1 = dirX.dot(con1.velocity);
  155.   float vx2 = dirX.dot(con2.velocity);
  156.   // Y coordinates of velocities
  157.   float vy1 = dirY.dot(con1.velocity);
  158.   float vy2 = dirY.dot(con2.velocity);
  159.  
  160.   // Applying the collision to X coordinates
  161.   u1 = (2 * vx2 * con2.mass + vx1 * (con1.mass - con2.mass)) / (con1.mass + con2.mass);
  162.   u2 = (2 * vx1 * con1.mass + vx2 * (con2.mass - con1.mass)) / (con1.mass + con2.mass);
  163.  
  164.   // Turning velocities back into vectors
  165.   PVector vel1 = PVector.mult(dirX, u1);
  166.   PVector vel2 = PVector.mult(dirX, u2);
  167.   vel1.add(PVector.mult(dirY, vy1));
  168.   vel2.add(PVector.mult(dirY, vy2));
  169.  
  170.   con1.velocity = vel1;
  171.   con2.velocity = vel2;
  172. }
  173.  
  174. void centralField()
  175. {
  176.   float dem = 0, dividerx = 0, dividery = 0, Distance;
  177.   PVector grav_atract = new PVector(0, 0);
  178.   ArrayList<PVector> reverseGravity = new ArrayList<PVector>(cir.size());
  179.  
  180.   if (stopStart)
  181.   {
  182.     for (int i = 0; i < cir.size(); i++)
  183.     {
  184.       reverseGravity.add(new PVector(0, 0));
  185.     }
  186.  
  187.     for (int i = 0; i<cir.size(); i++)
  188.     {
  189.       //Calculation of the mass center
  190.       dem += cir.get(i).mass;
  191.       dividerx += cir.get(i).mass * cir.get(i).point.x;
  192.       dividery += cir.get(i).mass * cir.get(i).point.y;
  193.  
  194.       for (int j = i + 1; j<cir.size(); j++)
  195.       {
  196.  
  197.         //calculating distance between object
  198.         PVector lengthFrom_i_to_j= PVector.sub( cir.get(j).point, cir.get(i).point);
  199.         PVector lengthFrom_j_to_i= PVector.sub( cir.get(i).point, cir.get(j).point);
  200.  
  201.         Distance = lengthFrom_i_to_j.mag();
  202.         lengthFrom_i_to_j.normalize();
  203.         lengthFrom_j_to_i.normalize();
  204.  
  205.         //adding gravity
  206.         float strength = (G_const * cir.get(i).mass * cir.get(j).mass)/(Distance * Distance);
  207.         lengthFrom_i_to_j.mult(strength );
  208.         lengthFrom_j_to_i.mult(strength );
  209.  
  210.         reverseGravity.get(j).add(lengthFrom_j_to_i);
  211.  
  212.         grav_atract.add(lengthFrom_i_to_j);
  213.       }
  214.  
  215.       grav_atract.add( reverseGravity.get(i));
  216.       cir.get(i).setSpeed(grav_atract);
  217.       grav_atract.mult(0);
  218.  
  219.       for (int k = 0; k<reverseGravity.size(); k++)
  220.       {
  221.         reverseGravity.get(k).mult(0);
  222.       }
  223.  
  224.       //checkForCollision();
  225.     }
  226.   }
  227.  
  228.   checkForCollision();
  229.  
  230.   for (int i = 0; i<cir.size(); i++)
  231.   {  
  232.     //cir.get(i).border();
  233.     cir.get(i).drawing();
  234.   }
  235.  
  236.   //Drawing a center of mass
  237.   pushMatrix();
  238.   strokeWeight(6);
  239.   fill(50, 0, 255 );
  240.   stroke(255, 0, 50);
  241.   point((dividerx/dem), (dividery/dem));
  242.   popMatrix();
  243. }
  244.  
  245. void homogeneousField()
  246. {
  247.   PVector  air_replica = new PVector(0.004, 0.004);
  248.   PVector  grav = new PVector(0.00, 3.00);
  249.   PVector air;
  250.  
  251.   if (stopStart)
  252.   {
  253.     for (int i = 0; i<cir.size(); i++)
  254.     {//Adding gravity
  255.       cir.get(i).setSpeed(grav);
  256.       air = air_replica.copy();
  257.  
  258.       if (cir.get(i).velocity.x>0 ||cir.get(i).velocity.y>0)
  259.       {
  260.         air.mult(-1);
  261.       }
  262.       //adding motion resistance
  263.       cir.get(i).setSpeed(air);
  264.     }
  265.   }
  266.  
  267.   checkForCollision();
  268.  
  269.   for (int j = 0; j<cir.size(); j++)
  270.   {
  271.     //cir.get(j).border();
  272.     cir.get(j).drawing();
  273.   }
  274. }
  275.  
  276. void mouseWheel(MouseEvent event) {
  277.   start = millis();
  278.   scrollMovement = constrain(event.getCount()*Multi + scrollMovement, -MaxFar, MaxFar);
  279. }
  280.  
  281. void mousePressed() {
  282.  
  283.   if (!stopStart)
  284.   {
  285.     if (field)
  286.     {
  287.       for (int i = 0; i<cir.size(); i++)
  288.       {
  289.         float d = dist(mouseX, mouseY, cir.get(i).point.x, cir.get(i).point.y);
  290.         if (d <= cir.get(i).radius ) {
  291.           currentIndex = i;
  292.           if (mouseButton == CENTER)
  293.           {
  294.             scrollMenuOpenByMouse[0] = true;
  295.           }
  296.         }
  297.       }
  298.     } else
  299.     {
  300.       for (int j = 0; j<cir.size(); j++)
  301.       {
  302.         float d = dist(mouseX, mouseY, cir.get(j).point.x, cir.get(j).point.y);
  303.         if (d <= cir.get(j).radius) {
  304.           currentIndex = j;
  305.           if (mouseButton == CENTER)
  306.           {
  307.             scrollMenuOpenByMouse[1] = true;
  308.           }
  309.         }
  310.       }
  311.     }
  312.   }
  313. }
  314.  
  315. void mouseReleased() {
  316.   if (!stopStart)
  317.   {
  318.     if (field)
  319.     {  
  320.       if (scrollMenuOpenByMouse[0]) {
  321.         scrollMenuOpenByMouse[0] = false;
  322.       }
  323.     } else
  324.     {
  325.       if (scrollMenuOpenByMouse[1]) {
  326.         scrollMenuOpenByMouse[1] = false;
  327.       }
  328.     }
  329.   }
  330. }
  331.  
  332. void changeVelocityByTheMouse(int currentIndex)
  333. {
  334.   int i = currentIndex;
  335.   PVector diff = new PVector(0, 0), position= new PVector(0, 0), result = new PVector(0, 0);
  336.  
  337.   if (!stopStart)
  338.   {
  339.     if (field)
  340.     {
  341.       if (scrollMenuOpenByMouse[0])
  342.       {
  343.         position = cir.get(currentIndex).point;
  344.         diff = PVector.sub(new PVector(mouseX, mouseY), position);
  345.         diff.normalize();
  346.         diff.mult(scrollMovement);
  347.         result.add(diff);
  348.       } else
  349.       {
  350.         cir.get(currentIndex).acceleration.add(result);
  351.       }
  352.     } else
  353.     {
  354.       if (scrollMenuOpenByMouse[1])
  355.       {
  356.         position = cir.get(currentIndex).point;
  357.         diff = PVector.sub(new PVector(mouseX, mouseY), position);
  358.         diff.normalize();
  359.         diff.mult(scrollMovement);
  360.         result.add(diff);
  361.       } else
  362.       {
  363.         cir.get(currentIndex).acceleration.add(result);
  364.       }
  365.     }
  366.   }
  367.  
  368.  
  369.   line(position.x, position.y, result.x + position.x, result.y + position.y);
  370.   circle(diff.x, diff.y, 10);
  371.   result.set(0, 0);
  372. }
  373.  
  374. void Homogen() {
  375.  
  376.   CentralButton(false);
  377. }
  378.  
  379. void Central() {
  380.  
  381.   CentralButton(true);
  382. }
  383.  
  384. void Reset()
  385. {
  386.   background(255);
  387.   ball_setup();
  388. }
  389.  
  390. void CentralButton(boolean i)
  391. {
  392.     field = i;
  393.     setting();
  394. }
  395.  
  396. void Menu()
  397. {
  398.   information = !information;
  399. }
  400.  
  401.  
  402. void Disclosures_set(String name)
  403. {
  404.   pozYSet = cp5.getController(name).getPosition();
  405.   cp5.getController("Set").setPosition(pozXSet, pozYSet[1]);
  406.   cp5.getController("input").setPosition(pozXSet, pozYSet[1]+button_height);
  407.   buttonName = name;  
  408.   cp5.getController("Set").show();
  409.   cp5.getController("input").show();
  410. }
  411.  
  412. void ButtonManagement()
  413. {
  414.   if (information)
  415.   {
  416.     cp5.get(Button.class, "Homogen").setVisible(true);
  417.     cp5.get(Button.class, "Central").setVisible(true);
  418.  
  419.   } else  if (information == false)
  420.   {
  421.     cp5.get(Button.class, "Homogen").setVisible(false);
  422.     cp5.get(Button.class, "Central").setVisible(false);
  423.   }
  424. }
  425.  
  426. void setting()
  427. {
  428.     float pozX = 0, pozY = 0;
  429.  
  430.     for (int i = control - 1; i >= 0; i--)
  431.     {
  432.       cir.remove(i);
  433.     }
  434.  
  435.     if ( field == true)
  436.     {
  437.  
  438.       for (int i = 0; i <control2; i++)
  439.       {
  440.         mass = 8 * randa.nextFloat() + 4;
  441.         radius = mass  * 4;
  442.         pozX = width * new Random().nextFloat();
  443.         pozY = height * new Random().nextFloat();
  444.         float border= 10;
  445.  
  446.         //Checking whether circles are overlapping or outside the screen boundary.
  447.         if (i!=0)
  448.         {
  449.           for (int j = 0; j <i; j++)
  450.           {      
  451.             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) {
  452.               pozX = (width * new Random().nextFloat());
  453.               pozY = (height * new Random().nextFloat());
  454.             }
  455.           }
  456.         }
  457.  
  458.         cir.add(new Circum(pozX, pozY, radius, mass));
  459.         cir.get(i).velocity = new PVector(0, 0);
  460.       }
  461.       control = control2;
  462.     } else if ( field == false)
  463.     {
  464.  
  465.       for (int i = 0; i<control2; i++)
  466.       {
  467.         mass = 8 * randa.nextFloat() + 4;
  468.         radius = mass  * 2;
  469.         pozX = (width * new Random().nextFloat()) - radius;
  470.         pozY =  (height * new Random().nextFloat()) - radius;
  471.  
  472.         cir.add(new Circum( pozX, pozY, radius, mass));
  473.         cir.get(i).setSpeed(inic);
  474.       }
  475.       control = control2;
  476.     }
  477.   }
  478.  
  479. void draw() {
  480.   background(200);
  481.  
  482.   if (field)
  483.   {
  484.     centralField();
  485.   } else
  486.   {
  487.     homogeneousField();
  488.   }
  489.  
  490.    if (!stopStart)
  491.   {
  492.     changeVelocityByTheMouse(currentIndex);
  493.   }
  494.  
  495.   ButtonManagement();
  496. }
  497.  
  498. class Circum {
  499.   int max_vel = 15;
  500.   float radius, mass;
  501.   float ref = 0.5;
  502.   float springness = 0.9999;
  503.   PVector  point = new PVector(0, 0);
  504.   PVector  acceleration = new PVector(0, 0);
  505.   PVector  velocity = new PVector(new Random().nextInt(max_vel) + 0.05, -1 * (new Random().nextInt(max_vel) + 0.05));
  506.   PVector gre = new PVector(0, 0);
  507.   String[] fieldName = new String[3];
  508.   float[] fieldValue  = new float[3];
  509.  
  510.   Circum() {
  511.     mass = 5 * new Random().nextFloat() + 1;
  512.     radius = mass * 4;
  513.     point.x = (width * new Random().nextFloat()) - radius;
  514.     point.y =  (height * new Random().nextFloat()) - radius;
  515.   }
  516.  
  517.   Circum(float x_, float y_, float r_, float m_) {
  518.     this();
  519.     point.x = x_;
  520.     point.y = y_;
  521.     radius = r_;
  522.     mass = m_;
  523.   }
  524.  
  525.   void setSpeed(PVector force) {
  526.     PVector f = force.copy();
  527.     acceleration.add(PVector.div( f, mass));
  528.     velocity.add(acceleration);
  529.     point.add(velocity);
  530.     gre = acceleration.copy();
  531.  
  532.     // If out of bounds
  533.     if (point.y - radius <= 0)
  534.     {
  535.       velocity.y *= -springness;
  536.       point.y = 0 + radius;
  537.     } else if (point.y + radius >= height)
  538.     {
  539.       velocity.y *= -springness;
  540.       point.y = height - radius ;
  541.     }
  542.     if (point.x - radius <= 0)
  543.     {
  544.       velocity.x *= -springness;
  545.       point.x = 0 + radius;
  546.     } else if (point.x + radius >= width)
  547.     {
  548.       velocity.x *= -springness;
  549.       point.x = width - radius;
  550.     }
  551.  
  552.     acceleration.mult(0);
  553.   }
  554.  
  555.   void drawing() {
  556.  
  557.     fill((map(velocity.y, -max_vel, max_vel, 50, 255)), (map(gre.y, -2, 2, 50, 100)), (map(point.y, 0, width, 1, 255)) );
  558.     stroke((map(point.x, 0, height, 100, 255)), (map(gre.x, -2, 2, 50, 100)), (map(velocity.x, -max_vel, max_vel, 50, 255)));
  559.     circle(point.x, point.y, 2 * radius);
  560.  
  561.     strokeWeight(2);
  562.     stroke(255, 255, 255);
  563.     PVector dir = velocity.copy();
  564.     dir.normalize().mult(radius);
  565.     dir = PVector.add(dir, point);
  566.     line(point.x, point.y, dir.x, dir.y);
  567.   }
  568. }
Advertisement
Add Comment
Please, Sign In to add comment