Boids keep escaping circle
in
Programming Questions
•
1 year ago
This is an update to my question several weeks ago:
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
- render();
- }
- void update() {
- wander_offset = random(-max_wander_offset, max_wander_offset);
- wander_theta += wander_offset;
- vel.x += cos(wander_theta);
- vel.y += sin(wander_theta);
- vel.limit(maxspeed);
- if (dist(stage.x, stage.y, loc.x, loc.y) > 210) {
- // Determine angle back to center of screen
- float ang = atan2(height/2-loc.y,width/2-loc.x);
- // adjust direction a bit in that direction...
- wander_theta += (ang-wander_theta)/20;
- }
- loc.add(vel);
- }
- void render() {
- float direction = vel.heading2D() + PI/2;
- fill(0); // the fill and stroke is only for the
- stroke(255); // temporary placeholder triangle
- pushMatrix();
- translate(loc.x,loc.y);
- rotate(direction);
- //image(planarian, 0, 0);
- beginShape(TRIANGLES); //I'm using the image above, but the triangles are just so anybody could test this
- vertex(0, -r*2);
- vertex(-r, r*2);
- vertex(r, r*2);
- endShape();
- popMatrix();
- }
- }
1