oneshotlm
Compare all models on this prompt →

Boids flocking · gemma-4-31b-it

Create a full-screen flocking simulation of boids using separation, alignment, and cohesion, drawn as oriented triangles that steer smoothly and flee from the mouse cursor.

⛨ sandboxed Open in new tab ↗
transcript · 4 messages
user

Create a full-screen flocking simulation of boids using separation, alignment, and cohesion, drawn as oriented triangles that steer smoothly and flee from the mouse cursor.

assistant
thinking
The user wants a full-screen flocking simulation (Boids) in a single `index.html` file.

Key requirements:
1.  **Boids logic**: Separation, Alignment, and Cohesion.
2.  **Visuals**: Oriented triangles.
3.  **Interaction**: Flee from the mouse cursor.
4.  **Constraint**: Single file `index.html`, no external resources, no network requests.
5.  **Full-screen**: Canvas should fill the window.

Plan:
1.  Create `index.html`.
2.  Implement a `Boid` class with:
    *   Position, velocity, acceleration.
    *   `update()` method to apply forces and update position.
    *   `draw()` method to render as an oriented triangle.
    *   `flock()` method to calculate the three main steering forces.
    *   `flee()` method to move away from the mouse.
3.  Implement a `Simulation` or main loop to:
    *   Initialize a population of Boids.
    *   Handle window resizing.
    *   Track mouse position.
    *   Clear canvas and draw all Boids every frame.
4.  Tweak parameters (visual range, max speed, max force) for a natural feel.

Let's write the code.
write index.html (7523 bytes)
toolResult

Successfully wrote 7523 bytes to index.html

assistant
thinking
The user wants a full-screen flocking simulation of boids.
I have created an `index.html` file with:
1. CSS for full-screen layout and basic styling.
2. JS with a `Vector` class for math.
3. A `Boid` class implementing:
    - Separation
    - Alignment
    - Cohesion
    - Mouse flee behavior
    - Screen wrapping (edges)
    - Triangle drawing oriented to velocity
4. A loop that handles updates and rendering.
5. Event listeners for resize and mouse movement.

The requirements were:
- Single-page web app.
- One self-contained file `index.html`.
- CSS in `<style>`, JS in `<script>`.
- No external resources/CDNs.
- Full-screen.
- Oriented triangles.
- Smooth steering.
- Flee from mouse.

I've implemented all of these. The `Boid` class handles the Reynolds' boids algorithm (separation, alignment, cohesion) and the flee force. The movement is smoothed by limiting the max force and max speed.

One small detail: in the `flock` method, I'm using `Vector.sub` and `Vector.dist` which I implemented as static methods. The `Vector` instance methods like `add`, `sub`, `mul`, `div` mutate the instance. I need to be careful about mutating vectors that are shared or meant to be temporary.

In `flock()`:
- `alignment`, `cohesion`, `separation` are created fresh.
- `alignment.div(total).setMag(SETTINGS.maxSpeed).sub(this.velocity).limit(SETTINGS.maxForce);` - this works because it's a chain on the fresh `alignment` vector.

Wait, looking at `Vector.sub(v1, v2)`, it returns a new Vector.
But `this.velocity.add(this.acceleration)` mutates `this.velocity`.
This is generally fine for the Boids implementation as long as I don't accidentally mutate positions or velocities when I should be calculating a steer force.

Let's double check the `flock` calculations:
`alignment.div(total).setMag(SETTINGS.maxSpeed).sub(this.velocity).limit(SETTINGS.maxForce);`
This mutates the local `alignment` vector, then uses `sub(this.velocity)` which also mutates `alignment`. This is correct.

The `flee()` method:
`let steer = Vector.sub(this.position, mouse);` - creates new vector.
`steer.setMag(SETTINGS.maxSpeed).sub(this.velocity).limit(SETTINGS.fleeForce);` - mutates `steer`. Correct.

Everything looks solid.

I have created a complete, self-contained boids flocking simulation in `index.html`. The implementation includes: - **Boids Algorithm**: Fully implemented Separation, Alignment, and Cohesion to create realistic flocking behavior. - **Smooth Steering**: Uses a force-based approach (acceleration -> velocity -> position) with `maxSpeed` and `maxForce` limits to ensure smooth, organic movement. - **Interactive Fleeing**: Boids detect the mouse cursor within a specific radius and steer away from it. - **Visuals**: Boids are rendered as oriented triangles that point in the direction of their current velocity. - **Optimization**: Uses a simple distance-based perception check and basic canvas rendering for high performance. - **Full-Screen**: The canvas automatically resizes to fit the window and boids wrap around the screen edges. You can open `index.html` in any modern web browser to run the simulation.

TAKE IT TO CODE

A single self-contained HTML file — copy it and open in any browser, or reuse the prompt with your own pi-agent run.

RUN

modelgemma-4-31b-it
vendorGoogle
harnesspi-agent
generated2026-07-22

EVALUATION

4/5 working

Boids as oriented triangles flock and swirl continuously in flowing clusters; consistent across all inputs, no visible mouse-flee effect confirmed.

Vision grade · anthropic/claude-sonnet-5

MEASUREMENTS

Raw readings from the capture, not a verdict — the grade above is the only judgement. Percentages are how much of the screen changed; the measure under-reports sparse motion on a dark background.

idle motion6.7%
on click7.9%
on drag8.2%
on wheel8.4%
on enter + space8.3%
on arrow keys8.3%
on w/a/s/d8.1%
frame spread12.7 / 255
console errors0
js errors none

METRICS

runtime124.3s
tokens in4.9k
tokens out3.2k
cached0
cost / run$0.0017