oneshotlm
Compare all models on this prompt →

Boids flocking · qwen3-32b

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
Okay, the user wants a full-screen flocking simulation with boids using separation, alignment, and cohesion. The boids should be oriented triangles that steer smoothly and flee from the mouse cursor. Let me break down what I need to do.

First, I need to create an HTML file that includes all the necessary elements inline. So, I'll have a <canvas> element to draw everything. The CSS should make the canvas full-screen. Then, the JavaScript will handle the simulation logic.

For the boids, I'll need to create an array of objects representing each boid. Each boid should have position, velocity, and maybe acceleration vectors. The flocking behavior comes from the three rules: separation, alignment, and cohesion. I'll have to implement each of these as functions that calculate the desired direction and apply it to the boid's acceleration.

Separation means boids steer away from others that are too close. Alignment is steering towards the average direction of nearby boids. Cohesion is moving towards the average position of neighbors.

Also, the boids need to flee from the mouse cursor. So, another rule that checks the distance from the mouse and applies a force away from it if it's within a certain radius.

The triangles should be oriented based on their velocity direction. So, each frame, I'll calculate the angle of the velocity vector and rotate the triangle accordingly.

I'll need to handle the canvas drawing. Clear the canvas each frame, update all boid positions based on their velocities and accelerations, then draw each boid as a triangle pointing in their direction.

Let me outline the steps:

1. Create the HTML structure with a canvas element.
2. Add CSS to make the canvas full-screen.
3. Write JavaScript that initializes an array of boids with random positions and velocities.
4. Implement the flocking rules as functions.
5. Update each boid's acceleration based on the rules and mouse position.
6. Apply acceleration to velocity, then velocity to position.
7. Draw each boid as an oriented triangle on the canvas.
8. Use requestAnimationFrame for smooth animation.

I should also make sure that the boids wrap around the screen edges instead of disappearing. That's a common technique in simulations to keep them within bounds.

Testing for the mouse position is important. If the mouse is near a boid, apply the flee force. The flee force could be a vector pointing away from the mouse, scaled by some factor.

Now, putting all this into code. The JavaScript will be a bit complex with vectors and physics, but manageable. I'll use p5.js or plain JS? Since the user wants everything in one file without external resources, I'll use plain JavaScript with the canvas API.

I'll need to handle vectors, so maybe create a Vector class or use arrays. For simplicity, maybe use objects with x and y properties.

Let me start writing the code structure. Initialize the canvas, set up the boids array, then in each frame, update and draw.

Potential issues: performance with too many boids, but since it's a single-page app, keeping it efficient is key. Using a reasonable number of boids, maybe 200-300.

Now, coding the separation, alignment, cohesion functions. Each function will take a boid and return a force vector. The flee from mouse function as well.

Finally, in the draw loop, apply all forces, update positions, and draw triangles.

Once everything is written, the code should run the simulation as described. Let me put all this into the index.html file.
write index.html (5230 bytes)

ool_call>

toolResult

Successfully wrote 5230 bytes to index.html

assistant
thinking
Okay, let me check what the user needed. They wanted a full-screen flocking simulation with boids using separation, alignment, cohesion, and fleeing from the mouse. The assistant created an HTML file with all the code inline. Let me verify if the code meets the requirements.

First, looking at the HTML structure. The canvas is set to full screen with CSS, which is correct. The JavaScript code defines a Boid class with position, velocity, and acceleration. The update method handles movement and screen wrapping. The draw method uses the velocity direction to orient the triangle, which matches the requirement.

The flocking rules are implemented: separation, alignment, cohesion. The separation checks distance and applies force. Alignment averages neighbors' velocities. Cohesion moves towards the average position. The mouse flee logic checks distance and applies a force away from the cursor. All these elements are present.

I should check if the code is efficient. Using a canvas and requestAnimationFrame for animation is standard. The code uses a simple triangle for each boid, which is efficient enough for 200 boids. The mouse event listener updates the position correctly.

Potential issues: The separation force calculation might need adjustment. The code uses inverse square for separation, which could be effective. The flee force is applied as a unit vector, which should work. The alignment and cohesion forces are scaled by the config parameters, which allows tuning.

The code seems correct. The user's requirements are all addressed. The file is self-contained with no external dependencies. The answer provided the index.html as requested. I think this should work well.

The index.html file has been successfully created with a full-screen boid flocking simulation that includes: 1. **Separation** - Boids steer away from nearby boids using inverse-square distance calculations 2. **Alignment** - Velocity matching with neighbors within perception radius 3. **Cohesion** - Moving toward average position of neighbors 4. **Mouse Flee** - Boids actively avoid the cursor with dynamic force application 5. **Smooth Animation** - 60fps rendering with canvas-based triangle visualization 6. **Edge Wrapping** - Boids reappear on opposite screen side when exiting The simulation uses 200 boids with configurable parameters in the `config` object. All logic is self-contained with no external dependencies, and the oriented triangles rotate naturally based on velocity vectors. You can open the file in any browser to see the interactive flocking behavior where boids scatter when the cursor approaches.

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

modelqwen3-32b
vendorQwen
harnesspi-agent
generated2026-08-01

EVALUATION

4/5 working

Boids flock into cyan clusters with triangle shapes, animating smoothly on their own; clicking scatters a flock briefly.

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 motion25.0%
on click27.0%
on drag25.1%
on enter + space28.0%
on arrow keys25.6%
on w/a/s/d23.0%
frame spread29.9 / 255
console errors0
js errors none

METRICS

runtime49.1s
tokens in5.4k
tokens out2.9k
cached0
cost / run$0.0013