The boids successfully avoid the walls thanks to lines 92 - 96, which change the angle of the boids path so that they turn back towards the circle's center. However, when they are at the rightmost side of the circle (at 0 degrees / 2pi), the boids appear to turn to avoid the wall and hit another one instantly, which causes them to keep moving outside of the boundary in a serpentine motion. So, once it's out, it constantly meets the condition to turn, which ends up making it unable to change directions.
Could I check the angle of 'ang,' and if it is at 0 / 2pi, give different values to 'wander_theta'? The angle adjustment works for all other parts of the circle but that one side, so I think the angle would be significant. How should I start to solve this problem?
Cluster cluster;
Stage stage;
//PImage planarian;
void setup() {
size(600,600);
cluster = new Cluster(); //Initialize the arrayList that contains all the moving objects
stage = new Stage(width/2,height/2,250); //Draws a circular stage that contains the moving objects
for (int i = 0; i < 20; i++) {
cluster.addFworm(new Fworm(new PVector(width/2,height/2), 2.0f)); //Adds a new object to the arrayList (first boid
//to the flock)
}
//planarian = loadImage("testW.png");
}
void draw() {
background(220);
stage.display();
cluster.run(); //animates every object in the ArrayList
}
class Stage {
float x;
float y;
float r;
Stage(float x_, float y_, float r_){
x = x_;
y = y_;
r = r_;
}
void display() {
fill(240);
noStroke();
ellipse(x, y, r*2, r*2);
}
}
class Cluster { //the 'flock' class from boids.
ArrayList fworms; // contains all objects
Cluster() {
fworms = new ArrayList();
}
void run() {
for (int i = 0; i < fworms.size(); i++) {
Fworm f = (Fworm) fworms.get(i);
f.run(fworms); //animates each individual object
}
}
void addFworm(Fworm f) { //the add object function
fworms.add(f);
}
}
class Fworm {
PVector loc;
PVector vel;
float max_wander_offset; //upper and lower ends of the range that generates a random number for changing
float wander_theta; //the randomized value assigned to the velocity
float wander_offset;
float maxspeed; //prevents the velocity from steadily increasing
float r;
Fworm(PVector l, float ms) {
loc = l.get();
max_wander_offset = 0.1;
wander_theta = random(TWO_PI);
vel = new PVector();
maxspeed = ms;
r = 6.0;
}
void run(ArrayList fworms) {
update();
// borders(); <-- the now obsolete border checking function from Flocking
I'm still working on a Boids derivative program that has several irregularly-shaped objects randomly moving around the screen. Unlike Boids, I would like users to be able to click on any object in the flock and have it respond to that click. I've read the threads about clicking on objects, but those examples featured circles and squares, while my images are represented by rectangles and can change rotation depending upon the object's heading.
I've tried to do this on lines 40 and 151, and in both cases, it sets the clickable area as an unrotated rectangle at the coordinates of the top-left corner. While this would work if the objects were all static rectangles, they move and rotate, so how could I get the clickable area to rotate with them? Any help would be greatly appreciated!
I'm completely new to Processing and most programming, so I don't even know how to begin coding for what I'm trying to accomplish. I'm making a Boids variant where I have an object that moves semi-randomly in a circular container.
When the object reaches one of the container walls, I want it to turn either left or right in a smooth arc of about 90 degrees in order to avoid the wall instead of immediately reversing directions like a bounce program.
This is my code so far, and I have my questions are in the comments on line 99 in order to avoid repeating myself. ANY advice or tutorials that could get me on the right track would be greatly appreciated, since I'm completely lost at this point.
Cluster cluster;
Stage stage;
//PImage planarian;
void setup() {
size(600,600);
cluster = new Cluster(); //Initialize the arrayList that contains all the moving objects
stage = new Stage(width/2,height/2,250); //Draws a circular stage that contains the moving objects
for (int i = 0; i < 1; i++) {
cluster.addFworm(new Fworm(new PVector(width/2,height/2), 2.0f)); //Adds a new object to the arrayList (first boid
//to the flock)
}
//planarian = loadImage("testW.png");
}
void draw() {
background(220);
stage.display();
cluster.run(); //animates every object in the ArrayList
}
class Stage {
float x;
float y;
float r;
Stage(float x_, float y_, float r_){
x = x_;
y = y_;
r = r_;
}
void display() {
fill(240);
noStroke();
ellipse(x, y, r*2, r*2);
}
}
class Cluster { //the 'flock' class from boids.
ArrayList fworms; // contains all objects
Cluster() {
fworms = new ArrayList();
}
void run() {
for (int i = 0; i < fworms.size(); i++) {
Fworm f = (Fworm) fworms.get(i);
f.run(fworms); //animates each individual object
}
}
void addFworm(Fworm f) { //the add object function
fworms.add(f);
}
}
class Fworm {
PVector loc;
PVector vel;
float max_wander_offset; //upper and lower ends of the range that generates a random number for changing
float wander_theta; //the randomized value assigned to the velocity
float wander_offset;
float maxspeed; //prevents the velocity from steadily increasing
float r;
Fworm(PVector l, float ms) {
loc = l.get();
max_wander_offset = 0.1;
wander_theta = random(TWO_PI);
vel = new PVector();
maxspeed = ms;
r = 6.0;
}
void run(ArrayList fworms) {
update();
// borders(); <-- the now obsolete border checking function from Flocking