Autonomous Behaviour - A Processing Compendium, New York 2012

Page 1

Autonomous Behavior: A Processing Compendium New York, Spring 2012 Claus Rytter Bruun De Neergaard


processing compendium

2


Introduction Mine tanker med de heri samlede algoritmiske øvelser er først og fremmest at, forstå hvordan det er at arbejde i en digital proces inden for et kreative felt (kunst og arkitektur). Men det handler også om forståelsen af potentialet i værktøjet og om at se koden eller algoritmen som et ‘materiale’ der kan formes og tilpasses, og om at blive klogere på begrænsningerne hvis ikke den tekniske snilde er tilstrækkelig. Det præsenterede arbejde er en del af resultatet fra et semester i New York, hvor jeg, gennem cirka halvdelen af forløbet har haft work-sessions og diskussioner med både Marius Watz (www. mariuswatz.com) og Brandt Graves (www.thefuturefuture.com) drejende sig om emner som generativ kunst, formidling af digitale øvelser og digital kunstform generelt, algoritmen som materiale og – ikke mindst – digital fabrikation eller fra-digital-til-fysisk. Indledningsvist vil jeg gerne forklare hvordan fascinationen med krativ kodning startede. Forestil dig et rum, ikke nødvendigvis helt defineret i sin afgrænsning, men et stort rum med plads til at manøvrere. Vi indsætter, i dette rum, hundredvis eller tusindvis af entiteter, som hver især er bygget op omkring et, to eller tre regelsæt, og flere variabler til at værdisætte de enkelte regelsæt, samt en sanse-radius. Nu trykker vi på ‘play’-knappen, og ser scenariet udfolde sig. Alle entiteterne bevæger sig i rummet. Alle entiteterne er på ethvert givent tidspunkt opmærksom på alle andre entiteter i rummet. Dog bliver alle entiter uden for entitetens sanse-radius dømt irrelevante. Sker det at, en anden entitet kommer tættere på en sanse-radius, aktiveres et, to eller tre regelsæt. + Det første regelsæt fortæller entiteten, at den skal dreje væk fra alle andre entiteter der befinder sig inden for dens sanse-radius (findes flere entiteter inden for sanseradius, vælg da den gennemsnitligt bedste retning væk fra alle andre). + Det andet regelsæt fortæller entiteten, at den skal styre mod alle andre entiteter der befinder sig inden for dens sanse-radius (findes flere entiteter inden for sanse-radius, vælg da retning af det gennemsnitlige punkt). + Det tredje regelsæt fortæller entiteten, at den skal skal finde samme retning som alle andre entiteter der befinder sig inden for dens sanse-radius (findes flere entiteter inden for sanse-radius, styr da i den gennemsnitlige retning). Nu ser vi, i rummet, at alle entiteter manøvrerer denne fiktive verden, samtidig med konstant at være opmærksom på alle andre entiteter. Hver enste gang en entitet trænger ind i en anden entitets sanse-radius, aktiveres reglerne, og der etableres et forhold mellem de to. Dette forhindrer overfyldte områder, samtidig med at fordre klyngedannelser og fælles retningsmønstre. Sakker en entitet bagud i en klynge, forsvinder den ud af sanse-radius, og vil påtage sig at vandre hvileløst rundt, til den træder ind i en anden sanse-radius. På denne måde er det muligt at simulere uforudsigelige møndstre og adfærd via relativt simple algortimer. Der vil, fra stramt definerede strukturer, vokse selvorganiserende adfærdsmønstre, som vi ikke ville kunne forudsige eller beregne. Grundstenene er de tre regelsæt der definerer interaktionen mellem entiteterne. De følgende sider viser resultater fra den samme algoritme, som skrevet i Processing (www. processing.org), omend med små ændringer ved skift fra to dimensioner til tre dimensioner.

processing compendium

3


class Agent { // constructor (runs only once) Agent() { }

// update calls all other functions void update() { animation(); edges(); display(); }

// update location void animate() { separation(); cohesion(); alignment(); }

// check if agent is outside window void edges() { }

// draw agent void display() { }

// separation, PVector separation() {

see page 5

}

// cohesion, PVector cohesion() { steer(); }

