Write a p5.js program (in one HTML file) that simulates a few realistically bouncing…
- User
Write a p5.js program (in one HTML file) that simulates a few realistically bouncing balls affected by gravity inside a square that rotates around its center. The balls should respond to collisions with the rotating square's walls, maintaining physical realism with velocity changes, gravity effects, and rotation-aware collision detection
- Assistant
Here’s a single‐HTML p5.js sketch that drops a bunch of balls into a square box rotating around its center. Collisions are handled in the rotating frame so that gravity, velocity reflection, and restitution all behave “physically” realistic. Enjoy! 🏀🔄
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Rotating Box with Bouncing Balls</title> <!-- p5.js CDN --> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.8.0/p5.js"></script> </head> <body> <script> let balls = []; let numBalls = 12; let boxSize = 400; let boxAngle = 0; let boxAngVel = 0.01; // radians per frame let gravity; function setup() { createCanvas(600, 600); gravity = createVector(0, 0.3); // initialize balls with random positions & velocities for (let i = 0; i < numBalls; i++) { let r = random(12, 20); let pos = createVector( width/2 + random(-boxSize/4, boxSize/4), height/2 + random(-boxSize/4, boxSize/4) ); let vel = p5.Vector.random2D().mult(random(1, 3)); balls.push(new Ball(pos, vel, r)); } noStroke(); } function draw() { background(30); // update rotation boxAngle += boxAngVel; // draw rotating box push(); translate(width/2, height/2); rotate(boxAngle); stroke(200); strokeWeight(2); noFill(); rectMode(CENTER); rect(0, 0, boxSize, boxSize); pop(); // update and draw each ball for (let b of balls) { b.applyForce(gravity); b.update(); b.checkCollision(); b.display(); } } // Ball class class Ball { constructor(pos, vel, r) { this.pos = pos.copy(); this.vel = vel.copy(); this.r = r; this.restitution = 0.9; // energy loss on bounce } applyForce(f) { this.vel.add(f); } update() { this.pos.add(this.vel); } checkCollision() { // 1) translate to box‐centered coords let c = createVector(width/2, height/2); let rel = p5.Vector.sub(this.pos, c); // 2) rotate by -boxAngle to go into box frame let ca = cos(-boxAngle), sa = sin(-boxAngle); let local = createVector( rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca ); // walls are at ±half = ±(boxSize/2) let half = boxSize/2 - this.r; let collidedX = false, collidedY = false; // check X‐walls if (local.x > half) { local.x = half; collidedX = true; } else if (local.x < -half) { local.x = -half; collidedX = true; } // check Y‐walls if (local.y > half) { local.y = half; collidedY = true; } else if (local.y < -half) { local.y = -half; collidedY = true; } if (collidedX || collidedY) { // rotate velocity into box frame let lv = createVector( this.vel.x * ca - this.vel.y * sa, this.vel.x * sa + this.vel.y * ca ); // reflect appropriate component if (collidedX) lv.x *= -this.restitution; if (collidedY) lv.y *= -this.restitution; // rotate back to world frame this.vel.set( lv.x * ca + lv.y * sa, -lv.x * sa + lv.y * ca ); } // 3) rotate local position back & translate to world let ca2 = cos(boxAngle), sa2 = sin(boxAngle); let world = createVector( local.x * ca2 - local.y * sa2, local.x * sa2 + local.y * ca2 ).add(c); this.pos = world; } display() { fill(200, 100, 220, 200); noStroke(); circle(this.pos.x, this.pos.y, this.r * 2); } } </script> </body> </html>How it works:
- Every frame we rotate the box by a small angular velocity.
- Each ball feels a constant gravity force, then updates its position.
- For collision detection we:
- Translate the ball into the box’s local coordinates,
- Rotate by –boxAngle to “freeze” the box horizontally/vertically,
- Check & resolve collisions with the 4 static walls (reflecting the local velocity),
- Rotate back & translate to world space.
Enjoy playing with the gravity, restitution, or angular speed to get different effects! 🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.