Write a complete, copy-pasteable p5.js sketch that shows a rigid square performing pure…
- User
Write a complete, copy-pasteable p5.js sketch that shows a rigid square performing pure rolling motion (rolling without slipping) along a flat horizontal surface.
Requirements:
- In every frame compute the square's pose only from the current elapsed time—no stored state or numerical integration.
- Exactly one corner must be in contact with the ground at any instant, and that contact point must have zero velocity relative to the ground (no sliding or clipping).
- Successive corners should take turns as the contact point, so a full 360-degree rotation occurs after the square has travelled four side-lengths.
- When the square leaves one canvas edge, wrap it seamlessly to the opposite edge with continuous motion and orientation.
- Include a simple ground line and square styling; the sketch must be formatted as a TypeScript module with the structure:
import p5 from 'p5' export default function sketch(p: p5) { // Variables and classes here p.setup = () => { // Setup code using p. prefix (e.g., p.createCanvas(600, 300)) } p.draw = () => { // Draw code using p. prefix (e.g., p.background(255)) } }All p5.js functions must be prefixed with 'p.' (e.g., p.createCanvas, p.background, p.rect, p.translate, p.rotate).
Assistant
import p5 from 'p5' export default function sketch(p: p5) { const s = 100; // Side length of the square const speed = 1; // Speed of the square p.setup = () => { p.createCanvas(600, 300); } p.draw = () => { p.background(255); // Draw ground line p.stroke(0); p.line(0, p.height, p.width, p.height); const t = p.frameCount; const d = speed * t; const wrappedD = d % (4 * s); const phase = Math.floor(wrappedD / s); const f = wrappedD % s; // Determine the rotation angle and position based on phase let theta = 0; let xCenter = 0; let yCenter = 0; if (phase === 0) { theta = (f / s) * p.HALF_PI; xCenter = d; yCenter = s * Math.sqrt(2) / 2; } else if (phase === 1) { theta = p.HALF_PI + (f / s) * p.HALF_PI; xCenter = d; yCenter = s * Math.sqrt(2) / 2; } else if (phase === 2) { theta = p.PI + (f / s) * p.HALF_PI; xCenter = d; yCenter = s * Math.sqrt(2) / 2; } else if (phase === 3) { theta = 3 * p.HALF_PI + (f / s) * p.HALF_PI; xCenter = d; yCenter = s * Math.sqrt(2) / 2; } // Wrap x position const wrappedX = (xCenter + p.width) % p.width; p.push(); p.translate(wrappedX, p.height); p.rotate(theta); p.rect(-s/2, -s/2, s, s); p.pop(); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.