Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var flock, cD, cN;
- function setup() {
- createCanvas(640, 360);
- flock = new Flock();
- for (var i = 0; i < 100; i++) {
- flock.add();
- }
- }
- function draw() {
- background(51);
- flock.run();
- }
- function Flock() {
- this.particles = [];
- }
- Flock.prototype.add = function () {
- this.particles.push();
- }
- Flock.prototype.run = function () {
- for (var i = 0; i < this.Particle.length; i++) {
- this.particles[i].update();
- this.particles[i].edge();
- this.particles[i].connect(this.particles);
- }
- }
- function Particle() {
- this.velocity = createVector(random(-0.4, 0.4), random(-0.4, 0.4));
- this.location = createVector(random(0, width), random(0, height));
- }
- Particle.prototype.update() = function(){
- this.location.add(this.velocity);
- }
- Particle.prototype.edge() = function() {
- if (this.location.x > width) this.velocity.x *= -1;
- if (this.location.y > height) this.velocity.y *= -1;
- if (this.location.x < 0) this.velocity.x *= -1;
- if (this.location.y < 0) this.velocity.y *= -1;
- }
- Particle.prototype.dots() = function() {
- noStroke();
- fill(255, 25 + alpha);
- ellipse(this.location.x, this.location.y, 1.5 + alpha, 1.5 + alpha);
- }
- Particle.prototype.connect() = function(particles) {
- for (var i = 0; i < particles.length; i++) {
- var alpha = 255 - map(closestDistance, 0, 51, 0, 255);
- var d = p5.Vector.dist(this.location, particle[i].location);
- if (boids[i] == this) {
- continue;
- }
- if (d < cD) {
- cD = d;
- cN = particles[i];
- }
- if (d < 51) {
- stroke(255, alpha);
- strokeWeight(0.1 + alpha * 0.003);
- line(this.location.x, this.location.y, particle[i].location.x, particle[i].location.y);
- }
- }
- alphaDbl = alpha * 2;
- alphaX = alpha*0.003;
- if (closestDistance < 112) {
- this.display(alphaDbl, alphaX);
- closestDistance = 10000;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement