Advertisement
xeromino

videoGlitch

Mar 30th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.22 KB | None | 0 0
  1. // The Nature of Code
  2. // Daniel Shiffman
  3. // http://natureofcode.com
  4.  
  5. // Demonstration of Craig Reynolds' "Flocking" behavior
  6. // See: http://www.red3d.com/cwr/
  7. // Rules: Cohesion, Separation, Alignment
  8.  
  9. // Click mouse to add boids into the system
  10.  
  11. // video stuff added by Jerome Herr
  12.  
  13. import processing.video.*;
  14. import com.hamoid.*;
  15.  
  16. VideoExport videoExport;
  17. Movie movie;
  18.  
  19. Flock flock;
  20.  
  21. void setup() {
  22.   size(1280, 720);
  23.   background(238);
  24.   //frameRate(30);  
  25.   movie = new Movie(this, "beyonce.mp4");
  26.   movie.play();
  27.   flock = new Flock();
  28.   // Add an initial set of boids into the system
  29.   for (int i = 0; i < 350; i++) {
  30.     Boid b = new Boid(width/2, height/2);
  31.     flock.addBoid(b);
  32.   }
  33.     videoExport = new VideoExport(this, "internetVideo" + random(9999) + ".mp4");
  34.   //videoExport.setFrameRate(30);
  35.   videoExport.startMovie();
  36. }
  37.  
  38. void draw() {
  39.   //background(255);
  40.   flock.run();
  41.   videoExport.saveFrame();
  42.   // Instructions
  43.   //fill(0);
  44.   //text("Drag the mouse to generate new boids.", 10, height-16);
  45. }
  46.  
  47. // Add a new boid into the System
  48. void mouseDragged() {
  49.   //flock.addBoid(new Boid(mouseX, mouseY));
  50. }
  51.  
  52. void movieEvent(Movie movie) {  
  53.   movie.read();
  54. }
  55.  
  56. void mouseReleased() {
  57.   //save(random(99999)+".png");
  58. }
  59. void keyPressed() {
  60.   if (key == 'q') {
  61.     videoExport.endMovie();
  62.     //exit();
  63.   }
  64. }
  65.  
  66. // The Nature of Code
  67. // Daniel Shiffman
  68. // http://natureofcode.com
  69.  
  70. // Boid class
  71. // Methods for Separation, Cohesion, Alignment added
  72.  
  73. class Boid {
  74.  
  75.   PVector position;
  76.   PVector velocity;
  77.   PVector acceleration;
  78.   float r;
  79.   float maxforce;    // Maximum steering force
  80.   float maxspeed;    // Maximum speed
  81.   int sz;
  82.  
  83.   Boid(float x, float y) {
  84.     acceleration = new PVector(0, 0);
  85.     velocity = new PVector(random(-1, 1), random(-1, 1));
  86.     position = new PVector(x, y);
  87.     r = 3.0;
  88.     maxspeed = 3;
  89.     maxforce = 0.05;
  90.   }
  91.  
  92.   void run(ArrayList<Boid> boids) {
  93.     flock(boids);
  94.     update();
  95.     borders();
  96.     render();
  97.   }
  98.  
  99.   void applyForce(PVector force) {
  100.     // We could add mass here if we want A = F / M
  101.     acceleration.add(force);
  102.   }
  103.  
  104.   // We accumulate a new acceleration each time based on three rules
  105.   void flock(ArrayList<Boid> boids) {
  106.     PVector sep = separate(boids);   // Separation
  107.     PVector ali = align(boids);      // Alignment
  108.     PVector coh = cohesion(boids);   // Cohesion
  109.     // Arbitrarily weight these forces
  110.     sep.mult(1.5);
  111.     ali.mult(1.0);
  112.     coh.mult(1.0);
  113.     // Add the force vectors to acceleration
  114.     applyForce(sep);
  115.     applyForce(ali);
  116.     applyForce(coh);
  117.   }
  118.  
  119.   // Method to update position
  120.   void update() {
  121.     // Update velocity
  122.     velocity.add(acceleration);
  123.     // Limit speed
  124.     velocity.limit(maxspeed);
  125.     position.add(velocity);
  126.     // Reset accelertion to 0 each cycle
  127.     acceleration.mult(0);
  128.   }
  129.  
  130.   // A method that calculates and applies a steering force towards a target
  131.   // STEER = DESIRED MINUS VELOCITY
  132.   PVector seek(PVector target) {
  133.     PVector desired = PVector.sub(target, position);  // A vector pointing from the position to the target
  134.     // Normalize desired and scale to maximum speed
  135.     desired.normalize();
  136.     desired.mult(maxspeed);
  137.     // Steering = Desired minus Velocity
  138.     PVector steer = PVector.sub(desired, velocity);
  139.     steer.limit(maxforce);  // Limit to maximum steering force
  140.     return steer;
  141.   }
  142.  
  143.   void render() {
  144.     sz = (int) random(20, 50);
  145.     PImage tmp = movie.get(int(position.x), int(position.y), sz, sz);
  146.     tint(255, 100);
  147.     image(tmp, position.x, position.y);
  148.   }
  149.  
  150.   void render_old() {
  151.     // Draw a triangle rotated in the direction of velocity
  152.     float theta = velocity.heading2D() + radians(90);
  153.     fill(175);
  154.     stroke(0);
  155.     pushMatrix();
  156.     translate(position.x, position.y);
  157.     rotate(theta);
  158.     beginShape(TRIANGLES);
  159.     vertex(0, -r*2);
  160.     vertex(-r, r*2);
  161.     vertex(r, r*2);
  162.     endShape();
  163.     popMatrix();
  164.   }
  165.  
  166.   // Wraparound
  167.   void borders() {
  168.     if (position.x < -r) position.x = width+r;
  169.     if (position.y < -r) position.y = height+r;
  170.     if (position.x > width+r) position.x = -r;
  171.     if (position.y > height+r) position.y = -r;
  172.   }
  173.  
  174.   // Separation
  175.   // Method checks for nearby boids and steers away
  176.   PVector separate (ArrayList<Boid> boids) {
  177.     float desiredseparation = 25.0f;
  178.     PVector steer = new PVector(0, 0, 0);
  179.     int count = 0;
  180.     // For every boid in the system, check if it's too close
  181.     for (Boid other : boids) {
  182.       float d = PVector.dist(position, other.position);
  183.       // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
  184.       if ((d > 0) && (d < desiredseparation)) {
  185.         // Calculate vector pointing away from neighbor
  186.         PVector diff = PVector.sub(position, other.position);
  187.         diff.normalize();
  188.         diff.div(d);        // Weight by distance
  189.         steer.add(diff);
  190.         count++;            // Keep track of how many
  191.       }
  192.     }
  193.     // Average -- divide by how many
  194.     if (count > 0) {
  195.       steer.div((float)count);
  196.     }
  197.  
  198.     // As long as the vector is greater than 0
  199.     if (steer.mag() > 0) {
  200.       // Implement Reynolds: Steering = Desired - Velocity
  201.       steer.normalize();
  202.       steer.mult(maxspeed);
  203.       steer.sub(velocity);
  204.       steer.limit(maxforce);
  205.     }
  206.     return steer;
  207.   }
  208.  
  209.   // Alignment
  210.   // For every nearby boid in the system, calculate the average velocity
  211.   PVector align (ArrayList<Boid> boids) {
  212.     float neighbordist = 50;
  213.     PVector sum = new PVector(0, 0);
  214.     int count = 0;
  215.     for (Boid other : boids) {
  216.       float d = PVector.dist(position, other.position);
  217.       if ((d > 0) && (d < neighbordist)) {
  218.         sum.add(other.velocity);
  219.         count++;
  220.       }
  221.     }
  222.     if (count > 0) {
  223.       sum.div((float)count);
  224.       sum.normalize();
  225.       sum.mult(maxspeed);
  226.       PVector steer = PVector.sub(sum, velocity);
  227.       steer.limit(maxforce);
  228.       return steer;
  229.     } else {
  230.       return new PVector(0, 0);
  231.     }
  232.   }
  233.  
  234.   // Cohesion
  235.   // For the average position (i.e. center) of all nearby boids, calculate steering vector towards that position
  236.   PVector cohesion (ArrayList<Boid> boids) {
  237.     float neighbordist = 50;
  238.     PVector sum = new PVector(0, 0);   // Start with empty vector to accumulate all positions
  239.     int count = 0;
  240.     for (Boid other : boids) {
  241.       float d = PVector.dist(position, other.position);
  242.       if ((d > 0) && (d < neighbordist)) {
  243.         sum.add(other.position); // Add position
  244.         count++;
  245.       }
  246.     }
  247.     if (count > 0) {
  248.       sum.div(count);
  249.       return seek(sum);  // Steer towards the position
  250.     } else {
  251.       return new PVector(0, 0);
  252.     }
  253.   }
  254. }
  255.  
  256. // The Nature of Code
  257. // Daniel Shiffman
  258. // http://natureofcode.com
  259.  
  260. // Flock class
  261. // Does very little, simply manages the ArrayList of all the boids
  262.  
  263. class Flock {
  264.   ArrayList<Boid> boids; // An ArrayList for all the boids
  265.  
  266.   Flock() {
  267.     boids = new ArrayList<Boid>(); // Initialize the ArrayList
  268.   }
  269.  
  270.   void run() {
  271.     for (Boid b : boids) {
  272.       b.run(boids);  // Passing the entire list of boids to each boid individually
  273.     }
  274.   }
  275.  
  276.   void addBoid(Boid b) {
  277.     boids.add(b);
  278.   }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement