import p5 from 'p5'
export default function sketch(p: p5) {
const squareSize = 60;
const groundLevel = 220;
const horizontalSpeed = 80; // pixels per second
p.setup = () => {
p.createCanvas(600, 300);
}
p.draw = () => {
p.background(240);
// Draw ground line
p.stroke(0);
p.strokeWeight(2);
p.line(0, groundLevel, p.width, groundLevel);
const time = p.millis() / 1000;
const totalDistance = horizontalSpeed * time;
// Calculate rolling motion parameters
const rollCycleLength = 4 * squareSize; // Distance for one complete 360° rotation
const distanceInCycle = totalDistance % rollCycleLength;
const totalRotation = (totalDistance / rollCycleLength) * 2 * p.PI;
// Contact point position with screen wrapping
const contactX = (totalDistance % p.width + p.width) % p.width;
const contactY = groundLevel;
// Distance from square center to any corner
const centerToCornerDistance = squareSize * Math.sqrt(2) / 2;
// Determine which corner is currently in contact
const segmentLength = squareSize; // Distance traveled per quarter rotation
const segmentIndex = Math.floor(distanceInCycle / segmentLength);
const progressInSegment = (distanceInCycle % segmentLength) / segmentLength;
const rotationInSegment = progressInSegment * p.PI / 2;
// Calculate center position based on which corner is touching
let centerX, centerY;
switch(segmentIndex) {
case 0: // Bottom-left corner touching
centerX = contactX + centerToCornerDistance * Math.cos(p.PI/4 + rotationInSegment);
centerY = contactY - centerToCornerDistance * Math.sin(p.PI/4 + rotationInSegment);
break;
case 1: // Bottom-right corner touching
centerX = contactX + centerToCornerDistance * Math.cos(3*p.PI/4 + rotationInSegment);
centerY = contactY - centerToCornerDistance * Math.sin(3*p.PI/4 + rotationInSegment);
break;
case 2: // Top-right corner touching
centerX = contactX + centerToCornerDistance * Math.cos(5*p.PI/4 + rotationInSegment);
centerY = contactY - centerToCornerDistance * Math.sin(5*p.PI/4 + rotationInSegment);
break;
default: // Top-left corner touching (case 3)
centerX = contactX + centerToCornerDistance * Math.cos(7*p.PI/4 + rotationInSegment);
centerY = contactY - centerToCornerDistance * Math.sin(7*p.PI/4 + rotationInSegment);
break;
}
// Draw the rolling square
p.push();
p.translate(centerX, centerY);
p.rotate(totalRotation);
p.fill(100, 150, 255);
p.stroke(0);
p.strokeWeight(2);
p.rectMode(p.CENTER);
p.rect(0, 0, squareSize, squareSize);
p.pop();
// Visualize the contact point
p.fill(255, 50, 50);
p.noStroke();
p.circle(contactX, contactY, 8);
}
}
This sketch implements pure rolling motion for a square where:
- The square's position is computed entirely from elapsed time (no stored state)
- Exactly one corner touches the ground at any instant with zero velocity
- The square rotates 90° for each side-length of horizontal travel
- After traveling 4 side-lengths, it completes a full 360° rotation
- The motion wraps seamlessly when reaching canvas edges
- The contact point (red dot) shows which corner is currently touching the ground
The key insight is that during each quarter of the rolling cycle, the square pivots around the current contact corner, with the center tracing a quarter-circle arc around that point.