Boids and obstacles
in
Programming Questions
•
2 years ago
Hello,
I'm trying to make a boids system with obstacles in processing. I made the system but can not make the boids react on obstacle: 1. To group around obstacle permanently 2. To make my boids pass round the obstacle. I attached text file with my code. Could somebody help me, please, seems that I'm completely lost here. Thank you,
Evgeniya
- import toxi.geom.*;
- import controlP5.*;
- //DECLARE
- ControlP5 jControl;
- ArrayList ballCollection;
- int bColor=0;
- int lineBetween=10;
- int separate=10;
- int cohesion=10;
- int sEllipse=10;
- int eMove=1;
- int align=200;
- void setup() {
- size(600,600);
- smooth();
- ballCollection = new ArrayList();
- jControl=new ControlP5(this);
- Slider b = jControl.addSlider("bColor", 0,255,0,10,10,200,10);
- Slider l = jControl.addSlider("lineBetween", 0,80,10,10,25,200,10);
- Slider s = jControl.addSlider("separate", 0,80,10,10,40,200,10);
- Slider c = jControl.addSlider("cohesion", 0,100,10,10,55,200,10);
- Slider e = jControl.addSlider("sEllipse", 0,15,10,10,70,200,10);
- Slider m = jControl.addSlider("eMove", 0,7,1,10,85,200,10);
- Slider a = jControl.addSlider("align", 0,400,100,10,100,200,10);
- for(int i = 0; i < 100; i++) {
- Vec3D origin = new Vec3D(random(width),random(200),0);
- Ball myBall = new Ball(origin);
- ballCollection.add(myBall);
- }
- }
- void draw() {
- background(bColor);
- fill(255,10);
- fill(255);
- ellipse (width/3,height/4, 15,15);
- ellipse (width/2,height/1.5, 15,15);
- for(int i = 0; i < ballCollection.size(); i++) {
- Ball mb = (Ball) ballCollection.get(i);
- mb.run();
- }
- if(keyPressed) saveFrame("agent_###.jpg");
- }
- void mousePressed() {
- Vec3D origin = new Vec3D(mouseX,mouseY,0);
- Ball myBall = new Ball(origin);
- ballCollection.add(myBall);
- }
- class Ball {
- Vec3D loc = new Vec3D (0,0,0);
- Vec3D speed = new Vec3D(random(-10,10),random(-10,10),0);
- Vec3D acc=new Vec3D();
- Vec3D grav = new Vec3D(0,0.2,0);
- float r;
- Ball(Vec3D _loc) {
- loc = _loc;
- r = 0.1;
- }
- void run() {
- display();
- move();
- // bounce();
- // gravity();
- borders();
- lineBetween();
- flock();
- }
- void flock() {
- separate(4);
- cohesion(0.001);
- align(0.1);
- }
- void align (float magnitude) {
- Vec3D steer =new Vec3D();
- int count=0;
- for (int i=0; i<ballCollection.size(); i++) {
- Ball other=(Ball) ballCollection.get(i);
- float distance=loc.distanceTo(other.loc);
- if (distance>0&&distance>align) {
- steer.addSelf(other.speed);
- count++;
- }
- }
- if (count>0) {
- steer.scaleSelf(1.0/count);
- }
- steer.scaleSelf (magnitude);
- acc.addSelf (steer);
- }
- void cohesion(float magnitude) {
- Vec3D sum =new Vec3D();
- int count=0;
- for (int i=0; i<ballCollection.size(); i++) {
- Ball other=(Ball) ballCollection.get(i);
- float distance=loc.distanceTo(other.loc);
- if (distance>0&&distance<cohesion) {
- sum.addSelf(other.loc);
- count++;
- }
- }
- if (count>0) {
- sum.scaleSelf(1.0/count);
- }
- Vec3D steer=sum.sub (loc);
- steer.scaleSelf(magnitude);
- acc.addSelf(steer);
- }
- void separate(float magnitude) {
- Vec3D steer=new Vec3D();
- int count=0;
- for (int i=0; i<ballCollection.size(); i++) {
- Ball other=(Ball) ballCollection.get(i);
- float distance=loc.distanceTo(other.loc);
- if (distance>0&&distance<separate) {
- Vec3D diff=loc.sub (other.loc);
- diff.normalizeTo (1.0/distance);
- steer.addSelf(diff);
- count++;
- }
- }
- if(count>0) {
- steer.scaleSelf(1.0/count);
- }
- steer.scaleSelf(magnitude);
- acc.addSelf(steer);
- }
- void lineBetween() {
- //ballCollection
- for (int i=0; i<ballCollection.size(); i++) {
- Ball other=(Ball) ballCollection.get(i);
- float distance=loc.distanceTo(other.loc);
- if (distance>0&&distance<lineBetween) {
- stroke (255,0,0);
- strokeWeight (0.1);
- line(loc.x,loc.y,other.loc.x,other.loc.y);
- }
- }
- }
- void gravity() {
- speed.addSelf(grav);
- }
- void bounce() {
- if(loc.x > width) {
- speed.x = speed.x * -1;
- }
- if(loc.x< 0) {
- speed.x = speed.x * -1;
- }
- if(loc.y > height) {
- speed.y = speed.y * -1;
- }
- if(loc.y < 0) {
- speed.y = speed.y * -1;
- }
- }
- void borders() {
- if (loc.x < -r) loc.x = width+r;
- if (loc.y < -r) loc.y = height+r;
- if (loc.x > width+r) loc.x = -r;
- if (loc.y > height+r) loc.y = -r;
- }
- void move() {
- speed.addSelf(acc);
- speed.limit(eMove);
- loc.addSelf(speed);
- acc.clear();
- }
- void display() {
- strokeWeight(0.01);
- stroke (255,0,0);
- fill(157);
- ellipse(loc.x,loc.y,sEllipse,sEllipse);
- // fill(157);
- // triangle(loc.x,loc.y,loc.x+2,loc.y+10,loc.x-2,loc.y+10);
- }
- }
1