Problem with drawing buffered points
in
Programming Questions
•
1 year ago
Been trying to solve this one for a couple of hours, but no luck so far.
The program below should draw a line constructed from a number of 'Movers' (really basic physics-y particles), storing the most recent lines in a buffer then drawing them. The end effect should be a trail of lines that bounces around the screen and creates pretty based patterns :3
Unfortunately all is not as it seems - instead of drawing a trail, all the lines are drawn at the most recent location. I've tried using println statements to check that the buffer values are ok, and nothing seems to be wrong there, so I think it must be a problem with the way that I'm drawing the lines. But I've used this drawing method before and it's worked fine.
Any ideas why it's not working? Thanks in advance!
- int resolution = 32; //Number of vertices per line
- int trails = 128; //Number of lines to store in buffer
- float patternWidth; //Width of initial line
- Mover[] linePoint = new Mover[resolution]; //Particles in line
- PVector[][] storage = new PVector[trails][resolution]; //Keep old locations in here
- void setup() {
- size(500, 500);
- patternWidth = width - 20;
- for (int i = 0; i < resolution; i++) {
- linePoint[i] = new Mover(new PVector((i * (patternWidth / (resolution - 1))) + ((width - patternWidth) / 2), 0));
- for (int j = 0; j < trails; j++) {
- storage[j][i] = new PVector(-10, -10);
- }
- }
- stroke(0, 127);
- noFill();
- }
- void draw() {
- background(255);
- for (int i = 0; i < resolution; i++) {
- linePoint[i].update(new PVector(random(-0.001, 0.001), random(-0.001, 0.001)));
- storage[frameCount % trails][i] = linePoint[i].location;
- println(frameCount % trails + " " + i + " " + storage[frameCount % trails][i]);
- }
- for (int i = 0; i < trails; i++) {
- beginShape();
- for (int j = 0; j < resolution; j++) {
- vertex(storage[i][j].x, storage[i][j].y);
- }
- endShape();
- }
- }
- class Mover {
- PVector location;
- PVector velocity;
- PVector gravity;
- float maxVel;
- Mover(PVector loc) {
- location = loc;
- velocity = new PVector(0, 0);
- gravity = new PVector(0, 0.01);
- maxVel = 5;
- }
- void update(PVector next) {
- velocity.add(next);
- velocity.add(gravity);
- velocity.mult(0.9995);
- velocity.limit(maxVel); //comment out for funsies :D
- location.add(velocity);
- if (location.x <= 0) {
- velocity.x = -velocity.x;
- location.x = 0;
- }
- if (width <= location.x) {
- velocity.x = -velocity.x;
- location.x = width;
- }
- if (location.y <= 0) {
- velocity.y = -velocity.y;
- location.y = 0;
- }
- if (height <= location.y) {
- velocity.y = -velocity.y;
- location.y = height;
- }
- }
- }
1