Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ParticleSystem ps; //Create an object of ParticleSystem.
- void setup() {
- frameRate(120);
- size(800,800);
- smooth(8);
- ps = new ParticleSystem();
- for (int i=0; i<200; i++) { //Add particles to the ParticleSystem object.
- ps.add();
- }
- }
- void draw() {
- background(50);
- ps.run();
- }
- class Particle {
- PVector velocity, location;
- Particle() { //construct particles with random velocity and location.
- velocity = new PVector(random(-0.4, 0.4), random(-0.4, 0.4));
- location = new PVector(random(0, width), random(0, height));
- }
- void update() { //movement method
- location.add(velocity);
- }
- void edge() { //wraparound case
- if (location.x > width) {
- velocity.x *= -1;
- } else if (location.x < 0) {
- velocity.x *= -1;
- }
- if (location.y > height) {
- velocity.y *= -1;
- } else if (location.y < 0) {
- velocity.y *= -1;
- }
- }
- void display(float alpha, float alphaX) { //display each particle as an ellipse
- noStroke();
- fill(255, 25 + alpha);
- ellipse(location.x, location.y, 1.5+alphaX, 1.5+alphaX);
- }
- float d, alpha, m, alpha2, alphaX;
- float closestDistance = 10000;
- Particle closestNeighbour = null;
- void connect(ArrayList<Particle> particles) {
- for (Particle other : particles) {
- d = PVector.dist(location, other.location); //get distance between particles.
- if (other==this) { //skip if its this particle.
- continue;
- }
- if (d<closestDistance) { //set a to lowest distance between particles.
- closestDistance = d;
- closestNeighbour = other;
- }
- if (d<51) { //for particles lower than distance of 51, draw a line between them
- alpha = 255 - map(closestDistance, 0, 51, 0, 255);
- stroke(255, alpha);
- strokeWeight(0.1+alpha*0.003);
- line(location.x, location.y, other.location.x, other.location.y);
- }
- }
- alpha = 255 - map(closestDistance, 0, 75, 0, 255);
- alphaX = alpha*0.003;
- if (closestDistance<112) { //change alpha according to proximity between neighboring particles.
- display(alpha, alphaX);
- closestDistance = 10000;
- }
- }
- }
- class ParticleSystem{
- ArrayList<Particle> particles;
- ParticleSystem(){
- particles= new ArrayList<Particle>();
- }
- void add(){
- particles.add(new Particle());
- }
- void run(){ //add new particles to arraylist
- for(Particle q : particles){ //for each particle in arraylist, update its location, check for borders and pass arraylist of other particles for detection.
- q.update();
- q.edge();
- q.connect(particles);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement