Using the ".get()" function in a boid flocking program: beginner trouble
in
Programming Questions
•
3 years ago
Hi. I'm new to Processing (as well as to programming in general). I'm teaching myself, and one of the first projects I'm attempting is a simple boid flocking program. I'm having difficulty with the section of code that determines the "cohesion" of the boids (the section highlighted in red below). The program compiles just fine without that section, but encounters this error when I add it in: "Cannot find anything named 'Boid1'."
I'm trying to copy the Pvector of one of the boids, so that I can then run some calculations based on that. The specific line of code is:
Boid1 Neighbour = Boid1s[Boid1] .get();
I know this is incorrect, as are many variants that I've tried. What is the correct syntax for this operation? Any suggestions?
If further clarification is needed, I'll gladly provide it.
Thanks!
// Creates an array of objects (with specified size)
Boid1[] Boid1s = new Boid1[5];
void setup() {
size (600,400);
smooth();
// Initializing all the elements of the array
for (int i = 0; i < Boid1s.length; i++) {
Boid1s[i] = new Boid1();
}
}
void draw() {
background(255);
for (int i = 0; i < Boid1s.length; i++) {
Boid1s[i].run();
}
}
class Boid1 { //defines the class
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
float diameter;
color c1;
Boid1() { //constructor function: gets called whenever a new boid is created
//the below algorithms are referenced in the functions
location = new PVector(random(width),random(height));
velocity = new PVector(random(-2,2),random(-2,2));
acceleration = new PVector(0,0);
topspeed = 4;
diameter = 15;
c1 = color(random(100,255),random(0,70),random(0,70),200);
}
void run(){ // the main function - calls all the other functions listed
update();
display();
avoidEdges();
cohesion();
}
// the below algorithms are called through the main function (see above)
void update() {
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display() {
stroke(c1);
fill(c1);
ellipse(location.x,location.y,diameter,diameter);
}
void avoidEdges() {
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x*-1;;
}
if ((location.y > height) || (location.y < 0)){
velocity.y = velocity.y*-1;
}
}
void cohesion(){ // each agent steers towards the average position of its neighbours
PVector aveLocation = new PVector(); // PVector to store average location
int neighbourcount = 0; // int to count the number of neighbours
for (int i = 0; i < Boid1s.length; i++){
Boid1 Neighbour = Boid1s[Boid1] .get(); //Gets a copy of the vector, returns a PVector object.
float distance = PVector.dist(location,Neighbour.location); //Calculates the Euclidean distance between //points
if (distance < 100){
aveLocation.add(Neighbour.location);
neighbourcount++;
}
}
aveLocation.div(neighbourcount); // divide by num neighbours to get the average - the center position of our near //neighbours
PVector toCenter = PVector.sub(aveLocation,Location); // make a vector to that point
toCenter.normalize(); // scale it to 1
toCenter.mult(.1); // multiply by .1
acceleration.add(toCenter); // add to the acceleration
}
} //closes class
I'm trying to copy the Pvector of one of the boids, so that I can then run some calculations based on that. The specific line of code is:
Boid1 Neighbour = Boid1s[Boid1] .get();
I know this is incorrect, as are many variants that I've tried. What is the correct syntax for this operation? Any suggestions?
If further clarification is needed, I'll gladly provide it.
Thanks!
// Creates an array of objects (with specified size)
Boid1[] Boid1s = new Boid1[5];
void setup() {
size (600,400);
smooth();
// Initializing all the elements of the array
for (int i = 0; i < Boid1s.length; i++) {
Boid1s[i] = new Boid1();
}
}
void draw() {
background(255);
for (int i = 0; i < Boid1s.length; i++) {
Boid1s[i].run();
}
}
class Boid1 { //defines the class
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
float diameter;
color c1;
Boid1() { //constructor function: gets called whenever a new boid is created
//the below algorithms are referenced in the functions
location = new PVector(random(width),random(height));
velocity = new PVector(random(-2,2),random(-2,2));
acceleration = new PVector(0,0);
topspeed = 4;
diameter = 15;
c1 = color(random(100,255),random(0,70),random(0,70),200);
}
void run(){ // the main function - calls all the other functions listed
update();
display();
avoidEdges();
cohesion();
}
// the below algorithms are called through the main function (see above)
void update() {
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display() {
stroke(c1);
fill(c1);
ellipse(location.x,location.y,diameter,diameter);
}
void avoidEdges() {
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x*-1;;
}
if ((location.y > height) || (location.y < 0)){
velocity.y = velocity.y*-1;
}
}
void cohesion(){ // each agent steers towards the average position of its neighbours
PVector aveLocation = new PVector(); // PVector to store average location
int neighbourcount = 0; // int to count the number of neighbours
for (int i = 0; i < Boid1s.length; i++){
Boid1 Neighbour = Boid1s[Boid1] .get(); //Gets a copy of the vector, returns a PVector object.
float distance = PVector.dist(location,Neighbour.location); //Calculates the Euclidean distance between //points
if (distance < 100){
aveLocation.add(Neighbour.location);
neighbourcount++;
}
}
aveLocation.div(neighbourcount); // divide by num neighbours to get the average - the center position of our near //neighbours
PVector toCenter = PVector.sub(aveLocation,Location); // make a vector to that point
toCenter.normalize(); // scale it to 1
toCenter.mult(.1); // multiply by .1
acceleration.add(toCenter); // add to the acceleration
}
} //closes class
1