Advertisement
Rafpast

changing_speed_of_balls_with_scroll

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