-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
247 lines (203 loc) · 8.56 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
#include <stdio.h>
#include "raylib.h"
#include "raymath.h"
#define MAX_NUM_ENTITIES 500
typedef enum EntityState {
ENTITY_STATE_INACTIVE,
ENTITY_STATE_ACTIVE,
} EntityState;
typedef struct Entity {
Vector2 position;
Vector2 velocity;
Vector2 acceleration;
Vector2 direction;
Vector2 projectionForward;
EntityState state;
float radius;
} Entity;
typedef struct Flock {
Entity entities[MAX_NUM_ENTITIES];
Vector2 seekTarget;
int shouldSeekTarget;
float maxSpeed;
float maxAlignForce;
float maxCohesionForce;
float maxSeparationForce;
float maxSeekForce;
float awarenessRadius;
float separationRadius;
int renderHelpers;
} Flock;
void InitFlock(Flock *flock, Vector2 center, EntityState state) {
for (int i = 0; i < MAX_NUM_ENTITIES; i++) {
Entity *entity = &flock->entities[i];
entity->radius = 5;
entity->position.x = center.x + GetRandomValue(-entity->radius * 10, entity->radius * 10);
entity->position.y = center.y + GetRandomValue(-entity->radius * 10, entity->radius * 10);
entity->acceleration.x = 0;
entity->acceleration.y = 0;
entity->direction.x = 0;
entity->direction.y = 0;
entity->state = state;
entity->velocity.x = GetRandomValue(-flock->maxSpeed, flock->maxSpeed);
entity->velocity.y = GetRandomValue(-flock->maxSpeed, flock->maxSpeed);
}
}
void UpdateFlock(Flock *flock) {
if (IsKeyPressed(KEY_R)) {
Vector2 startPoint = { GetScreenWidth() / 2, GetScreenHeight() / 2 };
InitFlock(flock, startPoint, ENTITY_STATE_ACTIVE);
}
if (IsKeyPressed(KEY_H)) {
flock->renderHelpers = flock->renderHelpers ? 0 : 1;
}
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
flock->seekTarget = GetMousePosition();
flock->shouldSeekTarget = 1;
} else {
flock->shouldSeekTarget = 0;
}
Vector2 align = {0, 0};
Vector2 cohesion = {0, 0};
Vector2 separation = {0, 0};
Vector2 seek = {0, 0};
Vector2 wander = {0, 0};
Vector2 screenCenter = { GetScreenWidth() /2, GetScreenHeight() / 2 };
for (int i = 0; i < MAX_NUM_ENTITIES; i++) {
Entity *entity = &flock->entities[i];
if (entity->state == ENTITY_STATE_INACTIVE) continue;
int awarenessNeighboursCount = 0;
int toCloseNeighboursCount = 0;
for (int j = 0; j < MAX_NUM_ENTITIES; j++) {
if (j == i || flock->entities[j].state == ENTITY_STATE_INACTIVE) continue;
Entity *other = &flock->entities[j];
float distanceToOther = Vector2Distance(entity->position, other->position);
if (distanceToOther > 0 && distanceToOther <= flock->awarenessRadius) {
align = Vector2Add(align, other->velocity);
cohesion = Vector2Add(cohesion, other->position);
awarenessNeighboursCount++;
}
if (distanceToOther >0 && distanceToOther <= flock->separationRadius) {
Vector2 diff = Vector2Subtract(entity->position, other->position);
diff = Vector2Normalize(diff);
diff.x /= distanceToOther;
diff.y /= distanceToOther;
separation = Vector2Add(separation, diff);
toCloseNeighboursCount++;
}
}
if (awarenessNeighboursCount > 0) {
align.x /= awarenessNeighboursCount;
align.y /= awarenessNeighboursCount;
align = Vector2ClampValue(align, -flock->maxSpeed, flock->maxSpeed);
align = Vector2Subtract(align, entity->velocity);
align = Vector2Normalize(align);
align = Vector2Scale(align, flock->maxAlignForce);
cohesion.x /= awarenessNeighboursCount;
cohesion.y /= awarenessNeighboursCount;
cohesion = Vector2Subtract(cohesion, entity->position);
cohesion = Vector2ClampValue(cohesion, -flock->maxSpeed, flock->maxSpeed);
cohesion = Vector2Subtract(cohesion, entity->velocity);
cohesion = Vector2Normalize(cohesion);
cohesion = Vector2Scale(cohesion, flock->maxCohesionForce);
}
if (toCloseNeighboursCount > 0) {
separation.x /= toCloseNeighboursCount;
separation.y /= toCloseNeighboursCount;
separation = Vector2Normalize(separation);
separation = Vector2Scale(separation, flock->maxSpeed);
separation = Vector2Subtract(separation, entity->velocity);
separation = Vector2Normalize(separation);
separation = Vector2Scale(separation, flock->maxSeparationForce);
}
if (flock->shouldSeekTarget) {
float distanceToMouse = Vector2Distance(entity->position, flock->seekTarget);
if (distanceToMouse > entity->radius) {
seek = Vector2Subtract(flock->seekTarget, entity->position);
seek = Vector2Normalize(seek);
seek = Vector2Scale(seek, flock->maxSpeed);
seek = Vector2Subtract(seek, entity->velocity);
seek = Vector2Normalize(seek);
seek = Vector2Scale(seek, flock->maxSeekForce);
}
} else {
// Wander
}
entity->acceleration.x += align.x;
entity->acceleration.y += align.y;
entity->acceleration.x += cohesion.x;
entity->acceleration.y += cohesion.y;
entity->acceleration.x += separation.x;
entity->acceleration.y += separation.y;
entity->acceleration.x += seek.x;
entity->acceleration.y += seek.y;
entity->acceleration.x += wander.x;
entity->acceleration.y += wander.y;
if (entity->position.x + entity->radius >= GetScreenWidth() || entity->position.x - entity->radius <= 0) {
entity->velocity.x *= -1;
}
if (entity->position.y + entity->radius >= GetScreenHeight() || entity->position.y - entity->radius <= 0) {
entity->velocity.y *= -1;
}
entity->velocity.x += entity->acceleration.x;
entity->velocity.y += entity->acceleration.y;
entity->direction = Vector2Normalize(entity->velocity);
entity->projectionForward = Vector2Add(entity->position, Vector2Scale(entity->direction, flock->awarenessRadius));
entity->position.x += entity->velocity.x * GetFrameTime();
entity->position.y += entity->velocity.y * GetFrameTime();
entity->acceleration.x = 0;
entity->acceleration.y = 0;
}
}
void DrawFlock(Flock flock) {
for (int i = 0; i < MAX_NUM_ENTITIES; i++) {
Entity entity = flock.entities[i];
if (entity.state == ENTITY_STATE_INACTIVE) continue;
DrawCircleV(entity.position, entity.radius, YELLOW);
DrawCircleLinesV(entity.position, entity.radius, RED);
if (flock.renderHelpers) {
DrawCircleLinesV(entity.position, flock.awarenessRadius, (Color){ 255, 255, 255, 30});
DrawCircleLinesV(entity.position, flock.separationRadius, (Color){ 255, 0, 0, 30});
Vector2 forwardTarget = flock.shouldSeekTarget ? flock.seekTarget : entity.projectionForward;
DrawLineV(entity.position, forwardTarget, (Color){ 255, 255, 255, 30});
DrawCircleV(forwardTarget, 2, (Color){ 253, 249, 0, 40 });
}
}
if (flock.shouldSeekTarget) {
DrawCircleV(flock.seekTarget, 5, (Color){ 255, 255, 255, 10});
}
}
void DrawUI(Flock flock) {
char text[100];
sprintf(text, "FPS %d Entities %d Speed %0.1f Align %0.2f Cohesion %0.2f Separation %0.2f Seek %0.2f", GetFPS(), MAX_NUM_ENTITIES, flock.maxSpeed, flock.maxAlignForce, flock.maxCohesionForce, flock.maxSeparationForce, flock.maxSeekForce);
DrawText(text, 10, 10, 20, WHITE);
DrawText("R to Reload. H to toggle debug mode. Left Mouse pressed to seek pointer", 10, GetScreenHeight() - 22, 20, GRAY);
}
int main() {
Flock flock = {
.seekTarget = {0, 0},
.shouldSeekTarget = 0,
.renderHelpers = 0,
.maxSpeed = 150,
.maxAlignForce = 0.1,
.maxCohesionForce = 0.1,
.maxSeparationForce = 1,
.maxSeekForce = 4,
.awarenessRadius = 50,
.separationRadius = 15
};
InitWindow(1024, 1024, "Boids");
SetTargetFPS(144);
Vector2 startPoint = { GetScreenWidth() / 2, GetScreenHeight() / 2 };
InitFlock(&flock, startPoint, ENTITY_STATE_ACTIVE);
while(!WindowShouldClose()) {
UpdateFlock(&flock);
BeginDrawing();
ClearBackground(BLACK);
DrawFlock(flock);
DrawUI(flock);
EndDrawing();
}
CloseWindow();
return 0;
}