1 : |
lamonts |
2125 |
/*! \file flock.diderot
|
2 : |
|
|
*
|
3 : |
|
|
* \author Lamont Samuels
|
4 : |
|
|
*
|
5 : |
|
|
* This example is an implementation of Chris Reynolds Boids algorithm. It represents an example of using Spatial communication
|
6 : |
|
|
*/
|
7 : |
|
|
/*
|
8 : |
|
|
* COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
|
9 : |
|
|
* All rights reserved.
|
10 : |
|
|
*/
|
11 : |
|
|
real{} initVelocities = load("data/velocities.nrrd");
|
12 : |
|
|
int numBoids = length(initVelocities)/2;
|
13 : |
|
|
int numBoids = length(initVelocities)/2;
|
14 : |
|
|
int strandSize = 2;
|
15 : |
|
|
real query_radius = 50;
|
16 : |
|
|
int MAX_SPEED = 2;
|
17 : |
|
|
real MAX_FORCE = 0.05;
|
18 : |
|
|
int stepsMax = 10000;
|
19 : |
|
|
real boundsFactor = 10.0;
|
20 : |
|
|
input int width;
|
21 : |
|
|
input int height;
|
22 : |
|
|
int NORTH = 0;
|
23 : |
|
|
int SOUTH = 1;
|
24 : |
|
|
int WEST = 2;
|
25 : |
|
|
int EAST = 3;
|
26 : |
|
|
|
27 : |
|
|
strand Boid(int i,real vx, real vy) {
|
28 : |
|
|
vec2 pos = [real(width)/2.0,real(height)/2.0];
|
29 : |
|
|
vec2 vel = [vx,vy];
|
30 : |
|
|
int steps = 0;
|
31 : |
|
|
int id = i;
|
32 : |
|
|
output vec4 boidInfo = [pos[0],pos[1],vel[0],vel[1]];
|
33 : |
|
|
update {
|
34 : |
|
|
vec2 cohereVec = [0.0,0.0];
|
35 : |
|
|
vec2 alignVec = [0.0,0.0];
|
36 : |
|
|
vec2 seperateVec = [0.0,0.0];
|
37 : |
|
|
vec4 wrapDimensions = [-boundsFactor,
|
38 : |
|
|
height + boundsFactor,
|
39 : |
|
|
-boundsFactor,
|
40 : |
|
|
width + boundsFactor];
|
41 : |
|
|
int count = 0;
|
42 : |
|
|
|
43 : |
|
|
foreach(Boid neighbor in sphere(query_radius)){
|
44 : |
|
|
real d = dist(pos,neighbor.pos);
|
45 : |
|
|
|
46 : |
|
|
if(d > 0) {
|
47 : |
|
|
cohereVec += neighbor.pos;
|
48 : |
|
|
alignVec += neighbor.vel;
|
49 : |
|
|
seperateVec = seperateVec + (normalize(pos-neighbor.pos)/d);
|
50 : |
|
|
count+=1;
|
51 : |
|
|
}
|
52 : |
|
|
}
|
53 : |
|
|
|
54 : |
|
|
if(count > 0) {
|
55 : |
|
|
cohereVec /= real(count);
|
56 : |
|
|
alignVec /= real(count);
|
57 : |
|
|
seperateVec /= real(count);
|
58 : |
|
|
}
|
59 : |
|
|
|
60 : |
|
|
real isZero = alignVec • alignVec;
|
61 : |
|
|
|
62 : |
|
|
// limit the force
|
63 : |
|
|
if ( isZero != 0 && |alignVec| > MAX_FORCE) {
|
64 : |
|
|
alignVec = normalize(alignVec);
|
65 : |
|
|
alignVec *= MAX_FORCE;
|
66 : |
|
|
}
|
67 : |
|
|
|
68 : |
|
|
|
69 : |
|
|
vec2 diff = cohereVec - pos;
|
70 : |
|
|
isZero = cohereVec • cohereVec;
|
71 : |
|
|
|
72 : |
|
|
if(|diff| > 0 && isZero != 0){
|
73 : |
|
|
|
74 : |
|
|
cohereVec = normalize(cohereVec);
|
75 : |
|
|
|
76 : |
|
|
if(|diff| < 100.0) {
|
77 : |
|
|
cohereVec *= (MAX_SPEED*(|diff|/100.0));
|
78 : |
|
|
}else {
|
79 : |
|
|
cohereVec *= MAX_SPEED;
|
80 : |
|
|
}
|
81 : |
|
|
|
82 : |
|
|
cohereVec -= vel;
|
83 : |
|
|
|
84 : |
|
|
// limit the force
|
85 : |
|
|
if (|cohereVec| > MAX_FORCE) {
|
86 : |
|
|
cohereVec = normalize(cohereVec);
|
87 : |
|
|
cohereVec *= MAX_FORCE;
|
88 : |
|
|
}
|
89 : |
|
|
}
|
90 : |
|
|
else{
|
91 : |
|
|
cohereVec = [0.0,0.0];
|
92 : |
|
|
}
|
93 : |
|
|
|
94 : |
|
|
|
95 : |
|
|
vec2 acceleration = cohereVec + alignVec + seperateVec;
|
96 : |
|
|
vel += acceleration;
|
97 : |
|
|
|
98 : |
|
|
if (|vel| > MAX_SPEED){
|
99 : |
|
|
vel = normalize(cohereVec);
|
100 : |
|
|
vel *= MAX_SPEED;
|
101 : |
|
|
}
|
102 : |
|
|
|
103 : |
|
|
pos += vel;
|
104 : |
|
|
|
105 : |
|
|
/* Wrapping Code, if a boid goes outside the window */
|
106 : |
|
|
real posX = wrapDimensions[EAST] if pos[0] < wrapDimensions[WEST]
|
107 : |
|
|
else wrapDimensions[WEST] if pos[0] > wrapDimensions[EAST]
|
108 : |
|
|
else pos[0];
|
109 : |
|
|
|
110 : |
|
|
real posY = wrapDimensions[SOUTH] if pos[1] < wrapDimensions[NORTH]
|
111 : |
|
|
else wrapDimensions[NORTH] if pos[1] > wrapDimensions[SOUTH]
|
112 : |
|
|
else pos[1];
|
113 : |
|
|
|
114 : |
|
|
pos = [posX,posY];
|
115 : |
|
|
|
116 : |
|
|
boidInfo = [pos[0],pos[1], vel[0], vel[1]];
|
117 : |
|
|
}
|
118 : |
|
|
}
|
119 : |
|
|
|
120 : |
|
|
initially {Boid(i,initVelocities{i*2},initVelocities{i*2+1}) | i in 0 .. numBoids-1 };
|
121 : |
|
|
|