Additional function in boid class
in
Contributed Library Questions
•
2 years ago
Hey guys,
I'm trying to get my agent (or boid) system to interact with
this Gestaltung sketch.
Basically there's a rhizomic network in the centre, and:
1)
I want the individual agents to stop (if they get close to one of the blinking "nodes")
2)
a new line (called "spring" in the code), drawn to connect the agent with the nearby node
I've started a new function called "connect" under the Agent class. Currently it's "deactivated" so you can see how the code works without the function.
If you know what's wrong, let me know, I would be very grateful!
Theres a library
generativedesign, which you will need to make the code work.
- //
- // Original code by
- // Generative Gestaltung, ISBN: 978-3-87439-759-9
- // http://www.generative-gestaltung.de
- //
- // Flocking agents based on pseudo-code by Gwyllim Jahn
- /**
- * more nodes and more springs
- *
- * KEYS
- * r : reset positions
- * s : save png
- * p : save pdf
- */
- import generativedesign.*;
- import processing.pdf.*;
- boolean savePDF = false;
- // an array for the nodes
- Node[] nodes = new Node[60];
- // an array for the springs
- Spring[] springs = new Spring[0];
- // dragged node
- Node selectedNode = null;
- float nodeDiameter = 15;
- //agent variables
- ArrayList agentPop;
- //Create some global variables for our agents:
- //a float variable called align, make this equal to 20
- float align = 20;
- //a float variable called avoid, make this equal to 5
- float avoid = 5;
- //a float variable called falloff, make this equal to 1
- float falloff = 1;
- //an integer variable called numAgents, make this equal to 500
- int numAgents = 100;
- void setup() {
- size(700, 700);
- background(255);
- smooth();
- noStroke();
- initNodesAndSprings();
- //Introduce arraylist
- agentPop = new ArrayList();
- for (int i = 0; i < numAgents; i++) {
- PVector loc = new PVector (random(0, width), random (0, height), 0);
- PVector vel = new PVector (random (-1,1), random (-1,1), 0);
- float s = 5;
- int c = 50;
- Agent frank = new Agent(loc, s, c, vel);
- agentPop.add(frank);
- }
- }
- void draw() {
- if (savePDF) beginRecord(PDF, timestamp()+".pdf");
- background(255);
- // fill(255, 20);
- // rect(0, 0, width, height);
- // let all nodes repel each other
- for (int i = 0 ; i < nodes.length; i++) {
- nodes[i].attract(nodes);
- }
- // apply spring forces
- for (int i = 0 ; i < springs.length; i++) {
- springs[i].update();
- }
- // apply velocity vector and update position
- for (int i = 0 ; i < nodes.length; i++) {
- nodes[i].update();
- }
- if (selectedNode != null) {
- selectedNode.x = mouseX;
- selectedNode.y = mouseY;
- }
- // draw springs
- stroke(100);
- strokeWeight(2);
- for (int i = 0 ; i < springs.length; i++) {
- line(springs[i].fromNode.x, springs[i].fromNode.y, springs[i].toNode.x, springs[i].toNode.y);
- }
- // draw nodes
- noStroke();
- for (int i = 0 ; i < nodes.length; i++) {
- fill(255);
- ellipse(nodes[i].x, nodes[i].y, nodeDiameter, nodeDiameter);
- fill(0, random(10,255), random(10,255));
- ellipse(nodes[i].x, nodes[i].y, nodeDiameter-4, nodeDiameter-4);
- }
- if (savePDF) {
- savePDF = false;
- println("saving to pdf – finishing");
- endRecord();
- }
- //loop through agent system
- for (int j = 0; j <agentPop.size(); j++) {
- Agent a = (Agent) agentPop.get(j);
- a.run();
- }
- }
- void initNodesAndSprings() {
- // init nodes
- float rad = nodeDiameter/2;
- for (int i = 0; i < nodes.length; i++) {
- nodes[i] = new Node(width/2+random(-200, 200), height/2+random(-200, 200));
- nodes[i].setBoundary(rad, rad, width-rad, height-rad);
- nodes[i].setRadius(100);
- nodes[i].setStrength(-5);
- }
- // set springs randomly
- springs = new Spring[0];
- for (int j = 0 ; j < nodes.length-1; j++) {
- int rCount = floor(random(1, 2));
- for (int i = 0; i < rCount; i++) {
- int r = floor(random(j+1, nodes.length));
- Spring newSpring = new Spring(nodes[j], nodes[r]);
- newSpring.setLength(15);
- newSpring.setStiffness(1);
- springs = (Spring[]) append(springs, newSpring);
- }
- }
- }
- void mousePressed() {
- // Ignore anything greater than this distance
- float maxDist = 20;
- for (int i = 0; i < nodes.length; i++) {
- Node checkNode = nodes[i];
- float d = dist(mouseX, mouseY, checkNode.x, checkNode.y);
- if (d < maxDist) {
- selectedNode = checkNode;
- maxDist = d;
- }
- }
- }
- void mouseReleased() {
- if (selectedNode != null) {
- selectedNode = null;
- }
- }
- void keyPressed() {
- if(key=='s' || key=='S') saveFrame(timestamp()+"_##.png");
- if(key=='p' || key=='P') {
- savePDF = true;
- println("saving to pdf - starting (this may take some time)");
- }
- if(key=='r' || key=='R') {
- background(255);
- initNodesAndSprings();
- }
- }
- String timestamp() {
- return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", Calendar.getInstance());
- }
- // random flocking boids
- class Agent {
- //--------------------------------------------CLASS PROPERTIES
- PVector location;
- PVector velocity;
- float agentSize = 8;
- int col = 50;
- //--------------------------------------------CLASS CONSTRUCTOR
- Agent (PVector LOCATION, float AGENTSIZE, int AGENTCOLOUR, PVector VELOCITY){
- location = LOCATION;
- agentSize = AGENTSIZE;
- velocity = VELOCITY;
- col = AGENTCOLOUR;
- }
- void run () {
- //run render
- render();
- //run updatePos
- updatePos();
- //run flock
- flock();
- //new function, to stop agent from moving and connect the agent to the node
- //connect();
- }
- void render () {
- fill(col);
- ellipse (location.x, location.y, agentSize, agentSize);
- }
- //updates the position of the agents
- void updatePos () {
- location.add(velocity);
- if ( location.x < 0) {
- location.x = width;
- }
- if (location.x > width) {
- location.x = 0;
- }
- if (location.y < 0) {
- location.y = height;
- }
- if (location.y > height) {
- location.y = 0;
- }
- }
- /*
- //function to stop agent from moving when it gets close to a node, connect the agent to the node with a new spring/line
- void connect(){
- //loop through all the nodes in the list
- for (int i = 0 ; i < nodes.length; i++) {
- Node checkNode = nodes[i];
- float distance = dist(location.x, location.y, checkNode.x, checkNode.y);
- if (distance < 20) {
- // draw springs
- stroke(100);
- strokeWeight(2);
- for (int i = 0 ; i < springs.length; i++) {
- line(springs[i].fromNode.x, springs[i].fromNode.y, springs[i].tolocation.x, springs[i].tolocation.y);
- }
- }
- }
- }
- */
- void flock () {
- float minDist = 1000000;
- for (int k = 0; k < agentPop.size(); k++) {
- Agent a = (Agent) agentPop.get(k);
- float agentDist = PVector.dist(location, a.location);
- if (agentDist > 0) {
- if (agentDist < minDist) {
- minDist = agentDist;
- }
- if (agentDist < align) {
- PVector addVec;
- addVec = a.velocity.get();
- addVec.mult(falloff/agentDist);
- velocity.add(addVec);
- velocity.normalize();
- agentSize = agentSize - 0.1;
- }
- if (agentDist < avoid) {
- PVector addVec = a.location.get();
- addVec.sub(location);
- addVec.mult(falloff/agentDist);
- addVec.sub(velocity);
- velocity.normalize();
- }
- }
- }
- if (agentSize < 8) {
- agentSize=8;
- }
- if (agentSize > 8) {
- agentSize= 8;
- }
- }
- }
Thanks heaps,
Josh
1