«BoidRoids Class» by xffff

on 21 Dec'12 23:37 in classsimulationswarmboidsport

Port of the "BoidRoids.js" file contained in the Max distribution examples. Used with permission of Cycling '74

Thought this might be a good example to put up here on writing classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
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
/*
SuperCollider port of BoidRoids.js from the Max examples
Used with permission of Cycling '74 (thanks!)

Mike Murphy 2012
*/

// mode \average = "average x/y/vx/vy list"
// mode \normal = "series of x/y/vx/vy lists"

BoidRoids {
	// global varables and code
	var
	agents,
	<avgvelocity_x = 0,
	<avgvelocity_y = 0,
	<centroid_x = 0,
	<centroid_y = 0,
	<>myagentcount = 0,
	<myseparation = 0.03,
	<myalignment = 0.05,
	<mycoherence = 0.02,
	<myinertia = 0.5,
	<myfriction = 0.5,
	<mysepthresh = 0.1,
	<mymaxvel = 0.05,
	<mygravity = 0,
	<mygravpoint_x = 0.5,
	<mygravpoint_y = 0.5,
	<>mode = \normal,
	x = 0,
	y = 0,
	vx = 0,
	vy = 0;

	*new{ |numboids|
		^super.new.initagents(numboids);
	}

	initagents{ |n|
		// clip agentcount to range 1.-1000.
		myagentcount = n.clip(1,1000);
		agents = Array.new(n);

		for(0,myagentcount-1,{ |i|
			// start with random position/velocity
			x = rrand(0.0,1.0);
			y = rrand(0.0,1.0);
			vx = (rrand(0.0,1.0)-0.5)*0.1;
			vy = (rrand(0.0,1.0)-0.5)*0.1;
			// create a new agent... agents[i][x,y,vx,vy]
			agents.add([x,y,vx,vy]);
			}
		);
	}

	// task function
	getBoids {
		//var i;
		var cx=0;
		var cy=0;
		var cvx=0;
		var cvy=0;

		for (0,myagentcount-1,{ |i|
			this.agent_tick(i);

			// calculate current frame's average position/velocity
			cx = cx + agents[i][0];
			cy = cy + agents[i][1];
			cvx = cvx + agents[i][2];
			cvy = cvy + agents[i][3];
			}
		);

		centroid_x = cx/myagentcount;
		centroid_y = cy/myagentcount;
		avgvelocity_x = cvx/myagentcount;
		avgvelocity_y = cvy/myagentcount;

		if(mode==\normal,{
			^agents;
			},{
				if(mode==\average,{
					^[centroid_x,centroid_y,avgvelocity_x,avgvelocity_y];
					},{
						postln("BoidRoids: mode can be either \average, or \normal");
					}
				);
			}
		);

	}

	// one iteration of the simulation for a given agent
	// we pass this the index of an agent in the agents array
	// it has the attributes agent[x,y,vx,vy]...
	agent_tick { | agent |
		var px,py;

		// save current velocity for inertia calc
		//px = vx;
		//py = vy;
		px = agents[agent][2];
		py = agents[agent][3];

		// apply rules
		this.separate(agent);
		this.align(agent);
		this.cohere(agent);
		this.gravitate(agent);

		// inertia
		//vx = (px*myinertia) + (vx*(1-myinertia));
		//vy = (py*myinertia) + (vy*(1-myinertia));
		agents[agent][2] = (px*myinertia) + (agents[agent][2]*(1-myinertia));
		agents[agent][3] = (px*myinertia) + (agents[agent][3]*(1-myinertia));

		// velocity limit
		agents[agent][2] = agents[agent][2].clip(mymaxvel.neg,mymaxvel);
		agents[agent][3] = agents[agent][3].clip(mymaxvel.neg,mymaxvel);

		// update position based on velocity and friction
		//x = x + (vx*(1-myfriction));
		//y = y + (vy*(1-myfriction));
		agents[agent][0] = agents[agent][0] + (agents[agent][2]*(1-myfriction));
		agents[agent][1] = agents[agent][1] + (agents[agent][3]*(1-myfriction));

		this.twrap(agent); // torus space
	}

	// rules (a is the index of an agent in the agents array)
	separate { |a|
		var dx,dy,proxscale,mag;

		// run from positions of neighbors
		for(0,myagentcount-1,{ |i|
			if(a != i,{
				dx = agents[i][0] - agents[a][0];
				dy = agents[i][1] - agents[a][1];

				//torus space
				if(dx>0.5,{
					dx = dx - 1;
					},{
						if(dx<0.5.neg,{
							dx = dx + 1;
						});
				});
				if(dy>0.5,{
					dy = dy - 1;
					},{
						if(dy<0.5.neg,{
							dy = dy + 1;
						});
				});

				if((abs(dx)>1e-4)&&(abs(dy)>1e-4),{
					mag = (dx*dx)+(dy*dy); // cheap mag, no sqrt
					},{
						mag = 0.01;
				});

				if(mag<mysepthresh,{
					if (mag<1e-4,{
						proxscale = 8;
						},{
							proxscale = (mysepthresh /
								(mysepthresh-(mysepthresh-mag))).clip(0,8);
					});

					agents[a][2] = agents[a][2] - (dx*myseparation*proxscale);
					agents[a][3] = agents[a][3] - (dy*myseparation*proxscale);
					}
				);
				}
			);
			}
		);
	}

	align { |a|
		var dvx,dvy;

		// conform to velocities towards average velocity
		dvx = avgvelocity_x - agents[a][2];
		dvy = avgvelocity_y - agents[a][3];

		agents[a][2] = agents[a][2] + (dvx*myalignment);
		agents[a][3] = agents[a][3] + (dvy*myalignment);
	}

	cohere { |a|
		var dx,dy;

		// move towards center of mass
		dx = centroid_x - agents[a][0];
		dy = centroid_y - agents[a][1];

		agents[a][2] = agents[a][2] + (dx*mycoherence);
		agents[a][3] = agents[a][3] + (dy*mycoherence);
	}

	gravitate { |a|
		var dx,dy;

		// move towards center
		dx = mygravpoint_x - agents[a][0];
		dy = mygravpoint_y - agents[a][1];

		agents[a][2] = agents[a][2] + (dx*mygravity);
		agents[a][3] = agents[a][3] + (dy*mygravity);
	}

	// utility functions
	twrap { |a|
		if(agents[a][0]<0,{
			agents[a][0] = agents[a][0] + 1;
			},{
				if(agents[a][0]>1,{
					agents[a][0] = agents[a][0] - 1;
				});
		});

		if(agents[a][1]<0,{
			agents[a][1] = agents[a][1] + 1;
			},{
				if(agents[a][1]>1,{
					agents[a][1] = agents[a][1] - 1;
				});
		});
	}

	// not in use atm
	bounce { |a|
		if ((agents[a][0]<0)||(agents[a][0]>1), {
			agents[a][2] = agents[a][2].neg;
		});
		if ((agents[a][1]<0)||(agents[a][1]>1), {
			agents[a][3] = agents[a][3].neg;
		});

	}

	// set var vunctions
	separation { |v|
		myseparation = v.clip(0,1)*0.1;
	}

	alignment { |v|
		myalignment = v.clip(0,1)*0.1;
	}

	coherence { |v|
		mycoherence = v.clip(0,1)*0.1;
	}

	friction { |v|
		myfriction = v.clip(0,1);
	}

	inertia { |v|
		myinertia = v.clip(0,1);
	}

	septhresh { |v|
		mysepthresh = v.clip(0,0.5);
	}

	maxvel { |v|
		mymaxvel = v.clip(0,1)*0.1;
	}

	gravity { |v|
		mygravity = v.clip(-1,1)*0.1;
	}

	gravpoint { |x,y|
		mygravpoint_x = x.clip(0,1);
		mygravpoint_y = y.clip(0,1);
	}
}
descendants
«Re: BoidRoids Class» by foxparse (private)
full graph
raw 5929 chars (focus & ctrl+a+c to copy)
reception
comments