-
Notifications
You must be signed in to change notification settings - Fork 0
/
boid.c
192 lines (145 loc) · 5.01 KB
/
boid.c
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
#include "inc.h"
/* BOID FUNCTIONS
ALL FUNCTIONS IN THIS FILE SHOULD REENTRANT
*/
int boidInsert(boidContainer * container, boid * insert){
//Check for need to extend container
if(container->size == container->alloc){
container->alloc += CONTAINEREXTEND;
if((container->boidArr = (boid *) realloc(container->boidArr, container->alloc * sizeof(boid))) == NULL){
fprintf(stderr, "Error extending boid container, quitting...\n");
exit(1);
}
}
container->boidArr[container->size] = *insert;
++container->size;
return 1;
}
int boidRemove(boidContainer * container, int * list){
return 0;
}
//Calculate the vector to move towards the exit
directionVector moveToExit(const goalContainer * const goals, const boidContainer * const boidlist, int index){
directionVector exitVector;
int i;
int * res;
exitVector.x = 0;
exitVector.y = 0;
for(i = 0; i < goals->size; i++){
res = goals->pos[i];
//We want the closest goal
if((abs(res[0] - boidlist->boidArr[index].xpos) + abs(res[1] - boidlist->boidArr[index].ypos)) < (abs(exitVector.x) + abs(exitVector.y)) || i == 0){
exitVector.x = res[0] - boidlist->boidArr[index].xpos;
exitVector.y = res[1] - boidlist->boidArr[index].ypos;
}
}
return exitVector;
}
//Try to keep boids away from each other so they don't colllide
directionVector aversion(const boidContainer * const boidlist, int index){
directionVector aversionVec;
int i;
aversionVec.x = 0;
aversionVec.y = 0;
for(i = 0; i < boidlist->size; i++){
if(i == index){
continue;
}
if((abs(boidlist->boidArr[index].xpos - boidlist->boidArr[i].xpos) + abs(boidlist->boidArr[index].ypos - boidlist->boidArr[i].ypos)) < SEPARATIONWIDTH){
aversionVec.x += boidlist->boidArr[i].xpos - boidlist->boidArr[index].xpos;
aversionVec.y += boidlist->boidArr[i].ypos - boidlist->boidArr[index].ypos;
}
}
return aversionVec;
}
//Try to move towards the center of the group
directionVector alignment(const boidContainer * const boidlist, int index){
directionVector alignVec;
int i, count = 0;
alignVec.x = 0;
alignVec.y = 0;
for(i = 0; i < boidlist->size; i++){
if(i == index){
continue;
}
if((abs(boidlist->boidArr[index].xpos - boidlist->boidArr[i].xpos) + abs(boidlist->boidArr[index].ypos - boidlist->boidArr[i].ypos)) < ALIGNWIDTH){
//If the boid we are looking at is within a certain distance then try to align with it
alignVec.x += boidlist->boidArr[index].velocity.x;
alignVec.y += boidlist->boidArr[index].velocity.y;
count ++;
}
}
//Take the average
if(count > 0){
alignVec.x = alignVec.x/count;
alignVec.y = alignVec.y/count;
}
return alignVec;
}
//Try to steer towards the middle of the group
directionVector cohesion(const boidContainer * const boidlist, int index){
directionVector cohesVec;
int i, count = 0;
cohesVec.x = 0;
cohesVec.y = 0;
for(i = 0; i < boidlist->size; i++){
if(i == index){
continue;
}
if((abs(boidlist->boidArr[index].xpos - boidlist->boidArr[i].xpos) + abs(boidlist->boidArr[index].ypos - boidlist->boidArr[i].ypos)) < COHEREWIDTH){
//If the boid we are looking at is within a certain distance then try to align with it
cohesVec.x += boidlist->boidArr[index].xpos;
cohesVec.y += boidlist->boidArr[index].ypos;
count++;
}
}
//Take the average location of boids
if(count > 0){
cohesVec.x = cohesVec.x/count - boidlist->boidArr[index].xpos;
cohesVec.y = cohesVec.y/count - boidlist->boidArr[index].ypos;
}
return cohesVec;
}
//Add vector quantities with a weight
inline void addVector(directionVector * addTo, const directionVector * const source, int weight){
addTo->x += source->x * weight;
addTo->y += source->y * weight;
}
//Limit boid movement to one block in one direction
inline void limitVec(directionVector * limitWhat){
if(limitWhat->x == 0 && limitWhat->y == 0){
//Do nothing
}else if(abs(limitWhat->x) > abs(limitWhat->y)){
limitWhat->y = 0;
limitWhat->x = limitWhat->x / abs(limitWhat->x);
}else{
limitWhat->x = 0;
limitWhat->y = limitWhat->y / abs(limitWhat->y);
}
}
void moveBoid(const goalContainer * const goals, boidContainer * boidlist, int index){
directionVector exitVec, cohVec, alignVec, averVec, acceleration;
int i;
acceleration.x = 0;
acceleration.y = 0;
//Could possibly make these all execute in parallel as well
exitVec = moveToExit(goals, boidlist, index);
cohVec = cohesion(boidlist, index);
alignVec = alignment(boidlist, index);
averVec = aversion(boidlist, index);
//Add up vectors with weights
addVector(&acceleration, &exitVec, 3);
addVector(&acceleration, &cohVec, 1);
addVector(&acceleration, &alignVec, 1);
addVector(&acceleration, &averVec, 2);
//Limit the vector to required speed
limitVec(&acceleration);
boidlist->boidArr[index].velocity = acceleration;
boidlist->boidArr[index].xpos += acceleration.x;
boidlist->boidArr[index].ypos += acceleration.y;
for(i = 0; i < goals->size; i++){
if(boidlist->boidArr[index].xpos == goals->pos[i][0] && boidlist->boidArr[index].ypos == goals->pos[i][1]){
boidlist->boidArr[index].active = 0;
}
}
}