see page 6

// alignment, PVector alignment() {

see page 7

}

// steering PVector steer() { }

processing compendium

4


processing compendium

5


processing compendium

6


processing compendium

7


The building blocks Space Space 1: Two dimensional Space 2: Three dimensional Form Form Form Form Form

1: 2: 3: 4:

Circle Point Line Sphere

Rules Rule 1: Separation Rule 2: Cohesion Rule 3: Alignment

processing compendium

8


Scenario 1 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Each agent is visualized as a point (Form 2) in space. Subsequent patterns of movement based on separation (Rule 1). There are no external relations, only internal relations define the movement of each agent.

processing compendium

9


Scenario 1 = S1 + F2 + R1 // number of agents = 1000; range = 10.0; separation scale = 2.0; cohesion scale = 1.5; alignment scale = 2.5; frame number = 1194.

Scenario 1 = S1 + F2 + R1 // number of agents = 1000; range = 10.0; separation scale = 2.0; cohesion scale = 1.5; alignment scale = 2.5; frame number = 756.

processing compendium

10


Scenario 1 = S1 + F2 + R1 // number of agents = 1000; range = 10.0; separation scale = 2.0; cohesion scale = 1.5; alignment scale = 2.5; frame number = 950.

Scenario 1 = S1 + F2 + R1 // number of agents = 1000; range = 50.0; separation scale = 7.5; cohesion scale = 1.5; alignment scale = 2.5; frame number = 164.

processing compendium

11


Scenario 1 = S1 + F2 + R1 // number of agents = 4000; range = 50.0; separation scale = 2.0; cohesion scale = 1.5; alignment scale = 2.5; frame number = 27.

Scenario 1 = S1 + F2 + R1 // number of agents = 2000; range = 50.0; separation scale = 7.5; cohesion scale = 1.5; alignment scale = 2.5; frame number = 66.

processing compendium

12


Scenario 1 = S1 + F2 + R1 // number of agents = 4000; range = 50.0; separation scale = 2.0; cohesion scale = 1.5; alignment scale = 2.5; frame number = 55.

Scenario 1 = S1 + F2 + R1 // number of agents = 4000; range = 50.0; separation scale = 2.0; cohesion scale = 1.5; alignment scale = 2.5; frame number = 86.

processing compendium

13


Scenario 2 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Each agent is visualized as a point (Form 2) in space. Subsequent patterns of movement based on separation (Rule 1) and cohesion (Rule 2). There are no external relations, only internal relations define the movement of each agent.

processing compendium

14


Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 1500; range = 150.0; separation scale = 20.0; cohesion scale = 0.5; alignment scale = 3.5; frame number = 120.

Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 1500; range = 150.0; separation scale = 5.0; cohesion scale = 10.5; alignment scale = 3.5; frame number = 77.

processing compendium

15


Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 2000; range = 350.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 14.

Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 500; range = 150.0; separation scale = 2.5; cohesion scale = 2.5; alignment scale = 3.5; frame number = 181.

processing compendium

16


Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 2000; range = 350.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 43.

Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 2500; range = 150.0; separation scale = 2.5; cohesion scale = 2.5; alignment scale = 3.5; frame number = 23.

processing compendium

17


Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 2500; range = 150.0; separation scale = 5.0; cohesion scale = 10.5; alignment scale = 3.5; frame number = 88.

Scenario 2 = S1 + F2 + R1 + R2 // number of agents = 500; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 401.

processing compendium

18


Scenario 3 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Each agent is visualized as a point (Form 2) in space. Subsequent patterns of movement based on cohesion (Rule 2) and alignment (Rule 3). There are no external relations, only internal relations define the movement of each agent.

processing compendium

19


Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 800; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 181.

Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 800; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 213.

processing compendium

20


Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 800; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 256.

Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 1500; range = 150.0; separation scale = 2.5; cohesion scale = 7.5; alignment scale = 1.5; frame number = 144.

processing compendium

21


Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 800; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 53.

Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 1500; range = 150.0; separation scale = 2.5; cohesion scale = 7.5; alignment scale = 3.5; frame number = 62.

processing compendium

22


Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 800; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 341.

Scenario 3 = S1 + F2 + R2 + R3 // number of agents = 800; range = 150.0; separation scale = 2.5; cohesion scale = 5.5; alignment scale = 3.5; frame number = 125.

processing compendium

23


Scenario 4 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Each agent is visualized as a point (Form 2) in space. Subsequent patterns of movement based on separation (Rule 1), cohesion (Rule 2) and alignment (Rule 3). There are no external relations, only internal relations define the movement of each agent.

processing compendium

24


Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 1800; range = 50.0; separation scale = 1.5; cohesion scale = 5.5; alignment scale = 7.5; frame number = 56.

Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 1800; range = 50.0; separation scale = 1.5; cohesion scale = 5.5; alignment scale = 7.5; frame number = 20.

processing compendium

25


Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 800; range = 50.0; separation scale = 1.5; cohesion scale = 7.5; alignment scale = 5.5; frame number = 83.

Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 800; range = 250.0; separation scale = 1.5; cohesion scale = 7.5; alignment scale = 5.5; frame number = 101.

processing compendium

26


Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 800; range = 250.0; separation scale = 1.5; cohesion scale = 7.5; alignment scale = 5.5; frame number = 195.

Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 800; range = 250.0; separation scale = 1.5; cohesion scale = 7.5; alignment scale = 5.5; frame number = 22.

processing compendium

27


Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 800; range = 50.0; separation scale = 1.5; cohesion scale = 7.5; alignment scale = 5.5; frame number = 160.

Scenario 4 = S1 + F2 + R1 + R2 + R3 // number of agents = 800; range = 50.0; separation scale = 1.5; cohesion scale = 7.5; alignment scale = 5.5; frame number = 23.

processing compendium

28


Scenario 5 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Each agent is visualized as a circle (Form 1) in space. Subsequent patterns of movement based on separation (Rule 1), cohesion (Rule 2) and alignment (Rule 3). There are no external relations, only internal relations define the movement of each agent.

processing compendium

29


Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 1000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 1.5; frame number = 157.

Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 1000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 1.5; frame number = 20.

processing compendium

30


Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 2000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 1.5; frame number = 50.

Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 2000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 16.

processing compendium

31


Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 2000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 30.

Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 2000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 6.

processing compendium

32


Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 2000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 60.

Scenario 5 = S1 + F1 + R1 + R2 + R3 // number of agents = 2000; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 92.

processing compendium

33


Scenario 6 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Each agent is visualized as a circle (Form 1) in space. All agents located within a defined range of another agent is visually connected by a line (Form 3). Subsequent patterns of movement based on separation (Rule 1), cohesion (Rule 2) and alignment (Rule 3). There are no external relations, only internal relations define the movement of each agent.

processing compendium

34


Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 107.

Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 35.

processing compendium

35


Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 200; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 108.

Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 200; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 17.

processing compendium

36


Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 111.

Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 200; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 187.

processing compendium

37


Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 70.

Scenario 6 = S1 + F1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 8.

processing compendium

38


Scenario 7 Defined number of agents in a two dimensional space (Space 1), random point of initiation. Agents are not visualized, only agents located within a defined range of another agent is visually connected by a line (Form 3). Subsequent patterns of movement based on separation (Rule 1), cohesion (Rule 2) and alignment (Rule 3). There are no external relations, only internal relations define the movement of each agent.

processing compendium

39


Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 300; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 122.

Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 300; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 116.

processing compendium

40


Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 51.

Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 400; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 51.

processing compendium

41


Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 300; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 336.

Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 300; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 422.

processing compendium

42


Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 300; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 51.

Scenario 7 = S1 + F3 + R1 + R2 + R3 // number of agents = 300; range = 125.0; separation scale = 0.5; cohesion scale = 7.5; alignment scale = 10.5; frame number = 523.

processing compendium

43


Scenario 8 Defined number of agents in a three dimensional space (Space 2), random point of initiation. Each agent is visualized as a sphere (Form 4) in space. All agents located within a defined range of another agent is visually connected by a line (Form 3). Subsequent patterns of movement based on separation (Rule 1), cohesion (Rule 2) and alignment (Rule 3). There are no external relations, only internal relations define the movement of each agent.

processing compendium

44


Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 1000; range = 50.0; separation scale = 30.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 126.

Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 1000; range = 50.0; separation scale = 30.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 203.

processing compendium

45


Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 500; range = 150.0; separation scale = 1.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 48.

Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 500; range = 150.0; separation scale = 1.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 85.

processing compendium

46


Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 500; range = 150.0; separation scale = 1.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 75.

Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 500; range = 150.0; separation scale = 1.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 103.

processing compendium

47


Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 500; range = 150.0; separation scale = 1.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 236.

Scenario 8 = S2 + F3 + F4 + R1 + R2 + R3 // number of agents = 500; range = 150.0; separation scale = 1.0; cohesion scale = 7.5; alignment scale = 13.5; frame number = 134.

processing compendium

48


The algorithm 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106

// VARS /////////////////////////////////////////////////////////////////////// int na = 300; // number of agents float range = 125.0; // range of each agent float sepscale = 0.5; // separation scale float aliscale = 10.5; // alignment scale float cohscale = 7.5; // cohesion scale color acolor = color(100); // agent color float aradius = 1.5; // agent size boolean connect = true;

// determins if connections are drawn

ArrayList agents = new ArrayList();

// an ArrayList to store all agents

// SETUP ///////////////////////////////////////////////////////////////////// void setup() { size(1189, 841); background(230); smooth(); ellipseMode(CENTER);

}

for (int i = 0; i < na; i++) { agents.add(new Agent()); }

// DRAW ////////////////////////////////////////////////////////////////////// void draw() { // create trail for each agent noStroke(); fill(230, 100); rect(-1, -1, width+1, height+1); // calling functions of all of the objects in the ArrayList for (int i = 0; i < agents.size(); i++) { Agent a = (Agent) agents.get(i); a.update(agents); }

}

// draw connections if (connect == true) { for (int i = 0; i < agents.size(); i++) { Agent ca = (Agent) agents.get(i); for (int j = i; j < agents.size(); j++) { Agent oa = (Agent) agents.get(j); if (dist(ca.loc.x, ca.loc.y, oa.loc.x, oa.loc.y) < (range/2)) { stroke(150); strokeWeight(0.5); line(ca.loc.x, ca.loc.y, oa.loc.x, oa.loc.y); } } } }

// KEYPRESSED //////////////////////////////////////////////////////////////// void keyPressed() { // save tif-file if (key == ‘i’) { save(“S7/” + “_na” + na + “_ra” + range + “_sep” + sepscale + “_ali” + aliscale + “_coh” + cohscale + “_fr” + frameCount + “.tif”); }

}

// enable or disable connections if (key == ‘1’) { connect = true; } else if (key == ‘2’) { connect = false; }

class Agent { PVector loc, vel, acc, ploc; float maxforce, maxvel; Agent() { // three vectors to describe agent location, velocity and acceleration loc = new PVector(random(0, width), random(0, height)); vel = new PVector(0, 0); acc = new PVector(0, 0); ploc = loc;

}

// maxforce makes sure that not all agents move with same speed - it creates variation maxforce = random(0.10, 0.75); maxvel = random(3.5, 10.5);

// UPDATE ////////////////////////////////////////////////////////////////// void update(ArrayList pop) { // update the movement animate(pop); // update position vel.add(acc); vel.limit(maxvel);

// add acceleration to velocity (increase speed) // make sure there is a movement speed limit

processing compendium

49


The algorithm (cont.) 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

loc.add(vel); acc.set(0, 0, 0);

}

// add velocity to location // make sure we set acceleration back to zero

// standard update functions edges(); display();

// ANIMATE ///////////////////////////////////////////////////////////////// void animate(ArrayList pop) { // calculate the steering vectors PVector sep = separation(pop); PVector coh = cohesion(pop); PVector ali = alignment(pop); // scale all vectors sep.mult(sepscale); coh.mult(cohscale); ali.mult(aliscale);

}

// add all steering vectors to movement acc.add(sep); acc.add(coh); acc.add(ali);

// EDGES /////////////////////////////////////////////////////////////////// void edges() { if (loc.x > width) { loc.x = 0; } else if (loc.x < 0) { loc.x = width; }

}

if (loc.y > height) { loc.y = 0; } else if (loc.y < 0) { loc.y = height; }

