Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. ParticleSystem ps; //Create an object of ParticleSystem.
  2.  
  3. void setup() {
  4. frameRate(120);
  5. size(800,800);
  6. smooth(8);
  7. ps = new ParticleSystem();
  8. for (int i=0; i<200; i++) { //Add particles to the ParticleSystem object.
  9. ps.add();
  10. }
  11. }
  12.  
  13. void draw() {
  14. background(50);
  15. ps.run();
  16. }
  17.  
  18. class Particle {
  19.  
  20. PVector velocity, location;
  21.  
  22. Particle() { //construct particles with random velocity and location.
  23. velocity = new PVector(random(-0.4, 0.4), random(-0.4, 0.4));
  24. location = new PVector(random(0, width), random(0, height));
  25. }
  26.  
  27. void update() { //movement method
  28. location.add(velocity);
  29. }
  30.  
  31. void edge() { //wraparound case
  32. if (location.x > width) {
  33. velocity.x *= -1;
  34. } else if (location.x < 0) {
  35. velocity.x *= -1;
  36. }
  37.  
  38. if (location.y > height) {
  39. velocity.y *= -1;
  40. } else if (location.y < 0) {
  41. velocity.y *= -1;
  42. }
  43. }
  44.  
  45. void display(float alpha, float alphaX) { //display each particle as an ellipse
  46.  
  47. noStroke();
  48. fill(255, 25 + alpha);
  49. ellipse(location.x, location.y, 1.5+alphaX, 1.5+alphaX);
  50. }
  51.  
  52. float d, alpha, m, alpha2, alphaX;
  53. float closestDistance = 10000;
  54. Particle closestNeighbour = null;
  55.  
  56. void connect(ArrayList<Particle> particles) {
  57. for (Particle other : particles) {
  58. d = PVector.dist(location, other.location); //get distance between particles.
  59.  
  60. if (other==this) { //skip if its this particle.
  61. continue;
  62. }
  63.  
  64. if (d<closestDistance) { //set a to lowest distance between particles.
  65. closestDistance = d;
  66. closestNeighbour = other;
  67. }
  68.  
  69.  
  70. if (d<51) { //for particles lower than distance of 51, draw a line between them
  71. alpha = 255 - map(closestDistance, 0, 51, 0, 255);
  72.  
  73. stroke(255, alpha);
  74. strokeWeight(0.1+alpha*0.003);
  75. line(location.x, location.y, other.location.x, other.location.y);
  76. }
  77. }
  78.  
  79. alpha = 255 - map(closestDistance, 0, 75, 0, 255);
  80. alphaX = alpha*0.003;
  81.  
  82. if (closestDistance<112) { //change alpha according to proximity between neighboring particles.
  83. display(alpha, alphaX);
  84. closestDistance = 10000;
  85. }
  86. }
  87. }
  88.  
  89. class ParticleSystem{
  90. ArrayList<Particle> particles;
  91.  
  92. ParticleSystem(){
  93. particles= new ArrayList<Particle>();
  94. }
  95.  
  96. void add(){
  97. particles.add(new Particle());
  98. }
  99.  
  100. void run(){ //add new particles to arraylist
  101. for(Particle q : particles){ //for each particle in arraylist, update its location, check for borders and pass arraylist of other particles for detection.
  102. q.update();
  103. q.edge();
  104. q.connect(particles);
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement