fix massive oversight

This commit is contained in:
Rokas Puzonas 2023-07-21 22:24:57 +03:00
parent 3c627cd9e1
commit cabc63c2c2
4 changed files with 30 additions and 19 deletions

View File

@ -3,7 +3,7 @@
#include "boid-list.hpp" #include "boid-list.hpp"
static BoidsListNodeIterator boid_list_get_iterator(BoidsListNode *node, uint16_t count) { 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) { 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; int left_count = *count;
BoidsListNode *prev = node; BoidsListNode *prev = node;
BoidsListNode *curr = node; BoidsListNode *curr = node;
while (left_count > BOIDS_PER_NODE && curr) { while (left_count >= BOIDS_PER_NODE && curr) {
prev = curr; prev = curr;
curr = curr->next; curr = curr->next;
left_count -= BOIDS_PER_NODE; left_count -= BOIDS_PER_NODE;
@ -38,7 +38,7 @@ static void boid_list_append(MemoryArena *arena, BoidsListNode *node, uint16_t *
prev->next = curr; prev->next = curr;
} }
prev->boid_ids[left_count] = new_boid; curr->boid_ids[left_count] = new_boid;
(*count)++; (*count)++;
} }

View File

@ -2,17 +2,17 @@
#include "memory-arena.hpp" #include "memory-arena.hpp"
#define BOIDS_PER_NODE 32 #define BOIDS_PER_NODE 128
struct BoidsListNode { struct BoidsListNode {
uint16_t boid_ids[BOIDS_PER_NODE];
BoidsListNode *next; BoidsListNode *next;
uint16_t boid_ids[BOIDS_PER_NODE];
}; };
struct BoidsListNodeIterator { struct BoidsListNodeIterator {
BoidsListNode *node;
int i;
uint16_t count; uint16_t count;
int i;
BoidsListNode *node;
}; };
static BoidsListNodeIterator boid_list_get_iterator(BoidsListNode *node, uint16_t count); static BoidsListNodeIterator boid_list_get_iterator(BoidsListNode *node, uint16_t count);

View File

@ -37,12 +37,12 @@ struct World {
std::vector<Obstacle> obstacles; std::vector<Obstacle> obstacles;
MemoryArena frame_arena; MemoryArena frame_arena;
float view_radius = 15; float view_radius = 10;
float view_angle = PI*1.5; float view_angle = PI*1.5;
float min_speed = 30; float min_speed = 10;
float max_speed = 50; float max_speed = 40;
float max_steer_speed = 100; float max_steer_speed = 200;
float separation_radius = 10; float separation_radius = 4;
float alignment_strength = 1; float alignment_strength = 1;
float cohesion_strength = 1; float cohesion_strength = 1;
@ -58,7 +58,7 @@ struct World {
}; };
struct Visuals { struct Visuals {
float boid_edge_size = 8; float boid_edge_size = 5;
Color boid_color = BLACK; Color boid_color = BLACK;
Color bg_color = RAYWHITE; Color bg_color = RAYWHITE;

View File

@ -12,7 +12,7 @@
#include "raygui.h" #include "raygui.h"
#define RPROF_IMPLEMENTATION #define RPROF_IMPLEMENTATION
#define RPROF_STUB_OUT // #define RPROF_STUB_OUT
#include "rprof.h" #include "rprof.h"
#include "boid-playground.hpp" #include "boid-playground.hpp"
@ -247,25 +247,32 @@ static void world_update(World *world, float dt) {
Boid *boid = &boids[i]; Boid *boid = &boids[i];
int chunk_x = boid->pos.x / chunk_size; int chunk_x = boid->pos.x / chunk_size;
int chunk_y = boid->pos.y / 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_STOP();
RPROF_START("Calc dot products and ranges (chunked)"); RPROF_START("Calc dot products and ranges (chunked)");
for (int y = 0; y < chunks_high; y++) { 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]; BoidsListNode *chunk = chunks[y][x];
size_t chunk_boid_count = chunk_boid_counts[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++) { 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; if (chunk_y < 0 || chunk_y >= chunks_high) continue;
for (int ox = -1; ox <= 1; ox++) { 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; if (chunk_x < 0 || chunk_x >= chunks_wide) continue;
BoidsListNode *neighbour_chunk = chunks[chunk_y][chunk_x]; BoidsListNode *neighbour_chunk = chunks[chunk_y][chunk_x];
size_t neighbour_chunk_boid_count = chunk_boid_counts[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; uint16_t boid1;
BoidsListNodeIterator it1 = boid_list_get_iterator(chunk, chunk_boid_count); 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_STOP();
RPROF_START("Apply forces"); RPROF_START("Apply forces");
for (int i = 0; i < boid_count; i++) { for (int i = 0; i < boid_count; i++) {
Boid *boid = &world->boids[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 radius", &world->view_radius, 2.5, 150);
GuiSlider(next_in_layout(&layout, 100, 15), NULL, "View angle", &world->view_angle, 0, 2*PI); 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), "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); 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); world_init(&g_world, screen_width, screen_height);
float border = g_world.collision_avoidance_distance; 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 boid;
boid_rand_init(&g_world, &boid, border); boid_rand_init(&g_world, &boid, border);
g_world.boids.push_back(boid); g_world.boids.push_back(boid);
} }
// g_world.boids.push_back({ .pos = { 800, 105 }});
// g_world.boids.push_back({ .pos = { 800, 110 }});
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
emscripten_set_main_loop(UpdateDrawFrame, 0, 1); emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else #else