how to choose Data File B while the animation is still running based on Date File A?
in
Programming Questions
•
7 months ago
Suppose I have Processing script which will
1. in the setup() block, read from a data.csv file and assign each row of data in this file as certain attributes to an object in an object array, e.g. xyz coordinates and xyz velocity of a point
2. in the draw() block, draw the objects and animate them, e.g. moving each object according to its velocity
I want to ask that, while the animation is going on, how can I choose another .csv file, read it, assign the data to a new object array, and start to animate this new array of objects ...
As far as I know, the script in the setup() block will only be execute for one time after we run the Processing program. Then, how can we intervene here, while the script in the draw() block is be executed frame after frame, by asking Processing to run what's inside the setup() block again using a new input data file through, maybe, controlP5 interface elements?
I hope my question is explained clearly.
Thanks in advance!
Example script (based on Daniel Shiffman's Nature of Code book "NOC-1-7-motion101"):
The content of the data.csv file can be as simple as the following:
300,100,1,1
200,500,-1,-1
1. in the setup() block, read from a data.csv file and assign each row of data in this file as certain attributes to an object in an object array, e.g. xyz coordinates and xyz velocity of a point
2. in the draw() block, draw the objects and animate them, e.g. moving each object according to its velocity
I want to ask that, while the animation is going on, how can I choose another .csv file, read it, assign the data to a new object array, and start to animate this new array of objects ...
As far as I know, the script in the setup() block will only be execute for one time after we run the Processing program. Then, how can we intervene here, while the script in the draw() block is be executed frame after frame, by asking Processing to run what's inside the setup() block again using a new input data file through, maybe, controlP5 interface elements?
I hope my question is explained clearly.
Thanks in advance!
Example script (based on Daniel Shiffman's Nature of Code book "NOC-1-7-motion101"):
- Mover[] movers;
int numMovers;
void setup() {
size(500,500);
smooth();
String[] data = loadStrings("./data.csv");
numMovers = data.length;
movers = new Mover[numMovers];
for (int i=0; i<numMovers; i++){
float[] vars = float(split(data[i],","));
movers[i] = new Mover(vars[0], vars[1], vars[2], vars[3]);
}
}
void draw() {
background(255);
for (int i=0; i<numMovers; i++){
movers[i].update();
movers[i].checkEdges();
movers[i].display();
}
}
class Mover {
PVector location;
PVector velocity;
Mover(float l_x, float l_y, float v_x, float v_y)
{
location = new PVector(l_x, l_y);
velocity = new PVector(v_x, v_y);
}
void update() {
location.add(velocity);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(location.x, location.y, 48, 48);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
}
else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
}
else if (location.y < 0) {
location.y = height;
}
}
}
The content of the data.csv file can be as simple as the following:
300,100,1,1
200,500,-1,-1
1