Inspiration

Every physics student draws the same diagrams: a block on a ramp, a pendulum, two balls about to collide. You label the masses, write μ = 0.2, draw an arrow for the speed, and then nothing happens. The drawing never moves, so you never find out whether your intuition was right. I wanted the diagram in the notebook to be the experiment.

What it does

You take a photo of a sketch, upload one, or draw on the screen. Doodle Lab:

  1. Reads the drawing with Gemini into strict JSON: every ball, box, surface, rope, rod, spring and pivot, where it is, and the numbers written beside it (mass, friction, bounce, spring constant, the speed on an arrow, a length that sets the scale). What it read is drawn over your photo in blue ink.
  2. Snaps and checks the reading. Code moves each shape onto the pencil marks actually in the photo, then fixes what is left: a ball placed inside the floor, a block tilted differently from its ramp, a seesaw pivot a few pixels off its fulcrum, links to objects that do not exist, impossible values. Every change is listed ("Fixed", "Assumed", "Left out").
  3. Runs it with Planck.js (a port of the Box2D physics engine) in SI units. Your own pencil lines are lifted off the paper and moved to where the engine puts them, with strobe copies every eighth of a second.

For students: change any number and run it again (the sample ramp is 24°, tan 24° = 0.45, so the block slides at μ = 0.2 and stays put at μ = 0.5); tap where you think the object will stop before running it; compare the engine with the textbook formula for pendulums (T = 2π√(L/g), with the large-angle correction) and springs (T = 2π√(m/k)); watch energy move between motion, height, spring and friction.

The AI, and why it matters here

Model: Google Gemini through the Gemini API, with structured JSON output. Order of models: gemini-3-flash-preview (low thinking), then gemini-3.5-flash (low thinking), then gemini-3.5-flash-lite (low thinking), then gemini-3.1-flash-lite. The free tier allows 20 requests a day for the first two, so most live reads use 3.5 Flash-Lite; the page always says which model read the drawing.

Role: the model is the eyes and nothing else. It turns a messy pencil drawing, including handwriting like "500 g" or "k = 50 N/m", into a structured scene. It never decides how anything moves; the physics engine does. That split is the point: language models are bad at simulating physics, and physics engines cannot read handwriting.

Representative input and output. Input: the pendulum sample drawing. Output (shortened):

{"isPhysicsSketch": true,
 "whatIsIt": "A pendulum with a 5 kg bob suspended by a string of length 1 m from a ceiling.",
 "balls": [{"id": "bob", "box_2d": [498, 683, 573, 737], "label": "5 kg", "massKg": 5, ...}],
 "surfaces": [{"id": "ceiling", "points": [[134, 285], [134, 725]], ...}],
 "anchors": [{"id": "pivot", "point": [134, 508]}],
 "links": [{"kind": "rope", "from": "pivot", "to": "bob", ...}],
 "measurements": [{"from": [134, 465], "to": [532, 672], "meters": 1, "label": "1 m"}]}

The app turns that into a 1.00 m pendulum released 34° from vertical: the formula gives 2.05 s and the engine measures 2.05 s. For a drawing of a house with a sun, the model answers isPhysicsSketch: false and the app says so instead of inventing a scene.

