{"id":15903201,"url":"https://github.com/sarckk/boids","last_synced_at":"2025-08-28T21:32:06.756Z","repository":{"id":85893523,"uuid":"386492881","full_name":"sarckk/boids","owner":"sarckk","description":"Boids implementation in C++ with spatial hashing","archived":false,"fork":false,"pushed_at":"2021-07-24T20:50:49.000Z","size":1150,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-16T05:26:03.403Z","etag":null,"topics":["boids","cpp","flocking-simulation","simulation","spatialhash"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sarckk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-16T03:08:49.000Z","updated_at":"2024-11-27T19:34:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"7d5be8cb-c3e5-4e56-8c04-440202f5a71a","html_url":"https://github.com/sarckk/boids","commit_stats":{"total_commits":15,"total_committers":1,"mean_commits":15.0,"dds":0.0,"last_synced_commit":"d63f883f84786f2c95d4b3dbcaf48272146c4704"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarckk%2Fboids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarckk%2Fboids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarckk%2Fboids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sarckk%2Fboids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sarckk","download_url":"https://codeload.github.com/sarckk/boids/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231300707,"owners_count":18355062,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boids","cpp","flocking-simulation","simulation","spatialhash"],"created_at":"2024-10-06T12:01:16.327Z","updated_at":"2024-12-26T02:23:09.332Z","avatar_url":"https://github.com/sarckk.png","language":"C++","readme":"# Boids\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/sarckk/boids/master/images/demo.png\" alt=\"boids_screenshot\" /\u003e\n\u003c/p\u003e\n\n\nBoids simulation in C++. Made with [SFML](https://github.com/SFML/SFML) and [Imgui-SFML](https://github.com/eliasdaler/imgui-sfml).\n\nThis implementation uses [spatial hashing](https://conkerjo.wordpress.com/2009/06/13/spatial-hashing-implementation-for-fast-2d-collisions/) for an efficient radius\nquery.\n\nI also considered tree-based techniques (think k-d trees and Quadtrees),\nbut ended up using spatial hashing for a few reasons:\n\n1. Tree-based techniques are not optimal for when objects on which the queries\n   are being run is constantly moving as in boids simulations. This is because rebuilding\n   the tree is expensive, and thus most implementations of boids using such techniques\n   usually end up rebuilding the entire tree again. In comparison, the spatial hashing method\n   initialises the hash grid once, and only updates the boids' membership in each cell if it has moved (which,\n   in our case, is not frequent because each boid moves very little during 1/60 seconds per frame).\n   Constant-time updates thus give spatial hashing the upper hand here.\n2. Quadtrees were not chosen because it can be tricky to find the optimal value for the max. no. of boids\n   that can go into one leaf node. If we set it to 1 (default), this can be problematic when the distance between\n   any two boids is miniscule, in which case the Quadtree will continue to recursively partition the space\n   until we are left with leaf nodes that represent veeery small areas.\n3. K-d trees were not chosen because they are not well-suited for dynamic insertions, as it is costly to\n   rebalance the tree to ensure efficient queries. This was a problem because this simulation supports dynamically\n   changing the number of boids in the program.\n4. Simplicity. Spatial hashing is simpler to implement, and sometimes simplicity is the key.\n\nThat said, there were still some challenges I encountered along the way. For one, in an ideal scenario,\nthere should be an additional logic in the boids velocity update algorithm to make boids avoid overly-populated\ngrid cells, but I didnt' have the time to implement this.\n\nThen there was the question about radius queries. Usually with fixed grid methods, you instantiate each cell size\nto be equal to the query radius _r_, but in my case, since I wanted to allow users to dynamically change the perception\nradius of the boids, this wasn't possible (unless I rebuilt the whole grid from scratch on each update).\nI thus implemented the solution proposed in Nicolas Brodu's 2007 paper [Query Sphere Indexing for Neighborhood Requests](https://nicolas.brodu.net/common/recherche/publications/QuerySphereIndexing.pdf)\nand adapted it for 2D grids. \n\n### Build Instructions\n\nFirst, execute `build.sh` from the top-level directory of this project:\n\n```bash\n./build.sh\n```\n\nThis will create a `build` directory with the executable inside. Then simply run:\n\n```bash\n./build/boids\n```\n\nYou don't have to install SFML, ImGui or ImGui-SFML on your local machine\nbefore you do this, because this project uses `CMake`'s `FetchContents`\nto manage dependencies. As a result, the first time you execute the script\nit might take a while since it will download all the dependencies\nif they don't exist on your machine.\n\n### References\n\n-   https://www.red3d.com/cwr/boids/\n-   https://www.youtube.com/watch?v=mhjuuHl6qHM\u0026ab_channel=TheCodingTrain\n-   http://www.kfish.org/boids/pseudocode.html\n-   https://nicolas.brodu.net/common/recherche/publications/QuerySphereIndexing.pdf\n-   https://conkerjo.wordpress.com/2009/06/13/spatial-hashing-implementation-for-fast-2d-collisions/\n-   https://www.gamedev.net/tutorials/_/technical/game-programming/spatial-hashing-r2697/\n-   https://www.youtube.com/watch?v=oewDaISQpw0\u0026ab_channel=SimonDev\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarckk%2Fboids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsarckk%2Fboids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsarckk%2Fboids/lists"}