move boid drawing to separate function

This commit is contained in:
Rokas Puzonas 2023-08-01 22:04:10 +03:00
parent 50c860ddf9
commit c713b271c7
2 changed files with 42 additions and 17 deletions

View File

@ -1,3 +1,4 @@
-Idepends/raylib-cpp/include/ -Idepends/raylib-cpp/include/
-Idepends/raylib/src/ -Idepends/raylib/src/
-Idepends/raygui/src/ -Idepends/raygui/src/
-DRLGL_IMPLEMENTATION

View File

@ -742,6 +742,41 @@ static void draw_circle_sector(Vector2 center, float radius, float start_angle,
rlEnd(); rlEnd();
} }
static void draw_boids(World *world, Visuals *visuals) {
int boid_count = world->boids.size();
float boid_length = visuals->boid_edge_size * std::sqrt(3)/2;
float boid_width = visuals->boid_edge_size * 0.6;
Color color = visuals->boid_color;
rlBegin(RL_TRIANGLES);
for (int i = 0; i < boid_count; i++) {
Boid *boid = &world->boids[i];
Vector2 triangle[] = {
{ boid_length*2/3.0f, 0 },
{ -boid_length*1/3.0f, -boid_width/2 },
{ -boid_length*1/3.0f, boid_width/2 },
};
float facing = std::atan2(boid->dir.y, boid->dir.x);
float facing_cos = cos(facing);
float facing_sin = sin(facing);
for (int i = 0; i < 3; i++) {
Vector2 new_pos = boid->pos;
new_pos.x += triangle[i].x * facing_cos - triangle[i].y * facing_sin;
new_pos.y += triangle[i].x * facing_sin + triangle[i].y * facing_cos;
triangle[i] = new_pos;
}
rlColor4ub(color.r, color.g, color.b, color.a);
rlVertex2f(triangle[0].x, triangle[0].y);
rlVertex2f(triangle[1].x, triangle[1].y);
rlVertex2f(triangle[2].x, triangle[2].y);
}
rlEnd();
}
static void world_draw(World *world, Visuals *visuals) { static void world_draw(World *world, Visuals *visuals) {
for (int i = 0; i < world->obstacles.size(); i++) { for (int i = 0; i < world->obstacles.size(); i++) {
draw_obstacle(&world->obstacles[i], GRAY); draw_obstacle(&world->obstacles[i], GRAY);
@ -761,32 +796,21 @@ static void world_draw(World *world, Visuals *visuals) {
} }
} }
float boid_length = visuals->boid_edge_size * std::sqrt(3)/2; int boid_count = world->boids.size();
float boid_width = visuals->boid_edge_size * 0.6; for (int i = 0; i < boid_count; i++) {
for (int i = 0; i < world->boids.size(); i++) {
Boid *boid = &world->boids[i]; Boid *boid = &world->boids[i];
if (visuals->draw_collision_avoidance_rays) { if (visuals->draw_collision_avoidance_rays) {
draw_obstacle_avoidance_rays(visuals, world, boid); draw_obstacle_avoidance_rays(visuals, world, boid);
} }
if (visuals->draw_separation_radius) { if (visuals->draw_separation_radius) {
DrawCircleLines(boid->pos.x, boid->pos.y, world->separation_radius, MAGENTA); DrawCircleLines(boid->pos.x, boid->pos.y, world->separation_radius, MAGENTA);
} }
}
Vector2 triangle[] = { draw_boids(world, visuals);
{ boid_length*2/3.0f, 0 },
{ -boid_length*1/3.0f, -boid_width/2 },
{ -boid_length*1/3.0f, boid_width/2 },
};
float facing = std::atan2(boid->dir.y, boid->dir.x);
for (int i = 0; i < 3; i++) {
triangle[i] = Vector2Add(boid->pos, Vector2Rotate(triangle[i], facing));
}
DrawTriangle(triangle[0], triangle[1], triangle[2], visuals->boid_color);
for (int i = 0; i < boid_count; i++) {
Boid *boid = &world->boids[i];
if (visuals->draw_boid_direction) { if (visuals->draw_boid_direction) {
DrawCircle(boid->pos.x, boid->pos.y, visuals->boid_edge_size * 0.05, RED); DrawCircle(boid->pos.x, boid->pos.y, visuals->boid_edge_size * 0.05, RED);
Vector2 look_pos = Vector2Add(boid->pos, vector2_mul_value(boid->dir, visuals->boid_edge_size*1.5)); Vector2 look_pos = Vector2Add(boid->pos, vector2_mul_value(boid->dir, visuals->boid_edge_size*1.5));