From cabc63c2c2bb35a9083fc7e728816cdc1b8555d9 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Fri, 21 Jul 2023 22:24:57 +0300 Subject: [PATCH] fix massive oversight --- src/boid-list.cpp | 6 +++--- src/boid-list.hpp | 8 ++++---- src/boid-playground.hpp | 12 ++++++------ src/main.cpp | 23 +++++++++++++++++------ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/boid-list.cpp b/src/boid-list.cpp index 61b2c12..27ecde0 100644 --- a/src/boid-list.cpp +++ b/src/boid-list.cpp @@ -3,7 +3,7 @@ #include "boid-list.hpp" static BoidsListNodeIterator boid_list_get_iterator(BoidsListNode *node, uint16_t count) { - return { .node = node, .i = 0, .count = count }; + return { .count = count, .i = 0, .node = node }; } static bool boid_list_iterator_next(BoidsListNodeIterator *iterator, uint16_t *value) { @@ -26,7 +26,7 @@ static void boid_list_append(MemoryArena *arena, BoidsListNode *node, uint16_t * int left_count = *count; BoidsListNode *prev = node; BoidsListNode *curr = node; - while (left_count > BOIDS_PER_NODE && curr) { + while (left_count >= BOIDS_PER_NODE && curr) { prev = curr; curr = curr->next; left_count -= BOIDS_PER_NODE; @@ -38,7 +38,7 @@ static void boid_list_append(MemoryArena *arena, BoidsListNode *node, uint16_t * prev->next = curr; } - prev->boid_ids[left_count] = new_boid; + curr->boid_ids[left_count] = new_boid; (*count)++; } diff --git a/src/boid-list.hpp b/src/boid-list.hpp index cdd863e..27c274e 100644 --- a/src/boid-list.hpp +++ b/src/boid-list.hpp @@ -2,17 +2,17 @@ #include "memory-arena.hpp" -#define BOIDS_PER_NODE 32 +#define BOIDS_PER_NODE 128 struct BoidsListNode { - uint16_t boid_ids[BOIDS_PER_NODE]; BoidsListNode *next; + uint16_t boid_ids[BOIDS_PER_NODE]; }; struct BoidsListNodeIterator { - BoidsListNode *node; - int i; uint16_t count; + int i; + BoidsListNode *node; }; static BoidsListNodeIterator boid_list_get_iterator(BoidsListNode *node, uint16_t count); diff --git a/src/boid-playground.hpp b/src/boid-playground.hpp index 1566bc5..62a943d 100644 --- a/src/boid-playground.hpp +++ b/src/boid-playground.hpp @@ -37,12 +37,12 @@ struct World { std::vector obstacles; MemoryArena frame_arena; - float view_radius = 15; + float view_radius = 10; float view_angle = PI*1.5; - float min_speed = 30; - float max_speed = 50; - float max_steer_speed = 100; - float separation_radius = 10; + float min_speed = 10; + float max_speed = 40; + float max_steer_speed = 200; + float separation_radius = 4; float alignment_strength = 1; float cohesion_strength = 1; @@ -58,7 +58,7 @@ struct World { }; struct Visuals { - float boid_edge_size = 8; + float boid_edge_size = 5; Color boid_color = BLACK; Color bg_color = RAYWHITE; diff --git a/src/main.cpp b/src/main.cpp index 9fc1922..5a23018 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ #include "raygui.h" #define RPROF_IMPLEMENTATION -#define RPROF_STUB_OUT +// #define RPROF_STUB_OUT #include "rprof.h" #include "boid-playground.hpp" @@ -247,25 +247,32 @@ static void world_update(World *world, float dt) { Boid *boid = &boids[i]; int chunk_x = boid->pos.x / chunk_size; int chunk_y = boid->pos.y / chunk_size; + + BoidsListNode *node = chunks[chunk_y][chunk_x]; + uint16_t *count = &chunk_boid_counts[chunk_y][chunk_x]; + boid_list_append(&world->frame_arena, node, count, i); } RPROF_STOP(); RPROF_START("Calc dot products and ranges (chunked)"); for (int y = 0; y < chunks_high; y++) { - for (int x = 0; x < chunks_high; x++) { + for (int x = 0; x < chunks_wide; x++) { BoidsListNode *chunk = chunks[y][x]; size_t chunk_boid_count = chunk_boid_counts[y][x]; + if (chunk_boid_count == 0) continue; for (int oy = -1; oy <= 1; oy++) { - int chunk_y = x + oy; + int chunk_y = y + oy; if (chunk_y < 0 || chunk_y >= chunks_high) continue; for (int ox = -1; ox <= 1; ox++) { - int chunk_x = y + ox; + int chunk_x = x + ox; if (chunk_x < 0 || chunk_x >= chunks_wide) continue; BoidsListNode *neighbour_chunk = chunks[chunk_y][chunk_x]; size_t neighbour_chunk_boid_count = chunk_boid_counts[chunk_y][chunk_x]; + if (neighbour_chunk_boid_count == 0) continue; + uint16_t boid1; BoidsListNodeIterator it1 = boid_list_get_iterator(chunk, chunk_boid_count); @@ -284,6 +291,7 @@ static void world_update(World *world, float dt) { } RPROF_STOP(); + RPROF_START("Apply forces"); for (int i = 0; i < boid_count; i++) { Boid *boid = &world->boids[i]; @@ -510,7 +518,7 @@ static void ui_draw(World *world, Visuals *visuals, UI *ui) { GuiSlider(next_in_layout(&layout, 100, 15), NULL, "View radius", &world->view_radius, 2.5, 150); GuiSlider(next_in_layout(&layout, 100, 15), NULL, "View angle", &world->view_angle, 0, 2*PI); - gui_valuebox_float(next_in_layout(&layout, 50, 15, 60), "Min speed", &world->min_speed, 0, world->max_speed, &ui->min_speed_edit); + gui_valuebox_float(next_in_layout(&layout, 50, 15, 60), "Min speed", &world->min_speed, 0, 1000, &ui->min_speed_edit); gui_valuebox_float(next_in_layout(&layout, 50, 15, 60), "Max speed", &world->max_speed, 0, 1000, &ui->max_speed_edit); gui_valuebox_float(next_in_layout(&layout, 50, 15, 60), "Steer speed", &world->max_steer_speed, 0, 1000, &ui->steer_speed_edit); @@ -604,12 +612,15 @@ int main() { world_init(&g_world, screen_width, screen_height); float border = g_world.collision_avoidance_distance; - for (int i = 0; i < MAX_BOIDS; i++) { + for (int i = 0; i < 20000; i++) { Boid boid; boid_rand_init(&g_world, &boid, border); g_world.boids.push_back(boid); } + // g_world.boids.push_back({ .pos = { 800, 105 }}); + // g_world.boids.push_back({ .pos = { 800, 110 }}); + #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(UpdateDrawFrame, 0, 1); #else