Advertisement
Rafpast

SoftBody1

Apr 21st, 2021
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.25 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. boolean stopStart = true;
  4. int polygonVerticies = 3;
  5. float delta_time, now = System.nanoTime();
  6. ArrayList<SoftBody> body  = new ArrayList<SoftBody>();
  7.  
  8. void setup() {
  9.   size(1920, 1020, P2D);
  10.   background(0);
  11.  
  12.  ArrayList<PVector> verticies = new ArrayList<PVector>();
  13.   softBodyCreation(verticies);
  14. }
  15.  
  16. void softBodyCreation(ArrayList<PVector> verticies_)
  17. {
  18.   for (int i = 0; i < polygonVerticies; i++)
  19.   {
  20.     verticies_.add(new PVector(width * new Random().nextFloat(), height * new Random().nextFloat()));
  21.   }
  22.  
  23.   body.add(new SoftBody(verticies_, 20));
  24. }
  25.  
  26. void keyPressed()
  27. {
  28.   if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
  29.   {
  30.     if (key == 'R' ||key == 'r')
  31.     {//reset
  32.       ArrayList<PVector> verticie = new ArrayList<PVector>();
  33.       float bodySize = body.size();
  34.       background(0);
  35.       body.clear();
  36.  
  37.       for (int i = 0; i < bodySize; i++)
  38.       {
  39.         softBodyCreation(verticie);
  40.       }
  41.     } else if (key == 'D' ||key == 'd')
  42.     {//adding one figure
  43.       ArrayList<PVector> verticie = new ArrayList<PVector>();
  44.  
  45.       softBodyCreation(verticie);
  46.     } else if (key == 'S' ||key == 's')
  47.     {
  48.       stopStart = !stopStart;
  49.     }
  50.   }
  51. }
  52.  
  53. void calculate_delta_time()
  54. {
  55.   delta_time = (System.nanoTime()- now)/100000000;
  56.   now = System.nanoTime();
  57. }
  58.  
  59. void draw() {
  60.   fill(0, 100);
  61.   background(0);
  62.  
  63.   for (int i = 0; i < body.size(); i++)
  64.   {
  65.     body.get(i).show(stopStart);
  66.   }
  67. }
  68.  
  69. class SoftBody {
  70.  
  71.   ArrayList<Spring> sidesAndDiagonals = new ArrayList<Spring>();
  72.   float verticiesRadius = 2, mass, springRestLenght = 200;
  73.  
  74.   SoftBody()
  75.   {
  76.   }
  77.  
  78.   SoftBody(ArrayList<PVector> verticies, float mass_)
  79.   {
  80.  
  81.     for (int i = 0; i < verticies.size() - 1; i++)
  82.     {
  83.       sidesAndDiagonals.add(new Spring(new Circum(verticies.get(i).x, verticies.get(i).y, verticiesRadius, mass_/verticies.size()), new Circum(verticies.get(i + 1).x, verticies.get(i + 1).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
  84.     }
  85.  
  86.     sidesAndDiagonals.add(new Spring(new Circum(verticies.get(verticies.size() - 1).x, verticies.get(verticies.size() - 1).y, verticiesRadius, mass_/verticies.size()),
  87.       new Circum(verticies.get(0).x, verticies.get(0).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
  88.  
  89.     mass = mass_;
  90.   }
  91.  
  92.   void reset(ArrayList<PVector> verticies, float mass_)
  93.   {
  94.     sidesAndDiagonals.clear();
  95.  
  96.     for (int i = 0; i < verticies.size() - 1; i++)
  97.     {
  98.       sidesAndDiagonals.add(new Spring(new Circum(verticies.get(i).x, verticies.get(i).y, verticiesRadius, mass_/verticies.size()), new Circum(verticies.get(i + 1).x, verticies.get(i + 1).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
  99.     }
  100.  
  101.     sidesAndDiagonals.add(new Spring(new Circum(verticies.get(verticies.size() - 1).x, verticies.get(verticies.size() - 1).y, verticiesRadius, mass_/verticies.size()),
  102.       new Circum(verticies.get(0).x, verticies.get(0).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
  103.  
  104.     mass = mass_;
  105.   }
  106.  
  107.   void addPoint(PVector point)
  108.   {    
  109.     if (sidesAndDiagonals.size() > 0)
  110.     {
  111.       sidesAndDiagonals.add(new Spring(new Circum(sidesAndDiagonals.get(sidesAndDiagonals.size() - 1).massPoint1.point.x, sidesAndDiagonals.get(sidesAndDiagonals.size() - 1).massPoint1.point.y, verticiesRadius, mass/sidesAndDiagonals.size()),
  112.         new Circum(point.x, point.y, verticiesRadius, mass/sidesAndDiagonals.size()), springRestLenght));
  113.     } else
  114.     {
  115.       sidesAndDiagonals.add(new Spring(new Circum(point.x, point.y, verticiesRadius, mass/sidesAndDiagonals.size()), new Circum(point.x, point.y, verticiesRadius, mass/sidesAndDiagonals.size()), springRestLenght));
  116.     }
  117.   }
  118.  
  119.   void show(boolean stopStart)
  120.   {
  121.     for (int i = 0; i < sidesAndDiagonals.size(); i++)
  122.     {
  123.       if (stopStart)
  124.       {
  125.         sidesAndDiagonals.get(i).update();
  126.       }
  127.       sidesAndDiagonals.get(i).show();
  128.     }
  129.   }
  130. }
  131.  
  132. class Spring {
  133.  
  134.   Circum massPoint0, massPoint1;
  135.   float restLenght = 100, stifness = 1, dampingFactor = 0.990;
  136.  
  137.   Spring(Circum point0, Circum point1, float restLenght_)
  138.   {
  139.     massPoint0 = new Circum(point0.point.x, point0.point.y, point0.radius, point0.mass);
  140.     massPoint1 = new Circum(point1.point.x, point1.point.y, point1.radius, point1.mass);
  141.  
  142.     restLenght = restLenght_;
  143.   }
  144.  
  145.   void update()
  146.   {
  147.     PVector lengthFromZeroToOne = PVector.sub(massPoint1.point, massPoint0.point).normalize();
  148.     PVector lengthFromOneToZero = PVector.sub(massPoint0.point, massPoint1.point).normalize();
  149.     PVector velocityDiffrence = PVector.sub(massPoint1.velocity, massPoint0.velocity);
  150.  
  151.     float forceBetwenMasses = PVector.dot(lengthFromZeroToOne, velocityDiffrence) * stifness;  
  152.     float springForce = stifness * (PVector.dist(massPoint1.point, massPoint0.point) - restLenght);
  153.  
  154.     float totalForce = springForce + forceBetwenMasses;
  155.  
  156.     massPoint0.setSpeed(lengthFromZeroToOne.setMag(totalForce));
  157.     massPoint1.setSpeed(lengthFromOneToZero.setMag(totalForce));
  158.   }
  159.  
  160.   void show()
  161.   {
  162.     massPoint0.drawing();
  163.     line(massPoint0.point.x, massPoint0.point.y, massPoint1.point.x, massPoint1.point.y);
  164.     massPoint1.drawing();
  165.   }
  166. }
  167.  
  168. class Circum {
  169.   int max_velocity = 15;    
  170.   PVector point = new PVector(0, 0);
  171.   PVector accelToDraw = new PVector(0, 0);  
  172.   PVector acceleration = new PVector(0, 0);
  173.   float radius, mass, springness = 0.9999;
  174.   PVector velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));  
  175.  
  176.   Circum()
  177.   {
  178.     mass = 5 * new Random().nextFloat() + 1;
  179.     radius = mass * 4;
  180.     point.x = (width * new Random().nextFloat()) - radius;
  181.     point.y =  (height * new Random().nextFloat()) - radius;
  182.   }
  183.  
  184.   Circum(float x_, float y_, float radius_, float mass_)
  185.   {
  186.     point.x = x_;
  187.     point.y = y_;
  188.     mass = mass_;
  189.     radius = radius_;
  190.   }
  191.  
  192.   Circum(PVector pos_, PVector vel_, PVector acc_, float radius_, float mass_, float springiness_)
  193.   {
  194.     mass = mass_;
  195.     point = pos_;
  196.     velocity = vel_;
  197.     radius = radius_;    
  198.     acceleration = acc_;
  199.     springness = springiness_;
  200.   }
  201.  
  202.   void setSpeed(PVector force)
  203.   {
  204.     PVector f = force.copy();
  205.     acceleration.add(PVector.div( f, mass));
  206.     acceleration.mult(delta_time);
  207.     velocity.add(acceleration);
  208.     point.add(velocity);
  209.     accelToDraw = acceleration.copy();
  210.  
  211.     // If out of bounds
  212.     if (point.y - radius <= 0)
  213.     {
  214.       velocity.y *= -springness;
  215.       point.y = 0 + radius;
  216.     } else if (point.y + radius >= height)
  217.     {
  218.       velocity.y *= -springness;
  219.       point.y = height - radius ;
  220.     }
  221.    
  222.     if (point.x - radius <= 0)
  223.     {
  224.       velocity.x *= -springness;
  225.       point.x = 0 + radius;
  226.     } else if (point.x + radius >= width)
  227.     {
  228.       velocity.x *= -springness;
  229.       point.x = width - radius;
  230.     }
  231.    
  232.     acceleration.mult(0);
  233.   }
  234.  
  235.   void drawing()
  236.   {
  237.     fill(200, 10, 100);
  238.     stroke(100, 10, 200);
  239.     circle(point.x, point.y, 2 * radius);
  240.  
  241.     strokeWeight(2);
  242.     stroke(255, 255, 255);
  243.     PVector dir = velocity.copy();
  244.     dir.normalize().mult(radius);
  245.     dir = PVector.add(dir, point);
  246.     line(point.x, point.y, dir.x, dir.y);
  247.   }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement