Advertisement
xeromino

repelAttract

Jul 12th, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. int num = 50, fc;
  2. Mover[ ]movers = new Mover[num];
  3. Attractor a;
  4. float theta;
  5. PFont myFont;
  6. String watermark = "p5art.tumblr.com";
  7. Boolean save = false;
  8.  
  9. void setup() {
  10.   size(540, 540);
  11.   background(0);
  12.   noCursor();
  13.   myFont = loadFont("PTSans-Caption-12.vlw");
  14.   textFont(myFont);
  15.   for (int i=0; i<movers.length; i++) {
  16.     movers[i] = new Mover(random(0.5, 2), random(width), random(height));
  17.   }
  18.   a = new Attractor();
  19. }
  20.  
  21. void draw() {
  22.   background(#490A3D);
  23.   fill(255, 100);
  24.   text(watermark, width-textWidth(watermark)-5, height-5);
  25.   a.update();
  26.   a.display();
  27.  
  28.   for (int i=0; i<movers.length; i++) {
  29.     for (int j=0; j<movers.length; j++) {
  30.       if (i!= j) {
  31.         //PVector force = movers[j].repel(movers[i]);
  32.         PVector force = movers[j].attract(movers[i]);
  33.         movers[i].applyForce(force);
  34.       }
  35.     }
  36.     movers[i].update();
  37.     movers[i].display();
  38.     //movers[i].drawLines();
  39.     movers[i].checkEdges();
  40.   }
  41.  
  42.   for (int i=0; i<movers.length; i++) {
  43.     //PVector force = a.attract(movers[i]);
  44.     PVector force = a.repel(movers[i]);
  45.     movers[i].applyForce(force);
  46.   }
  47.  
  48.   if (save) {
  49.     if (frameCount%1==0 && frameCount<fc+120)saveFrame("image-####.gif");
  50.     if (frameCount==fc+120) noLoop();
  51.   }
  52. }
  53.  
  54. void keyPressed() {
  55.   fc = frameCount;
  56.   save = true;
  57. }
  58.  
  59. class Attractor {
  60.  
  61.   float mass;
  62.   PVector location;
  63.   float G;
  64.  
  65.   Attractor() {
  66.     location = new PVector(width/2, height/2);
  67.     mass = 20;
  68.     G = 40.0;
  69.   }
  70.  
  71.   void update() {
  72.     location = new PVector(mouseX, mouseY);
  73.   }
  74.  
  75.   void display() {
  76.     stroke(#ffffff);
  77.     strokeWeight(7);
  78.     fill(#BD1550);
  79.     ellipse(location.x, location.y, mass*2, mass*2);
  80.   }
  81.  
  82.   PVector attract(Mover m) {
  83.     PVector force = PVector.sub(location, m.location);
  84.     float distance = force.mag();
  85.     distance = constrain(distance, 5.0, 15.0);
  86.     float d = map(distance, 5.0, 15.0, .25, 1.0);
  87.     //d = 1;
  88.     force.normalize();
  89.     float strength = (G*mass*m.mass)/(distance*distance)*d;
  90.     force.mult(strength);
  91.  
  92.     return force;
  93.   }
  94.  
  95.   PVector repel(Mover m) {
  96.  
  97.     PVector force = PVector.sub(location, m.location);
  98.     float distance = force.mag();
  99.     distance = constrain(distance, 5.0, 5000.0);
  100.     force.normalize();
  101.  
  102.     float strength = (G * mass * m.mass) / (distance * distance)*-1;
  103.     force.mult(strength);
  104.     return force;
  105.   }
  106. }
  107.  
  108. class Attractor {
  109.  
  110.   float mass;
  111.   PVector location;
  112.   float G;
  113.  
  114.   Attractor() {
  115.     location = new PVector(width/2, height/2);
  116.     mass = 20;
  117.     G = 40.0;
  118.   }
  119.  
  120.   void update() {
  121.     location = new PVector(mouseX, mouseY);
  122.   }
  123.  
  124.   void display() {
  125.     stroke(#ffffff);
  126.     strokeWeight(7);
  127.     fill(#BD1550);
  128.     ellipse(location.x, location.y, mass*2, mass*2);
  129.   }
  130.  
  131.   PVector attract(Mover m) {
  132.     PVector force = PVector.sub(location, m.location);
  133.     float distance = force.mag();
  134.     distance = constrain(distance, 5.0, 15.0);
  135.     float d = map(distance, 5.0, 15.0, .25, 1.0);
  136.     //d = 1;
  137.     force.normalize();
  138.     float strength = (G*mass*m.mass)/(distance*distance)*d;
  139.     force.mult(strength);
  140.  
  141.     return force;
  142.   }
  143.  
  144.   PVector repel(Mover m) {
  145.  
  146.     PVector force = PVector.sub(location, m.location);
  147.     float distance = force.mag();
  148.     distance = constrain(distance, 5.0, 5000.0);
  149.     force.normalize();
  150.  
  151.     float strength = (G * mass * m.mass) / (distance * distance)*-1;
  152.     force.mult(strength);
  153.     return force;
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement