How tokachi269/wasm-boids works
Project type and purpose
• This is a single‐page web application (SPA) that integrates a high‐performance C++ Boids simulation via WebAssembly into a Vue.js/Three.js frontend.
• Purpose: run a large‐scale, hierarchical Boids (fish‐school) simulation in the browser with real‐time 3D visualization.
File structure
• Top‐level config/build files:
– package.json, webpack.config.js, vue.config.js, nodemon.json
• Public/static assets:
– public/index.html, assets/.gitkeep
• Frontend source (Vue + Three.js):
– src/main.js (app bootstrap)
– src/App.vue (root component)
– src/components/Settings.vue (UI for simulation parameters)
• WASM simulation engine (C++ sources):
– src/wasm/boids_tree.cpp/.h, entry.cpp/.h, main.cpp, vec3.h
README for additional insights
• Core algorithm: Hierarchical BoidTree structure for spatial partitioning (paper CG‐133, 2008)
• Key features: recursive Boid updates, inter‐unit influence, stress/species parameters
• Integration: C++ simulation ➔ compiled with Emscripten ➔ loaded as WebAssembly module ➔ JS/Three.js drives rendering
Main components and interactions (
a. Components
1) Frontend UI (Vue.js)
– Settings.vue: user‐adjustable parameters
– App.vue: orchestrates canvas + controls
2) Rendering Engine (Three.js)
– Receives boid positions each frame and draws WebGL scene
3) Simulation Engine (WASM module)
– Compiled C++ code exposing functions: initialize(), updateRecursive(), getBoidData()
4) Build Toolchain
– Emscripten: compiles C++ sources → .wasm + JS glue
– Webpack/Vue CLI: bundles JS, Vue components, and WASM assets
5) Static Host
– public/index.html serves the SPA
b. Relationships & data flow
– User interacts with Settings.vue → emits parameter change event
– Vue.js (main.js) captures event → calls JS wrapper functions around WASM API
– WASM module executes BoidTree recursive update → updates internal memory buffer
– JS glue reads back boid position buffer → passes array to Three.js
– Three.js updates mesh positions in WebGL scene → browser renders frame
– Loop: requestAnimationFrame triggers next update/render cycle
c. Architectural patterns & design principles
– Separation of concerns: UI layer, simulation engine, rendering engine
– High‐performance core in WebAssembly; lean JS for orchestration
– Spatial partitioning (KD‐tree–like) for O(n log n) neighbor queries
– Reactive UI (Vue) for parameter tuning in real time
d. Key technologies/frameworks
– Vue.js (SPA framework)
– Three.js (WebGL renderer)
– Emscripten (C++ → WebAssembly)
– Webpack (module bundler)
– C++11 (simulation code, Vec3 library)