// DISPLAY ///////////////////////////////////////////////////////////////// void display() { noFill(); strokeWeight(0.1); stroke(acolor); //ellipse(loc.x, loc.y, maxvel*8.25, maxvel*8.25); //line(ploc.x, ploc.y, loc.x, loc.y); //point(loc.x, loc.y); } // RULE 1 - SEPARATION ///////////////////////////////////////////////////// /* Separation its range. same range separation */

tells the current agent to avoid the neighbors that find themselves within Separation will effectively prevent ‘overcrowding’. Generally we use the variable for all steering behaviors, however, we decrease the range of to ensure cohesion can occur (range/3).

PVector separation(ArrayList pop) { PVector sum = new PVector(); int n = 0; for (int i = 0; i < pop.size(); i++) { Agent a = (Agent) pop.get(i); float d = loc.dist(a.loc);

}

// vector to return // counter

// dist from agent(i) to all other agents

// if dist is within range, and larger than zero (zero being itself) if ((d > 0) && (d < range/3)) { // calculate vector pointing away from neighbor(s) sum = loc.get(); sum.sub(a.loc); sum.normalize(); sum.mult(1/d); n++; }

// calculate average (based on how many agents within range) if (n > 0) { sum.mult(1/float(n)); } }

return sum;

// return the vector

// RULE 2 - COHESION /////////////////////////////////////////////////////// /* Cohesion is the steering towards the average position of all the population members that find themselves within the defined range of the current agent. The result of cohesion is that the population begins to show the effect of group behavior or clustering, imitating that of living creatures. It is often only in combination with rule 1, separation, that we see the full effect of cohesion. Without separation, the agents will cluster up tightly and, at least visually, merge. */ PVector cohesion(ArrayList pop) { PVector sum = new PVector(); int n = 0;

// vector to return // counter

for (int i = 0; i < pop.size(); i++) {

processing compendium

50


The algorithm (cont.) 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 289 290 291 292 293 294 295 296 297 298 299 300 301

Agent a = (Agent) pop.get(i); float d = loc.dist(a.loc);

}

// dist from agent(i) to all other agents

// if dist is within range, and larger than zero (zero being itself) if ((d > 0) && (d < range)) { sum.add(a.loc); n++; }

// calculate average (based on how many agents within range) if (n > 0) { sum.mult(1/float(n)); sum = steer(sum, true); // sum = result of steer()-function }

}

return sum;

// return the vector

// RULE 3 - ALIGNMENT ////////////////////////////////////////////////////// /* Alignment is the averaging of all the directions (velocities) of agents within a given range of the current agent. The result of alignment is that the population begins to flow in a similar manner, resulting in a more recognizeable behavioral pattern. The more we emphasize the alignment, the greater are the chances of creating ‘streams’ of agents, that move in same direction within a certain ‘flow’. */ PVector alignment(ArrayList pop) { PVector sum = new PVector(); int n = 0; for (int i = 0; i < pop.size(); i++) { Agent a = (Agent) pop.get(i); float d = loc.dist(a.loc);

// vector to return // counter

}

// dist from agent(i) to all other agents

// if dist is within range, and larger than zero (zero being itself) if ((d > 0) && (d < range)) { sum.add(a.vel); n++; }

// calculate average (based on how many agents within range) if (n > 0) { sum.mult(1/float(n)); sum.limit(maxforce); }

}

return sum;

// return the vector

// (RULE 4) - STEER //////////////////////////////////////////////////////// /* Steering calculates a steering vector towards a target - the steer function takes a second argument, and if true, it slows down as it approaches the target. */ PVector steer(PVector target, boolean slow) { PVector steerforce; // the steering vector target.sub(loc); float m = target.mag(); // calc the magnitude of the vector // if dist is within range, and larger than zero (zero being itself) if (m > 0 && m < range) { target.normalize(); // calc deceleration if slow is on, else, just continue if ((slow == true) && (m < (range/2))) { target.mult(maxvel * (range/width)); } else { target.mult(maxvel); } target.sub(vel); steerforce = target.get(); steerforce.limit(maxforce);

} else { steerforce = new PVector(0, 0); }

}

}

return steerforce;

// return the vector

processing compendium

51


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.