Write a p5js program that shows a ball bouncing inside a spinning hexagon. The ball should be affected by gravity and friction and it must bounce off rotating walls realistically
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a p5.js program that creates a ball bouncing inside a spinning hexagon with realistic physics:
javascript
let hexagon;
let ball;
let gravity = 0.4;
let friction = 0.99;
let airResistance = 0.999;
function setup() {
createCanvas(800, 600);
// Initialize hexagon
hexagon = {
x: width / 2,
y: height / 2,
radius: 200,
rotation: 0,
rotationSpeed: 0.01
};
// Initialize ball
ball = {
x: width / 2,
y: height / 2 - 50,
vx: random(-5, 5),
vy: 0,
radius: 15,
restitution: 0.8 // Bounce dampening
};
}
function draw() {
background(20);
// Update hexagon rotation
hexagon.rotation += hexagon.rotationSpeed;
// Update ball physics
updateBall();
// Draw hexagon
push();
translate(hexagon.x, hexagon.y);
rotate(hexagon.rotation);
noFill();
stroke(100, 200, 255);
strokeWeight(3);
drawHexagon(0, 0, hexagon.radius);
pop();
// Draw ball
fill(255, 100, 100);
noStroke();
circle(ball.x, ball.y, ball.radius * 2);
}
function drawHexagon(x, y, radius) {
beginShape();
for (let i = 0; i < 6; i++) {
let angle = TWO_PI / 6 * i;
let vx = x + cos(angle) * radius;
let vy = y + sin(angle) * radius;
vertex(vx, vy);
}
endShape(CLOSE);
}
function updateBall() {
// Apply gravity
ball.vy += gravity;
// Apply air resistance
ball.vx *= airResistance;
ball.vy *= airResistance;
// Update position
ball.x += ball.vx;
ball.y += ball.vy;
// Check collision with hexagon walls
checkHexagonCollision();
}
function checkHexagonCollision() {
// Get hexagon vertices in world coordinates
let vertices = [];
for (let i = 0; i < 6; i++) {
let angle = TWO_PI / 6 * i + hexagon.rotation;
vertices.push({
x: hexagon.x + cos(angle) * hexagon.radius,
y: hexagon.y + sin(angle) * hexagon.radius
});
}
// Check each edge of the hexagon
for (let i = 0; i < 6; i++) {
let v1 = vertices[i];
let v2 = vertices[(i + 1) % 6];
// Calculate edge vector and normal
let edge = {
x: v2.x - v1.x,
y: v2.y - v1.y
};
// Normal points inward
let normal = {
x: -edge.y,
y: edge.x
};
// Normalize the normal vector
let normalLength = sqrt(normal.x * normal.x + normal.y * normal.y);
normal.x /= normalLength;
normal.y /= normalLength;
// Vector from edge start to ball center
let toBall = {
x: ball.x - v1.x,
y: ball.y - v1.y
};
// Project ball position onto edge
let edgeLength = sqrt(edge.x * edge.x + edge.y * edge.y);
let edgeNorm = {
x: edge.x / edgeLength,
y: edge.y / edgeLength
};
let projection = toBall.x * edgeNorm.x + toBall.y * edgeNorm.y;
projection = constrain(projection, 0, edgeLength);
// Find closest point on edge
let closestPoint = {
x: v1.x + edgeNorm.x * projection,
y: v1.y + edgeNorm.y * projection
};
// Check distance from ball to closest point
let dx = ball.x - closestPoint.x;
let dy = ball.y - closestPoint.y;
let distance = sqrt(dx * dx + dy * dy);
// If collision detected
if (distance < ball.radius) {
// Calculate collision normal (from edge to ball)
let collisionNormal = {
x: dx / distance,
y: dy / distance
};
// Separate ball from edge
let overlap = ball.radius - distance;
ball.x += collisionNormal.x * overlap;
ball.y += collisionNormal.y * overlap;
// Calculate relative velocity
let relativeVelocity = ball.vx * collisionNormal.x + ball.vy * collisionNormal.y;
// Only bounce if moving towards the wall
if (relativeVelocity < 0) {
// Calculate impulse
let impulse = -relativeVelocity * (1 + ball.restitution);
// Apply impulse
ball.vx += impulse * collisionNormal.x;
ball.vy += impulse * collisionNormal.y;
// Apply friction to tangential velocity
let tangent = {
x: -collisionNormal.y,
y: collisionNormal.x
};
let tangentialVelocity = ball.vx * tangent.x + ball.vy * tangent.y;
ball.vx -= tangentialVelocity * (1 - friction) * tangent.x;
ball.vy -= tangentialVelocity * (1 - friction) * tangent.y;
}
}
}
}
// Add mouse interaction to change rotation speed
function mouseMoved() {
hexagon.rotationSpeed = map(mouseX, 0, width, -0.05, 0.05);
}
// Click to reset ball position
function mousePressed() {
ball.x = width / 2;
ball.y = height / 2 - 50;
ball.vx = random(-5, 5);
ball.vy = 0;
}
This program creates a realistic bouncing ball simulation with the following features:
Spinning Hexagon: The hexagon rotates continuously, and you can control the rotation speed by moving your mouse horizontally across the canvas.
Gravity: The ball is constantly pulled downward with a gravity force.
Friction: When the ball hits a wall, friction is applied to its tangential velocity, causing it to lose some speed along the wall surface.
Air Resistance: A small amount of air resistance is applied to make the motion more realistic.
Realistic Bouncing: The collision detection calculates the exact point of contact and applies proper physics for bouncing, including:
Normal force calculation based on the collision angle
Restitution (bounce dampening) to make bounces less than perfectly elastic
Proper velocity reflection based on the wall angle
Interactive Controls:
Move your mouse horizontally to control the hexagon's rotation speed
Click anywhere to reset the ball to its starting position
The collision detection works by checking each edge of the hexagon, finding the closest point on each edge to the ball, and determining if a collision has occurred. When a collision is detected, the ball's velocity is adjusted based on the collision normal and the physics parameters.