Source Code. Projects. Nerd Stuff. Art Stuff.

LHC


Posted on September 25th, by amoeboar in classes, icm. No Comments

This assignment is inspired by Large Hadron Collider (see the second image here).

We had two weeks to work on a project making use of arrays. Taking it a step farther, I wanted to make an array of objects. To do this, I had to write my first class, and then create an array of particle objects to iterate through my array loops. I also had to learn how to use PVectors, which proved to be really useful in generating complex math for these objects. I’m really interested in learning more about particle physics, so for future assignments, I’d like to add more to this assignment: forces like gravity, friction, deceleration, etc.

I’m still having some trouble getting Processing’s Javascript mode to work properly (at the time of writing this, it’s still in Beta), so it’s difficult for me to embed it here. Also, the sketch is too computationally intensive to create a movie from it, which you can do by putting

saveFrame("frames/yourSketchName####.png");

at the end of your draw() function (more on that here) and then using Quicktime to create an image sequence. Also, you can use Processing’s built in Movie Maker (under ‘Tools’) to similar effect, although I don’t have too much experience with this. Nevertheless, you can find my source code below so that you can copy and paste into Processing and run it yourself. You can read the comments within the sketch for how to get different effects on the particles.

[gn_spoiler title="Here is the source code:" open="0" style="1"]
[gn_tabs style="3"]
[gn_tab title="xploding"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/********************************
/ John Capogna
/ xploding.pde
/ ITP, 2012
********************************/

int particleCount = 8000;
float startX;      //  Starting positions for X and Y
float startY;

// create an array of objects
Particle[] particles = new Particle[particleCount+1];

void setup() {
  size(800, 600, P3D);

  // initialize all elements of the array
  for (int i = 0; i < particles.length; i++) {
    particles[i] = new Particle();
  }
}

void draw() {
  background(0);
  translate(startX, startY);
  for (int i = 0; i < particles.length; i++) {
    particles[i].update();
    particles[i].display();
  }
}

void mousePressed() {
  reset();              //  Reset particles on every click
}

void reset() {
  startX = mouseX;      //  Starting positions are at mouseX and mouseY
  startY = mouseY;      //
  for (int i = 0; i < particles.length; i++) {
    //  Create new particle array, replacing old one
    particles[i] = new Particle();
    particles[i].launch();
  }
}
[/gn_tab] [gn_tab title="Particle"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Particle {

  PVector location;
  PVector velocity;
  PVector acceleration;
  float colorR;  //  Don't need these unless colors are constant
  float colorG;  //  (see display() function)
  float colorB;  //

  Particle() {
    location = new PVector();
    velocity = new PVector();
    acceleration = new PVector();
    colorR = random(255);  //  Don't need these unless colors are constant
    colorG = random(255);  //  (see display() function)
    colorB = random(255);  //
  }

  void launch() {
    velocity = PVector.random3D();      // Create velocity with random value, for 3D
    acceleration = PVector.random3D();  // Create acceleration
  }

  void update() {
    velocity.add(acceleration);  // Velocity changes according to acceleration
    location.add(velocity);      // Location changes by velocity
    acceleration.mult(0);        // Clear acceleration each frame to prevent accumulation of acceleration
//    velocity.div(1.007);       // Uncomment this line to prevent particles from leaving screen
  }

  void display() {
    pushMatrix();
    strokeWeight(random(2));                        // Randomize size of points for "twinkling" effect
//    stroke(colorR, colorG, colorB);               // Uncomment this line and comment next line for constant colors
    stroke(random(255), random(255), random(255));  // Changing color values, randomized
    translate(location.x,location.y,location.z);
//    line(location.x,location.y,location.x + (location.x * 1.001),  // Creates a "line burst" effect
//          location.y + (location.y * 1.001));                      //
    point(0,0);
    popMatrix();
  }
}
[/gn_tab] [/gn_tabs] [/gn_spoiler]




Leave a Reply