How I built it

  • Reader: a Cloudflare Pages Function holds the Gemini key, accepts only same-origin JSON with an image under about 1.5 MB, rate-limits each visitor, and caps the whole site at 300 reads a day with a Workers KV counter. The JSON schema asks for one list per kind (balls, boxes, surfaces, anchors) with the geometry each needs marked required, because in early tests the model skipped optional position fields.
  • Snapping (refine.ts): builds an "ink map" of the photo (darkness relative to the local paper colour) and searches near each AI shape for the circle, rotated rectangle or line that sits best on the ink. It only moves a shape when the new place is clearly better, and never adds or removes anything.
  • Checker (validate.ts): turns untrusted JSON into a runnable scene, with every correction written down for the student.
  • Physics: Planck.js at 480 steps a second. A written μ is the friction between the two things touching (Box2D's own √(μa·μb) rule would change a written value), and a value on the object wins over one on the ramp.
  • Your lines move (cutout.ts): each moving shape is cut out of the photo with the paper made transparent, the spot is painted with the surrounding paper colour, and lines under it are restored only where the photo really had ink.
  • Front end: TypeScript and canvas, no framework; a draw pad for people without paper; works on a phone.

Testing (how I know it works)

  • Reading, 45 drawings with known answers (npm run eval). I wrote a generator that renders scenes as pencil sketches on paper made to look photographed (lined, squared, plain or cream paper; pencil or pen; three handwriting fonts; desk edges, uneven light, grain, rotation, blur), so every drawing has an exact answer: 36 normal drawings of 12 setups, 4 that are not physics, and 5 deliberately hard ones. A case passes only if every object is found and none invented, surfaces are traced within 2% of the image, every written number is read correctly, links are attached correctly, the scale is right, and the engine moves the read scene the same way as the drawn one. The prompt was tuned on 12 drawings and not on the other 33.
    • Gemini 3.5 Flash-Lite (low thinking): 35 of 45 pass (29 without snapping); 28/36 normal drawings, 4/4 non-physics drawings refused, 3/5 hard cases; every written label read correctly (55/55); median 2.9 s.
    • Gemini 3.5 Flash (low thinking), 12 drawings: 11/12, but 17 s per read. Gemini 3 Flash: 8 of the 9 it answered before the daily quota ran out.
    • Snapping to the pencil lines turned 6 Flash-Lite failures into passes and halved its position error; in these runs, for the models the app uses, it never turned a pass into a fail.
  • Engine against textbook physics (71 unit tests): free fall within 2% of √(2h/g), pendulum within 2% of 2π√(L/g) with the large-angle correction, spring within 3% of 2π√(m/k), a block on a 30° ramp holds at μ = 0.7 and slides at g(sin θ − μ cos θ) at μ = 0.2, energy drift under 1% per second, identical runs every time. The checker is tested against garbage JSON and bad readings; the scorer must pass a perfect reading of all 45 cases and fail planted mistakes.
  • In a real browser: Playwright drives Edge through the samples, prediction, a phone-sized screen, and an end-to-end test that draws on the pad, reads it through the live API and runs it.

Failure cases and limits

  • Towers of blocks fail every time. Knocking a tower over is chaotic: a block a few pixels wider falls a different way, so the result does not match the drawing even when the reading is good.
  • Lines read too far off. Flash-Lite sometimes places a floor or ramp 3–8% of the image away from the pencil line. Snapping fixes errors up to about 3.5%; a wider search scored worse overall because it can jump onto hatching or ruled lines. In the end-to-end test the ball rolls slightly above the drawn ramp for this reason.
  • Curved ramps get too few points, so the ball rolls on a straighter slope.
  • Collisions are sensitive: a 1-pixel change in where two balls touch tilts the bounce.
  • Generated test drawings only. Real phone photos of real pencil drawings have not been scored yet.
  • 2D only; a ball is a disc to the engine (it rolls like a coin, not a solid sphere); no air or rolling resistance.

Challenges

The model skipped optional position fields, so the schema had to make geometry required per kind. A ball "rolling on air" taught me that the reading needs to be snapped to the actual ink. A seesaw that would not tip turned out to be a plank resting on the fulcrum's tip 3 px from its pivot. And my first outcome check failed near-identical readings of a collision, because after a few metres of rolling two scenes 1 px apart end up 19 cm apart; the tolerance now scales with the distance travelled.

Accomplishments

The drawing on the page really moves, and the numbers hold up: the pendulum matches the textbook to the hundredth of a second, the friction threshold behaves as tan θ predicts, and every claim about the AI has a test set and a published list of failures behind it.

What I learned

Let each part do what it is good at: the model reads, plain code checks, the physics engine moves. Measuring with a generated test set made it obvious which model to use where, and that the cheap fast model plus snapping beats it alone.

What's next

Score real phone photos of student drawings; curved surfaces from more points; pulleys and inclined-plane carts; teacher-made challenge sheets ("draw a setup where the ball lands in the cup").

Pre-existing work vs this event

Built during AI OpenBuild (November 2026). Adapted from my earlier project Plain Page: the small Gemini JSON client with model fallback and the request guard. Open-source components: Planck.js (MIT), the Recursive typeface (OFL), Vite, Vitest, Playwright. Datasets: none; all 45 test drawings and the 6 sample drawings are generated by code in the repo. Model provider: Google (Gemini API).

Team

Zuhair Halawa (solo): idea, design, code, testing, video.

AI assistance

Claude was used as a coding assistant.

Built With

  • box2d
  • cloudflare-pages
  • cloudflare-workers
  • gemini-api
  • google-gemini
  • html5
  • planck.js
  • playwright
  • typescript
  • vite
  • vitest
Share this project:

Updates

Submission history