Array List, recasting objects?
in
Programming Questions
•
2 years ago
Hi..
The problem is that when I call up the particles from the arrayList (as opposed to the regular multidimensional array I had used previously, they seem to have lost the values they were instantiated with, and odd things happen with their local geometry.
Not too sure of my terms here as I'm a relative newbie, but I've just discovered I need to use an ArrayList instead of an array in order to add and remove elements at runtime, so I've sussed out how to do that:
- int pSystems = 4;
- int pDensity = 100;
- ArrayList particleSystems = new ArrayList();
- boolean saving = false;
- void setup() {
- size(1024, 768);
- smooth();
- frameRate(1);
- for (int i = 0; i < pSystems; i++) {
- ArrayList pSystem = new ArrayList();
- for (int j = 0; j < pDensity; j++) {
- pSystem.add(new Particle(new PVector(100*i, 100*i)));
- }
- particleSystems.add(pSystem);
- }
- }
- void draw() {
- background(255);
- // draw the particles
- for (int i = particleSystems.size()-1; i >= 0; i--) {
- ArrayList pSystem = (ArrayList) particleSystems.get(i);
- for (int j = pSystem.size()-1; j>=0; j--) {
- Particle particle = (Particle) pSystem.get(i);
- particle.update();
- particle.draw();
- }
- }
- }
Could anyone help me understand why this would be, and what the best way to get around it is? I'll copy in below my previous example, and the particle class (from the form & code book). Thanks
- int pSystems = 4;
- int pDensity = 100;
- Particle[][] particles = new Particle[pSystems][pDensity];
- boolean saving = false;
- void setup() {
- size(1024, 768);
- smooth();
- for (int i = 0; i < pSystems; i++) {
- for (int j = 0; j < pDensity; j++) {
- particles[i][j] = new Particle(new PVector(100*i, 100*i));
- }
- }
- }
- void draw() {
- background(255);
- // draw the particles
- for (int i = 0; i < pSystems; i++) {
- for (int j = 0; j < pDensity; j++) {
- particles[i][j].update();
- particles[i][j].draw();
- print(particles[i][j].loc);
- }
- }
- }
- /**
- * Simulate: Particles
- * from Form+Code in Design, Art, and Architecture
- * by Casey Reas, Chandler McWilliams, and LUST
- * Princeton Architectural Press, 2010
- * ISBN 9781568989372
- *
- * This code was written for Processing 1.2+
- * Get Processing at http://www.processing.org/download
- */
- // ---------------
- // Particle.pde
- // Based on the Particle class from: http://www.shiffman.net/teaching/nature/particles/
- // ---------------
- class Particle {
- PVector loc;
- PVector vel;
- PVector acc;
- PVector[] hist;
- int counter = 0;
- // create the particle
- Particle(PVector l) {
- float randmin = -HALF_PI;
- float randmax = 0;
- float r = random(0, TWO_PI);
- float x = cos(r);
- float y = sin(r);
- acc = new PVector(x / 250, y / 250);
- float q = random(0, 1);
- r = random(randmin, randmax);
- x = cos(r) * q;
- y = sin(r) * q;
- vel = new PVector(x, y);
- loc = l.get();
- hist = new PVector[1000];
- }
- // update location
- void update() {
- vel.add(acc);
- loc.add(vel);
- // save location every 10 frames
- if (frameCount % 10 == 0 && counter < hist.length) {
- hist[counter] = loc.get();
- counter++;
- }
- }
- // draw particle
- void draw() {
- fill(100,50);
- drawArrowHead(vel,loc,10);
- noFill();
- // draw history path
- stroke(0, 100);
- beginShape();
- for (int i=0; i<counter; i++) {
- vertex(hist[i].x,hist[i].y);
- }
- if (counter > 0) vertex(loc.x, loc.y);
- endShape();
- }
- void drawArrowHead(PVector v, PVector loc, float scale) {
- pushMatrix();
- float arrowsize = 4;
- // Translate to location to render vector
- translate(loc.x, loc.y);
- // rotate to heading
- rotate(v.heading2D());
- // Calculate length of vector & scale it to be bigger or smaller if necessary
- float len = v.mag()*scale;
- arrowsize = map(len, 0, 10, 0, 1) * arrowsize;
- // Draw point
- stroke(0, 100);
- fill(0, 100);
- line(0,0,len-arrowsize,0);
- noStroke();
- beginShape();
- vertex(len,0);
- vertex(len-arrowsize,+arrowsize/2);
- vertex(len-arrowsize,-arrowsize/2);
- endShape(CLOSE);
- popMatrix();
- }
- }
1