I am trying to port a processing sketch to openFrameworks
No problem with porting types and other specific language usage but there is one thing i don’t know how to deal with :
paths = (pathfinder[]) append(paths, new pathfinder(this));
in the pathfinder update() method.
the paths array is declared elsewhere. And how this ‘paths’ can be used inside the pathfinder class ?
Sorry i don’t really know how to explain the problem… hope i am making myself clear
here is the Processing code.
thanks a lot
class pathfinder {
PVector location;
PVector velocity;
float diameter;
pathfinder() {
location = new PVector(width/2, height);
velocity = new PVector(0, -1);
diameter = 32;
}
pathfinder(pathfinder parent) {
location = parent.location.get();
velocity = parent.velocity.get();
float area = PI*sq(parent.diameter/2);
float newDiam = sqrt(area/2/PI)*2;
diameter = newDiam;
parent.diameter = newDiam;
}
void update() {
if (diameter>0.5) {
location.add(velocity);
PVector bump = new PVector(random(-1, 1), random(-1, 1));
bump.mult(0.1);
velocity.add(bump);
velocity.normalize();
if (random(0, 1)<0.02) {
paths = (pathfinder[]) append(paths, new pathfinder(this));
}
}
}
}
pathfinder[] paths;
void setup() {
size(800, 600);
background(0);
ellipseMode(CENTER);
fill(255);
noStroke();
smooth();
paths = new pathfinder[1];
paths[0] = new pathfinder();
}
void draw() {
for (int i=0;i<paths.length;i++) {
PVector loc = paths[i].location;
float diam = paths[i].diameter;
ellipse(loc.x, loc.y, diam, diam);
paths[i].update();
}
}
void mousePressed() {
background(0);
paths = new pathfinder[1];
paths[0] = new pathfinder();
}
Working in C++ you shouldn’t really need to use “new” keywords, unless you want to do specific things.
In your case, you just want to append a pathfinder the the end of the paths list so I would recommend changing:
pathfinder[] paths;
to vector<pathfinder> paths;
paths = new pathfinder[1];
to paths.resize(1);
remove paths[0] = new pathfinder();
paths = (pathfinder[]) append(paths, new pathfinder(this));
to paths.emplace_back(this);
There are a few other errors probably.
If you do not know this you do not have a basic understanding of the c++ language syntax. There are so many good tutorials out there. Just read up on them. This is a good starting point www.cplusplus.com/doc/tutorial