Advertisement
Rafpast

N-pendulum 2

Dec 28th, 2020
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. Random randa = new Random();
  4. int numberOfNpendulum = 1, frameStartTime, frameEndTime;
  5. float delta_time, now = System.nanoTime();
  6. boolean stop = true;
  7. NPendulum nPend;
  8.  
  9. void setup() {  
  10.   size(1920, 1060, P2D);
  11.  
  12.   ArrayList<Float> nPendLenghts = new ArrayList<Float>(numberOfNpendulum);
  13.   ArrayList<Float> nPendAngles  = new ArrayList<Float>(numberOfNpendulum);
  14.  
  15.   //creating a random npendulum
  16.   nPendLenghts.add(0.0);
  17.   nPendAngles.add(0.0);
  18.  
  19.   for (int i = 0; i<numberOfNpendulum; i++)
  20.   {
  21.     nPendLenghts.add(random(45,150));
  22.     nPendAngles.add(random(TWO_PI));
  23.   }
  24.  
  25.   nPend = new NPendulum(numberOfNpendulum, nPendLenghts, nPendAngles);
  26. }
  27.  
  28. void keyPressed()
  29. {
  30.   if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
  31.   {
  32.     if (key == 'n' ||key == 'N')
  33.     {
  34.       nPend.reset(numberOfNpendulum);
  35.     } else if(key == 's' ||key == 'S')
  36.     {
  37.       stop = !stop;
  38.     }
  39.   }
  40. }
  41.  
  42. void calc_delta_time() {
  43.   delta_time = (System.nanoTime()-now)/100000000;
  44.   now = System.nanoTime();
  45. }
  46.  
  47. void draw() {
  48.   background(120);
  49.  
  50.   if (stop)
  51.   {
  52.     nPend.calculateAcceleration();
  53.      nPend.calculatePosition();
  54.   }
  55.   nPend.show();
  56.  
  57.   String advice =" Reset - N\n Zatrzymanie programu - S";
  58.   fill(0);
  59.   textSize(20);
  60.   text(advice,0,0, 660, 330);
  61.   frameStartTime = millis();
  62. }
  63.  
  64. class NPendulum {
  65.   ArrayList<Circum> bob = new ArrayList<Circum>();
  66.   FloatList angleAcceleration = new FloatList();
  67.   FloatList angleVelocity = new FloatList();
  68.   FloatList lenghts = new FloatList();
  69.   FloatList angles = new FloatList();
  70.   PVector origin = new PVector(width / 2, height/3);
  71.   float g = 0.0004905000, damping = 0.998;
  72.   int amount = 0;
  73.  
  74.   NPendulum(int amount_, ArrayList<Float> lenghts_, ArrayList<Float> angles_)
  75.   {
  76.     amount = amount_ + 1;
  77.     float mass = 10, radius = 20;
  78.  
  79.     for (int i = 0; i <lenghts_.size(); i++)
  80.     {
  81.       lenghts.append(lenghts_.get(i));
  82.     }  
  83.  
  84.     for (int i = 0; i <angles_.size(); i++)
  85.     {
  86.       angles.append(angles_.get(i));
  87.     }  
  88.  
  89.     bob.add(new Circum(origin.x, origin.y, radius, mass));
  90.  
  91.     for (int i = 1; i <amount; i++)
  92.     {
  93.       mass = random(10, 30);
  94.       radius = mass * 2;
  95.       bob.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
  96.       bob.get(i).point.add(bob.get(i - 1).point);
  97.       angleAcceleration.append(0);
  98.       angleVelocity.append(0);
  99.     }
  100.   }
  101.  
  102.   void reset(int number)
  103.   {
  104.     float mass = 10, radius = 20;
  105.  
  106.     lenghts.clear();
  107.     angles.clear();
  108.  
  109.     for (int i = bob.size() - 1; i >= 0; i--)
  110.     {
  111.       bob.remove(i);
  112.     }
  113.  
  114.     number++;
  115.     lenghts.append(0.0);
  116.     angles.append(0.0);
  117.  
  118.     bob.add(new Circum(origin.x, origin.y, radius, mass));
  119.  
  120.     for (int i = 1; i <number; i++)
  121.     {      
  122.       lenghts.append(random(bob.get(i - 1).radius * 2, 150));
  123.       angles.append(random(TWO_PI));
  124.       mass = 8 * randa.nextFloat() + 4;
  125.       radius = mass * 4;
  126.  
  127.       bob.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
  128.       bob.get(i).point.add(bob.get(i - 1).point);
  129.       bob.get(i).acceleration.set(0, 0);
  130.       bob.get(i).velocity.set(0, 0);
  131.     }
  132.   }
  133.  
  134.   int sigma(int j, int k)
  135.   {
  136.     return int(j<=k);
  137.   }
  138.  
  139.   int  phi(int j, int k)
  140.   {
  141.     return int(!(j==k));
  142.   }
  143.  
  144.  
  145.   void calculateAcceleration()
  146.   {
  147.     float dividend = 1, divider = 0;
  148.     float[] copyAcceleration = new float[bob.size()];
  149.  
  150.     for (int i = 0; i <bob.size() - 1; i++)
  151.     {
  152.       copyAcceleration[i] = angleAcceleration.get(i);
  153.     }
  154.  
  155.     for (int i = 0; i <bob.size() - 1; i++)
  156.     {
  157.       for (int j = 0; j <bob.size() - 1; j++)
  158.       {
  159.         dividend+= angles.get(j);
  160.       }
  161.  
  162.       for (int k = 0; k <bob.size() - 1; k++)
  163.       {
  164.         divider += bob.get(k).mass * lenghts.get(bob.size() - 1) * lenghts.get(bob.size() - 1) * sigma(bob.size(), k);
  165.       }
  166.  
  167.  
  168.       copyAcceleration[i] = -dividend/divider;
  169.     }
  170.  
  171.     for (int i = 0; i <bob.size() - 1; i++)
  172.     {
  173.       angleAcceleration.set(i, copyAcceleration[i]);
  174.     }
  175.   }
  176.  
  177.   void calculatePosition()
  178.   {
  179.     for (int i = 1; i <bob.size() - 1; i++)
  180.     {
  181.       bob.get(i).point.set(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)));
  182.       bob.get(i).point.add(bob.get(i - 1).point);
  183.       angleVelocity.add(i, angleAcceleration.get(i));
  184.       angles.add(i, angleVelocity.get(i));
  185.     }
  186.   }  
  187.  
  188.   void show()
  189.   {
  190.     bob.get(0).drawing();
  191.  
  192.     for (int i = 1; i <bob.size(); i++)
  193.     {
  194.       line(bob.get(i - 1).point.x, bob.get(i - 1).point.y, bob.get(i).point.x, bob.get(i).point.y);
  195.       bob.get(i).drawing();
  196.     }
  197.   }
  198. }
  199.  
  200. class Circum {
  201.   int max_velocity = 15;
  202.   float radius, mass, ref = 0.5, springness = 0.9999;
  203.   PVector  point = new PVector(0, 0);
  204.   PVector  acceleration = new PVector(0, 0);
  205.   PVector  velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));
  206.   PVector gre = new PVector(0, 0);
  207.  
  208.   Circum() {
  209.     mass = 5 * new Random().nextFloat() + 1;
  210.     radius = mass * 4;
  211.     point.x = (width * new Random().nextFloat()) - radius;
  212.     point.y = (height * new Random().nextFloat()) - radius;
  213.   }
  214.  
  215.   Circum(float x_, float y_, float radius_, float mass_) {
  216.     this();
  217.     point.x = x_;
  218.     point.y = y_;
  219.     radius = radius_;
  220.     mass = mass_;
  221.   }
  222.  
  223.   void setSpeed(PVector force) {
  224.     PVector f = force.copy();
  225.     acceleration.add(PVector.div( f, mass));
  226.     velocity.add(acceleration);
  227.     point.add(velocity);
  228.     gre = acceleration.copy();
  229.  
  230.     // If out of bounds
  231.     if (point.y - radius <= 0)
  232.     {
  233.       velocity.y *= -springness;
  234.       point.y = 0 + radius;
  235.     } else if (point.y + radius >= height)
  236.     {
  237.       velocity.y *= -springness;
  238.       point.y = height - radius ;
  239.     }
  240.     if (point.x - radius <= 0)
  241.     {
  242.       velocity.x *= -springness;
  243.       point.x = 0 + radius;
  244.     } else if (point.x + radius >= width)
  245.     {
  246.       velocity.x *= -springness;
  247.       point.x = width - radius;
  248.     }
  249.     acceleration.mult(0);
  250.   }
  251.  
  252.   void drawing() {
  253.  
  254.     fill((map(velocity.y, 8.05, 0.015, 60, 255)), (map(gre.y, - 0.015, 0.015, 50, 100)), (map(point.y, - 55.01, 0.015, 1, 255)));
  255.     stroke((map(point.x, 104, 0.015, 100, 255)), (map(gre.x, - 0.015, 0.015, 60, 110)), (map(velocity.x, 15, 0.015, 50, 255)));
  256.     circle(point.x, point.y, 2 * radius);
  257.  
  258.     strokeWeight(2);
  259.     stroke(255, 255, 255);
  260.     PVector dir = velocity.copy();
  261.     dir.normalize().mult(radius);
  262.     dir = PVector.add(dir, point);
  263.     line(point.x, point.y, dir.x, dir.y);
  264.   }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement