Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int num = 50, fc;
- Mover[ ]movers = new Mover[num];
- Attractor a;
- float theta;
- PFont myFont;
- String watermark = "p5art.tumblr.com";
- Boolean save = false;
- void setup() {
- size(540, 540);
- background(0);
- noCursor();
- myFont = loadFont("PTSans-Caption-12.vlw");
- textFont(myFont);
- for (int i=0; i<movers.length; i++) {
- movers[i] = new Mover(random(0.5, 2), random(width), random(height));
- }
- a = new Attractor();
- }
- void draw() {
- background(#490A3D);
- fill(255, 100);
- text(watermark, width-textWidth(watermark)-5, height-5);
- a.update();
- a.display();
- for (int i=0; i<movers.length; i++) {
- for (int j=0; j<movers.length; j++) {
- if (i!= j) {
- //PVector force = movers[j].repel(movers[i]);
- PVector force = movers[j].attract(movers[i]);
- movers[i].applyForce(force);
- }
- }
- movers[i].update();
- movers[i].display();
- //movers[i].drawLines();
- movers[i].checkEdges();
- }
- for (int i=0; i<movers.length; i++) {
- //PVector force = a.attract(movers[i]);
- PVector force = a.repel(movers[i]);
- movers[i].applyForce(force);
- }
- if (save) {
- if (frameCount%1==0 && frameCount<fc+120)saveFrame("image-####.gif");
- if (frameCount==fc+120) noLoop();
- }
- }
- void keyPressed() {
- fc = frameCount;
- save = true;
- }
- class Attractor {
- float mass;
- PVector location;
- float G;
- Attractor() {
- location = new PVector(width/2, height/2);
- mass = 20;
- G = 40.0;
- }
- void update() {
- location = new PVector(mouseX, mouseY);
- }
- void display() {
- stroke(#ffffff);
- strokeWeight(7);
- fill(#BD1550);
- ellipse(location.x, location.y, mass*2, mass*2);
- }
- PVector attract(Mover m) {
- PVector force = PVector.sub(location, m.location);
- float distance = force.mag();
- distance = constrain(distance, 5.0, 15.0);
- float d = map(distance, 5.0, 15.0, .25, 1.0);
- //d = 1;
- force.normalize();
- float strength = (G*mass*m.mass)/(distance*distance)*d;
- force.mult(strength);
- return force;
- }
- PVector repel(Mover m) {
- PVector force = PVector.sub(location, m.location);
- float distance = force.mag();
- distance = constrain(distance, 5.0, 5000.0);
- force.normalize();
- float strength = (G * mass * m.mass) / (distance * distance)*-1;
- force.mult(strength);
- return force;
- }
- }
- class Attractor {
- float mass;
- PVector location;
- float G;
- Attractor() {
- location = new PVector(width/2, height/2);
- mass = 20;
- G = 40.0;
- }
- void update() {
- location = new PVector(mouseX, mouseY);
- }
- void display() {
- stroke(#ffffff);
- strokeWeight(7);
- fill(#BD1550);
- ellipse(location.x, location.y, mass*2, mass*2);
- }
- PVector attract(Mover m) {
- PVector force = PVector.sub(location, m.location);
- float distance = force.mag();
- distance = constrain(distance, 5.0, 15.0);
- float d = map(distance, 5.0, 15.0, .25, 1.0);
- //d = 1;
- force.normalize();
- float strength = (G*mass*m.mass)/(distance*distance)*d;
- force.mult(strength);
- return force;
- }
- PVector repel(Mover m) {
- PVector force = PVector.sub(location, m.location);
- float distance = force.mag();
- distance = constrain(distance, 5.0, 5000.0);
- force.normalize();
- float strength = (G * mass * m.mass) / (distance * distance)*-1;
- force.mult(strength);
- return force;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement