Here's a complete p5.js program in a single HTML file that simulates realistically bouncing balls affected by gravity inside a rotating square. The balls respond to collisions with the rotating square's walls, taking into account the rotation for accurate collision detection and response.
Features:
- Gravity pulls balls downward.
- Balls bounce off the rotating square's walls with realistic velocity changes.
- The square rotates smoothly around its center.
- Collision detection is rotation-aware using inverse rotation to transform coordinates.
✅ Full HTML File (Copy and paste into an .html file and open in a browser):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Rotating Square with Bouncing Balls</title>
<script src="https://cdn.jsdelivr.net/npm/p5@1.9.0/lib/p5.min.js"></script>
</head>
<body>
<script>
let balls = [];
const numBalls = 5;
const gravity = 0.3;
const squareSize = 400;
let angle = 0;
const rotationSpeed = 0.01;
function setup() {
createCanvas(600, 600);
for (let i = 0; i < numBalls; i++) {
balls.push(new Ball(random(-squareSize/2 + 20, squareSize/2 - 20),
random(-squareSize/2 + 20, squareSize/2 - 20),
random(-2, 2), random(-2, 2)));
}
}
function draw() {
background(30);
translate(width / 2, height / 2);
rotate(angle);
// Draw rotating square
noFill();
stroke(255);
strokeWeight(2);
rectMode(CENTER);
rect(0, 0, squareSize, squareSize);
// Update and draw balls
for (let ball of balls) {
ball.update();
ball.checkCollision();
ball.display();
}
angle += rotationSpeed;
}
class Ball {
constructor(x, y, vx, vy) {
this.pos = createVector(x, y);
this.vel = createVector(vx, vy);
this.radius = 10;
}
update() {
this.vel.y += gravity;
this.pos.add(this.vel);
}
checkCollision() {
// Inverse rotate the ball position and velocity to align with unrotated square
let invAngle = -angle;
let posRot = p5.Vector.fromAngle(invAngle).rotate(0); // dummy to get vector
let x = this.pos.x * cos(invAngle) - this.pos.y * sin(invAngle);
let y = this.pos.x * sin(invAngle) + this.pos.y * cos(invAngle);
let vx = this.vel.x * cos(invAngle) - this.vel.y * sin(invAngle);
let vy = this.vel.x * sin(invAngle) + this.vel.y * cos(invAngle);
// Check collision with square bounds
let half = squareSize / 2 - this.radius;
let collided = false;
if (x < -half) {
x = -half;
vx *= -1;
collided = true;
} else if (x > half) {
x = half;
vx *= -1;
collided = true;
}
if (y < -half) {
y = -half;
vy *= -1;
collided = true;
} else if (y > half) {
y = half;
vy *= -1;
collided = true;
}
if (collided) {
// Dampen velocity slightly to simulate energy loss
vx *= 0.98;
vy *= 0.98;
}
// Rotate position and velocity back to world coordinates
this.pos.x = x * cos(angle) - y * sin(angle);
this.pos.y = x * sin(angle) + y * cos(angle);
this.vel.x = vx * cos(angle) - vy * sin(angle);
this.vel.y = vx * sin(angle) + vy * cos(angle);
}
display() {
fill(200, 100, 255);
noStroke();
ellipse(this.pos.x, this.pos.y, this.radius * 2);
}
}
</script>
</body>
</html>
🧠 How It Works:
- The square rotates using
rotate(angle) around the canvas center.
- Each ball's position and velocity are transformed into the square's local (non-rotated) coordinate system using inverse rotation.
- Collision detection is done in this local space against the square's axis-aligned bounds.
- If a collision is detected, the velocity is reflected and slightly dampened.
- The position and velocity are then transformed back to the world (rotated) coordinate system.
🧪 Try It Out:
- Open the HTML file in a browser.
- Watch the balls bounce realistically inside the rotating square.
- You can tweak gravity, number of balls, or rotation speed for different effects.
Let me know if you'd like to add features like ball-to-ball collisions or user interaction!