Skip to content

Add high-performance 2D Boids simulation with WebGPU + Svelte 5, deploy via GitHub Pages#1

Merged
LowWeiLin merged 3 commits into
mainfrom
copilot/create-boids-simulation
Mar 24, 2026
Merged

Add high-performance 2D Boids simulation with WebGPU + Svelte 5, deploy via GitHub Pages#1
LowWeiLin merged 3 commits into
mainfrom
copilot/create-boids-simulation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 24, 2026

Implements a 10,000+ agent Boids simulation targeting 60 FPS using WebGPU compute shaders (WGSL), with a Svelte 5 UI and automatic GitHub Pages deployment.

Simulation Engine

  • 3-pass GPU pipeline: spatial grid binning (atomic) → boid logic → instanced triangle render
  • Toroidal topology with Minimum Image Convention across all distance/velocity calculations
  • Agent types: Boids (cohesion + alignment + separation + predator avoidance + goal seeking + obstacle avoidance) and Predators (flock-centre pursuit + obstacle avoidance)
  • Obstacles as analytical shapes (circle/box) passed via uniform buffer; avoidance via SDF steering
  • Goals as global attractors with configurable strength weight
  • WebGL2 CPU-fallback (WebGLFallback.js) using a JS spatial hash + instanced rendering for browsers without WebGPU

UI (Svelte 5 runes)

  • Toolbox: Add/Move Goal, Add/Move Obstacle (click-drag interaction)
  • Sliders: separation, cohesion, alignment weights; perception radius; max speed; boid count (up to 20,000); predator count
  • Toggles: Show Grid, Debug Vectors
  • GPU/WebGL status badge + live FPS counter

Deployment

  • .github/workflows/deploy.yml: builds on push to main, uploads dist/ via actions/upload-pages-artifact, deploys with actions/deploy-pages
  • vite.config.js sets base: '/Boids/' to match the Pages subdirectory

One-time repo setup: Settings → Pages → Source → GitHub Actions

Original prompt

​Project Goal: Create a high-performance 2D Boids simulation in the browser using Vite, Svelte, and WebGPU (or highly optimized WebGL Compute). Target: 10,000+ agents at 60 FPS.
​1. Core Simulation Engine
​Topology: Toroidal (wrap-around) with Minimum Image Convention for distance and velocity calculations.
​Agent Types: * Boids (Prey): Standard Cohesion, Alignment, Separation + Predator Avoidance + Goal Seeking + Obstacle Avoidance.
​Predators: Pursuit behavior (target flock center) + Obstacle Avoidance.
​Obstacles & Goals: * Represent obstacles as Analytical Shapes (Circles/Boxes) passed as a Uniform Buffer to the GPU.
​Goals are global attractors with a "Strength" weight.
​2. Technical Stack & Optimization
​Framework: Svelte 5 (for UI/State) + Vite.
​Compute Strategy: Use WebGPU Compute Shaders (WGSL).
​Pass 1: Spatial Partitioning (Grid-based Binning) on the GPU to avoid O(n^2).
​Pass 2: Boid Logic (Update Velocity/Position).
​Pass 3: Render (Vertex/Fragment shaders using the Boid Buffer).
​Alternative (if WebGPU is unavailable): Use a Data Texture approach in WebGL where boid positions are stored in pixels.
​3. Features & UI (Svelte)
​Interaction Mode: A "Toolbox" to toggle between:
​Add/Move Goal: Click and drag to move a global attractor.
​Add/Move Obstacle: Click to spawn, drag to reposition.
​Simulation Controls: * Sliders for: Separation weight, Cohesion weight, Alignment weight, Perception Radius, and Max Speed.
​Toggle for "Show Grid" and "Debug Vectors."
​Count Slider: Dynamically spawn/despawn boids up to 20,000.
​4. Implementation Details
​Obstacle Avoidance: Use a "Steer-to-Avoid" raycasting logic or an Analytical SDF inside the shader for real-time avoidance of moving shapes.
​Toroidal Sensing: Ensure the spatial grid lookups use % (modulo) to check neighbor cells across the screen boundaries.


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

@LowWeiLin
Copy link
Copy Markdown
Owner

@copilot This should be set up to deploy via GitHub actions/GitHub pages

Copilot AI and others added 2 commits March 24, 2026 16:53
- Svelte 5 + Vite 6 project scaffold
- WebGPU compute pipeline (binning → boid-logic → render)
- Ping-pong boid storage buffers with toroidal topology
- Spatial grid binning shader (atomicAdd, 64 boids/cell max)
- Boid logic shader: separation, cohesion, alignment, predator
  avoidance, goal seeking, SDF obstacle avoidance, MIC toroidal
  distances
- Render shader: velocity-oriented triangles, blue boids / red predators
- WebGL2 + CPU fallback (spatial hash, instanced rendering)
- App.svelte: full-screen canvas, 2D overlay for grid/goals/obstacles,
  pointer events for goal/obstacle placement and drag
- ControlPanel.svelte: all sliders, toolbox, FPS display, GPU status

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI changed the title [WIP] Create high-performance 2D Boids simulation with agents Add high-performance 2D Boids simulation with WebGPU + Svelte 5, deploy via GitHub Pages Mar 24, 2026
Copilot AI requested a review from LowWeiLin March 24, 2026 16:57
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 24, 2026

@copilot This should be set up to deploy via GitHub actions/GitHub pages

Already set up in commit ee29821:

  • .github/workflows/deploy.yml — triggers on every push to main (plus manual workflow_dispatch), installs deps, runs npm run build, uploads dist/ with actions/upload-pages-artifact, then deploys via actions/deploy-pages.
  • vite.config.js — sets base: '/Boids/' so all asset paths resolve correctly under the GitHub Pages subdirectory (https://lowweilin.github.io/Boids/).

One-time repo setup needed: Settings → Pages → Source → GitHub Actions.

@LowWeiLin LowWeiLin marked this pull request as ready for review March 24, 2026 17:13
@LowWeiLin LowWeiLin merged commit 849de10 into main Mar 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants