-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
346 lines (276 loc) · 9.38 KB
/
main.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <SDL2/SDL.h>
#include <math.h>
#include <time.h>
#include "boid.c"
const int SCREEN_WIDTH = 480 * 2;
const int SCREEN_HEIGHT = 360 * 2;
const double SPRITE_SIZE = 8;
const int OBSTACLE_AMOUNT = 0;
const int AMOUNT = 1600;
const double FLOCK_RADIUS = 60;
const double BOID_SPEED = .15;
const double SEPERATION_STRENGTH = 2.02;
const double ALIGNMENT_STRENGTH = 1;
const double COHESION_STRENGTH = 2;
const double AVOID = 18;
const double TURN_RESISTANCE = 60;
void Tick(struct Boid *boids, struct Vec2 *obstacles, double delta);
void RefreshWindow(SDL_Renderer *renderer, const struct Boid *boids, const struct Vec2 *obstacles);
struct Boid* CreateBoids();
struct Vec2* CreateObstacles(int amount);
int main(void)
{
printf("initializing boids\n");
struct Boid *boids = CreateBoids();
printf("boids initialized\n");
struct Vec2 *obstacles = CreateObstacles(OBSTACLE_AMOUNT);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
printf( "SDL initialized\n" );
if(SDL_CreateWindowAndRenderer( SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer ) < 0)
{
printf( "Window/Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
}
printf( "Window & renderer created\n" );
SDL_Event e;
int running = 0;
int paused = -1;
double timeOfLastFrame = 0;
while(running == 0){
if(paused == -1)
{
RefreshWindow(renderer, boids, obstacles);
double deltaTime = (SDL_GetTicks() - timeOfLastFrame);
timeOfLastFrame = SDL_GetTicks();
Tick(boids, obstacles, deltaTime);
SDL_Delay(1);
}
while( SDL_PollEvent(&e) > 0 )
{
//dont work
if(e.type == SDLK_SPACE)
{
printf("Pause registered. pause/unpausing");
paused = -paused;
}
if( e.type == SDL_QUIT )
{
printf("QUIT registered. exiting now\n");
running = 1;
}
}
}
free(boids);
free(obstacles);
return 0;
}
struct Boid* CreateBoids()
{
struct Boid *boids = malloc(sizeof(struct Boid) * AMOUNT);
if(boids == NULL)
printf("ERROR: Failed to allocate memory for boids");
srand(653);
printf("USING SEED: %i\n", 2);
for(int i = 0; i < AMOUNT; i++)
{
boids[i].pos.x = rand() % SCREEN_WIDTH;
boids[i].pos.y = rand() % SCREEN_HEIGHT;
boids[i].dir.x = -1 + ((float)rand() / (float)RAND_MAX) * 2;
boids[i].dir.y = -1 + ((float)rand() / (float)RAND_MAX) * 2;
//boids[i].depth = 0.5 + (float)rand() / (float)RAND_MAX;
boids[i].depth = 0.5 + (double)(i + 1) / (double)(AMOUNT);
}
return boids;
}
struct Vec2* CreateObstacles(int amount)
{
struct Vec2* obstacles = malloc(amount * sizeof(struct Vec2));
for(int i = 0; i < amount; i++)
{
if(i % 2 == 0)
obstacles[i].x = 2.0;
else
obstacles[i].x = SCREEN_WIDTH - 2.0;
obstacles[i].y = (i) * SCREEN_HEIGHT / (double)(amount-1);
}
return obstacles;
}
void RefreshWindow(SDL_Renderer *renderer, const struct Boid *boids, const struct Vec2 *obstacles)
{
SDL_SetRenderDrawColor(renderer, 26, 26, 26, 255);
SDL_RenderClear(renderer);
for(int i = 0; i < AMOUNT; i++)
{
const double p = boids[i].depth * 70;
//SDL_SetRenderDrawColor(renderer, 130 + p, 186, 38, 25);
SDL_SetRenderDrawColor(renderer, p+20, p+50, p, 255);
const double BOID_SIZE = (SPRITE_SIZE * boids[i].depth + BOID_SIZE) / 2.0;
SDL_Rect rect;
rect.x = boids[i].pos.x - BOID_SIZE / 2.0;
rect.y = /*SCREEN_HEIGHT - */(boids[i].pos.y - BOID_SIZE / 2.0);
rect.h = BOID_SIZE;
rect.w = BOID_SIZE;
SDL_RenderFillRect(renderer, &rect);
const double FACE_SIZE = BOID_SIZE / 2.0;
const double FACE_DISTANCE = 6;
SDL_Rect face;
face.x = boids[i].pos.x + FACE_DISTANCE * boids[i].dir.x - FACE_SIZE / 2.0;
face.y = /*SCREEN_HEIGHT - */(boids[i].pos.y + FACE_DISTANCE * boids[i].dir.y - FACE_SIZE / 2.0);
face.h = FACE_SIZE;
face.w = FACE_SIZE;
SDL_RenderFillRect(renderer, &face);
}
SDL_SetRenderDrawColor(renderer, 200, 86, 38, 255);
for(int i = 0; i < OBSTACLE_AMOUNT; i++)
{
const double FACE_SIZE = SPRITE_SIZE * 2.0;
SDL_Rect rect;
rect.x = obstacles[i].x - FACE_SIZE / 2.0;
rect.y = /*SCREEN_HEIGHT - */(obstacles[i].y - FACE_SIZE / 2.0);
rect.h = FACE_SIZE;
rect.w = FACE_SIZE;
SDL_RenderFillRect(renderer, &rect);
}
SDL_RenderPresent(renderer);
}
double Loop(double x, const double min, const double max)
{
// shit gross
double y = x;
const double range = max - min;
if(y > max)
{
while (y>max){
y -= range;
}
}
if(y < min)
{
while ( y < min )
{
y += range;
}
}
return y;
}
double Lerp(const double a, const double b, const double i)
{
return a * (1 - i) + b * i;
}
void GetLocalBoids(struct Boid *boids, const int index, struct Flock *flock)
{
struct Boid *locals[AMOUNT];
int j = 0;
for (int i = 0; i < AMOUNT; i++)
{
if(i == index || fabs(boids[i].depth - boids[index].depth) >= .2)
continue;
struct Vec2 distance;
distance.x = boids[i].pos.x - boids[index].pos.x;
distance.y = boids[i].pos.y - boids[index].pos.y;
double distanceSqr = LengthSquared(&distance);
if(distanceSqr < FLOCK_RADIUS * FLOCK_RADIUS)
{
Normalize(&distance);
Normalize(&boids[i].dir);
if( Dot( &boids[i].dir, &distance ) > -.5 )
{
locals[j] = &boids[i];
j++;
}
}
}
flock->boids = malloc(j * sizeof(struct Boid**));
if(flock->boids == NULL)
printf("failed to allocate memory for local flock processing.");
for(int i = 0; i < j; i++)
{
flock->boids[i] = locals[i];
}
flock->size= j;
}
void GetNearbyObstacles(struct Vec2 *obstacles, const struct Vec2 pos, struct Vec2 **nearby, int *count)
{
struct Vec2 *temp = malloc(sizeof(struct Vec2) * OBSTACLE_AMOUNT);
int j = 0;
for (int i = 0; i< OBSTACLE_AMOUNT; i++)
{
struct Vec2 obsDis;
VecOperate(&obsDis, &obstacles[i], VEC_CPY);
VecOperate(&obsDis, &pos, VEC_SUB);
if(LengthSquared(&obsDis) < FLOCK_RADIUS * FLOCK_RADIUS)
{
temp[j] = obstacles[i];
j++;
}
}
*nearby = malloc(sizeof(struct Vec2*) * j);
for(int i = 0; i < j; i++)
{
*nearby[i] = temp[i];
}
*count = j;
free(temp);
}
void Tick(struct Boid *boids, struct Vec2 *obstacles, double delta)
{
for(int i = 0; i < AMOUNT; i++)
{
struct Boid *boid = &(boids[i]);
struct Vec2 steerDir;
struct Flock flock;
GetLocalBoids(boids, i, &flock);
if(flock.size > 0)
{
// average pos of all flockmates
struct Vec2 cohesionDir;
AvgFlockPos(flock, &cohesionDir);
VecOperate(&cohesionDir, &boid->pos, VEC_SUB);
SetLength(&cohesionDir, COHESION_STRENGTH);
//average directeion from current to flockmate
struct Vec2 seperationDir;
AvgFlockDir(flock, boid, &seperationDir);
Invert(&seperationDir);
SetLength(&seperationDir, SEPERATION_STRENGTH);
// avergae angle of all flockmates
struct Vec2 alignmentDir;
AvgFlockAngle(flock, &alignmentDir);
SetLength(&alignmentDir, ALIGNMENT_STRENGTH);
struct Vec2 newDir = { .x = 0, .y = 0 };
VecOperate(&newDir, &cohesionDir, VEC_ADD);
VecOperate(&newDir, &seperationDir, VEC_ADD);
VecOperate(&newDir, &alignmentDir, VEC_ADD);
Normalize(&newDir);
if(!isnan(newDir.x) && !isnan(newDir.y))
steerDir = newDir;
}
struct Vec2* nearbyObstacles;
int obstaclesCount = 0;
GetNearbyObstacles(obstacles, boid->pos, &nearbyObstacles, &obstaclesCount);
if(obstaclesCount > 0)
{
struct Vec2 dirAway = { .x=0, .y=0 };
for(int i = 0; i < obstaclesCount; i++)
{
dirAway.x += boid->pos.x - nearbyObstacles[i].x;
dirAway.y += boid->pos.y - nearbyObstacles[i].y;
}
double a = AVOID / Length(&steerDir);
steerDir.x = (a * steerDir.x + dirAway.x) / (a + 1);
steerDir.y = (a * steerDir.y + dirAway.y) / (a + 1);
}
free(flock.boids);
free(nearbyObstacles);
boid->dir.x = Lerp(boid->dir.x, steerDir.x, fmin(delta / TURN_RESISTANCE,1));
boid->dir.y = Lerp(boid->dir.y, steerDir.y, fmin(delta / TURN_RESISTANCE,1));
SetLength(&boid->dir, BOID_SPEED * boid->depth * delta);
TranslateVector(&boid->pos, &boid->dir);
boid->pos.x = Loop(boid->pos.x, 0, SCREEN_WIDTH );
boid->pos.y = Loop(boid->pos.y, 0, SCREEN_HEIGHT);
Normalize(&boid->dir);
}
}