forked from jakogut/tinyflock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboid.c
145 lines (116 loc) · 2.97 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
#include "config.h"
#include "boid.h"
boid* create_boid(SDL_Surface* image, int loc_x, int loc_y, int vel_x, int vel_y)
{
boid* b = malloc(sizeof *b);
b->sprite = image;
b->location = create_vector(loc_x, loc_y, 0);
b->velocity = create_vector(vel_x, vel_y, 0);
b->acceleration = create_vector(0, 0, 0);
return b;
}
void destroy_boid(boid* b)
{
destroy_vector(b->location);
destroy_vector(b->velocity);
destroy_vector(b->acceleration);
free(b);
}
// Avoid crowding flockmates
vector* flock_separate (boid** flock, boid* b)
{
vector* v = create_vector(0, 0, 0);
int neighborhood_population = 0;
int i;
for(i = 0; i < NUM_BOIDS; i++)
{
if(flock[i] != b)
{
float distance = vector_distance(flock[i]->location, b->location);
if((distance < NEIGHBORHOOD_RADIUS) && (distance < MIN_BOID_SEPARATION))
{
vector* loc = copy_vector(b->location);
vector_sub(loc, flock[i]->location);
vector_normalize(loc);
vector_div_scalar(loc, distance);
vector_add(v, loc);
destroy_vector(loc);
neighborhood_population++;
}
}
}
if(neighborhood_population > 0) vector_div_scalar(v, neighborhood_population);
else vector_init(v, 0);
if(vector_magnitude(v) > 0)
{
vector_normalize(v);
vector_mul_scalar(v, MAX_BOID_VELOCITY);
vector_sub(v, b->velocity);
vector_mul_scalar(v, MAX_BOID_STEERING_FORCE);
}
return v;
}
// Head in the same average direction as flockmates
vector* flock_align(boid** flock, boid* b)
{
vector* v = create_vector(0, 0, 0);
int neighborhood_population = 0;
int i;
for(i = 0; i < NUM_BOIDS; i++)
{
if(flock[i] != b)
{
float distance = vector_distance(flock[i]->location, b->location);
if(distance < NEIGHBORHOOD_RADIUS)
{
vector_add(v, flock[i]->velocity);
neighborhood_population++;
}
}
}
if(neighborhood_population > 0) vector_div_scalar(v, neighborhood_population);
else vector_init(v, 0);
if(vector_magnitude(v) > 0)
{
vector_normalize(v);
vector_mul_scalar(v, MAX_BOID_VELOCITY);
vector_sub(v, b->velocity);
vector_mul_scalar(v, MAX_BOID_STEERING_FORCE);
}
return v;
}
// Head towards the average position of flockmates
vector* flock_cohere(boid** flock, boid* b)
{
vector* v = create_vector(0, 0, 0);
int neighborhood_population = 0;
int i;
for(i = 0; i < NUM_BOIDS; i++)
{
if(flock[i] != b)
{
float distance = vector_distance(flock[i]->location, b->location);
if(distance < NEIGHBORHOOD_RADIUS)
{
vector_add(v, flock[i]->location);
neighborhood_population++;
}
}
}
if(neighborhood_population > 0) vector_div_scalar(v, neighborhood_population);
else vector_init(v, 0);
if(vector_magnitude(v) > 0)
{
vector_normalize(v);
vector_mul_scalar(v, MAX_BOID_VELOCITY);
vector_sub(v, b->velocity);
vector_mul_scalar(v, MAX_BOID_STEERING_FORCE);
}
return v;
}
void flock_limit_velocity(boid** flock, int num_boids, float max_velocity)
{
int i;
for(i = 0; i < num_boids; i++)
vector_clamp_scalar(flock[i]->velocity, (0 - max_velocity), max_velocity);
}