Help: Record & playback positions of all particles in a system
in
Programming Questions
•
1 year ago
I am currently trying to record and playback the positions of all particles in a particle system. I understand the concept though I am having difficulty with the implementation. I can record into a single array though I have difficulty when approaching multiple arrays.
To help illustrate my desired goal, I have created a working version with only one array:
- // Record_Mouse Example
- ArrayList<PVector> list = new ArrayList<PVector>(); // for storing the vector of our mouse location
- int frames; // count frames for our interpolation
- boolean begin = false; // our introduction screen
- boolean recording;
- void setup() {
- size(900, 600);
- smooth();
- }
- void draw() {
- background(100);
- frames++;
- // Introduction ----------------------
- if (!begin){
- noStroke();
- fill(255);
- text("click the mouse to create a loop", 20, 20, 200, 200);
- fill(200);
- ellipse(mouseX,mouseY,60,60);
- }
- // Recording ----------------------
- if (recording && begin) {
- fill(255, 0, 0);
- ellipse(mouseX,mouseY,60,60);
- list.add(new PVector(mouseX, mouseY)); // add the mouse vector to the array
- }
- // Playback ----------------------
- else if (begin) {
- PVector rec = list.get(frames%list.size());
- fill(0, 255, 0);
- ellipse(rec.x,rec.y,60,60);
- }
- }
- void mousePressed() {
- list.clear(); // clear the array
- recording = true; // turn on recording
- begin = true; // move past the introduction screen
- }
- void mouseReleased() {
- recording = false; // turn off recording
- }
The example I am working with is located here:
- Particle[] p = new Particle[4];
- boolean recording;
- boolean begin = false;
- void setup() {
- size(900, 600);
- smooth();
- noStroke();
- for (int i=0; i< p.length; i++) {
- p[i] = new Particle();
- p[i].myIndex = i;
- }
- }
- void draw() {
- background(100);
- fill(255);
- text("click the mouse to create a loop", 20, 20, 200, 200);
- if (begin) {
- if (recording) {
- for (Particle pa : p) {
- pa.bounceIfColliding();
- pa.display();
- pa.move();
- // -------------------------------------- Recording Help Needed Here
- // pa.record();
- }
- fill(255, 0, 0);
- text("RECORDING", width-100, 25, 150, 100);
- }
- else if (!recording && begin) {
- for (Particle pa : p) {
- pa.display();
- // -------------------------------------- Playback Help Needed Here
- // step through array list
- // pa.playback(x,y);
- }
- fill(0, 255, 0);
- text("PLAYING", width-100, 25, 150, 100);
- }
- }
- if (!begin) {
- for (Particle pa : p) {
- pa.bounceIfColliding();
- pa.display();
- pa.move();
- }
- }
- }
- void mousePressed() {
- begin = true;
- recording = true;
- for (Particle pa : p) {
- float distance = dist(mouseX, mouseY, pa.x, pa.y);
- if (distance < pa.r) {
- pa.grabbed = true;
- }
- }
- }
- void mouseReleased() {
- recording = false;
- for (Particle pa : p) {
- pa.grabbed = false;
- }
- }
Particle Class:
- class Particle {
- float x, y; // position
- float vx, vy; // speed
- float r; // size
- color myColor;
- int myIndex;
- boolean grabbed;
- Particle() {
- r = random(50, 70);
- setRandomLocation();
- while ( colliding () ) setRandomLocation();
- while (abs (vx) < 0.1) vx = random(-2, 2);
- while (abs (vy) < 0.1) vy = random(-2, 2);
- myColor = color(200);
- myIndex = -1;
- }
- void setRandomLocation() {
- x = random(r, width-r); // create a margin of r for x and y
- y = random(r, height-r); // to be sure we're fully on screen
- }
- boolean colliding() {
- for (int i=0; i< p.length; i++) { // check all circles
- if (i != myIndex) { // but don't count yourself
- if (p[i] == null) continue;
- float distance = dist(x, y, p[i].x, p[i].y);
- float radiiSum = r + p[i].r;
- if (distance < radiiSum) {
- return true;
- }
- }
- }
- return false;
- }
- void bounceIfColliding() {
- if (grabbed) return;
- for (int i=0; i< p.length; i++) { // check all circles
- if (i != myIndex) { // but don't count yourself
- if (p[i].grabbed) continue;
- float distance = dist(x, y, p[i].x, p[i].y);
- float radiiSum = r + p[i].r;
- if (distance < radiiSum) {
- float otherVx = p[i].vx;
- float otherVy = p[i].vy;
- p[i].vx = vx;
- p[i].vy = vy;
- vx = otherVx;
- vy = otherVy;
- }
- }
- }
- }
- void display() {
- fill(myColor);
- ellipse(x, y, r*2, r*2); // radius times 2 is diameter
- }
- void move() {
- if (grabbed) {
- x = mouseX;
- y = mouseY;
- vx = mouseX - pmouseX;
- vy = mouseY - pmouseY;
- }
- else {
- x += vx;
- y += vy;
- if (x > width-r || x < r) vx *= -1;
- if (y > height-r || y < r) vy *= -1;
- }
- }
- void record() {
- // -------------------------------------- Recording Help Needed Here
- // save to an arrayList (myIndex,x,y);
- }
- void playback(int x_, int y_) {
- x = x_;
- y = y_;
- // -------------------------------------- Playback Help Needed Here
- // read from an arrayList
- }
- }
Thank you in advance for your help.
- matt
1