I'll create an HTML program with a bouncing ball inside a spinning hexagon. This simulation includes gravity, friction, and realistic collision detection with the rotating walls.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ball in Spinning Hexagon</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #1a1a1a;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #444;
background-color: #000;
}
.controls {
position: absolute;
top: 20px;
left: 20px;
color: white;
background: rgba(0, 0, 0, 0.7);
padding: 15px;
border-radius: 5px;
}
.controls label {
display: block;
margin-bottom: 10px;
}
.controls input {
width: 150px;
margin-left: 10px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="controls">
<label>
Rotation Speed:
<input type="range" id="rotationSpeed" min="-5" max="5" step="0.1" value="1">
<span id="rotationValue">1</span>
</label>
<label>
Gravity:
<input type="range" id="gravity" min="0" max="2" step="0.1" value="0.5">
<span id="gravityValue">0.5</span>
</label>
<label>
Friction:
<input type="range" id="friction" min="0.9" max="1" step="0.01" value="0.98">
<span id="frictionValue">0.98</span>
</label>
<label>
Bounce Damping:
<input type="range" id="bounceDamping" min="0.5" max="1" step="0.05" value="0.8">
<span id="bounceDampingValue">0.8</span>
</label>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Set canvas size
canvas.width = 800;
canvas.height = 600;
// Hexagon properties
const hexagon = {
centerX: canvas.width / 2,
centerY: canvas.height / 2,
radius: 200,
rotation: 0,
rotationSpeed: 0.01
};
// Ball properties
const ball = {
x: hexagon.centerX,
y: hexagon.centerY - 50,
radius: 15,
vx: 3,
vy: 0,
color: '#ff6b6b',
trail: []
};
// Physics properties
let gravity = 0.5;
let friction = 0.98;
let bounceDamping = 0.8;
// Get hexagon vertices
function getHexagonVertices(centerX, centerY, radius, rotation) {
const vertices = [];
for (let i = 0; i < 6; i++) {
const angle = rotation + (Math.PI / 3) * i;
vertices.push({
x: centerX + radius * Math.cos(angle),
y: centerY + radius * Math.sin(angle)
});
}
return vertices;
}
// Check if point is inside hexagon
function isPointInsideHexagon(px, py, vertices) {
let inside = false;
for (let i = 0, j = vertices.length - 1; i < vertices.length; j = i++) {
const xi = vertices[i].x, yi = vertices[i].y;
const xj = vertices[j].x, yj = vertices[j].y;
const intersect = ((yi > py) !== (yj > py))
&& (px < (xj - xi) * (py - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
// Get closest point on line segment
function closestPointOnSegment(px, py, x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
const t = Math.max(0, Math.min(1, ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy)));
return {
x: x1 + t * dx,
y: y1 + t * dy
};
}
// Handle collision with hexagon walls
function handleCollision() {
const vertices = getHexagonVertices(hexagon.centerX, hexagon.centerY, hexagon.radius, hexagon.rotation);
for (let i = 0; i < vertices.length; i++) {
const v1 = vertices[i];
const v2 = vertices[(i + 1) % vertices.length];
// Find closest point on edge to ball center
const closest = closestPointOnSegment(ball.x, ball.y, v1.x, v1.y, v2.x, v2.y);
// Calculate distance from ball center to closest point
const dx = ball.x - closest.x;
const dy = ball.y - closest.y;
const distance = Math.sqrt(dx * dx + dy * dy);
// Check if collision occurred
if (distance < ball.radius) {
// Calculate normal vector
const normal = {
x: dx / distance,
y: dy / distance
};
// Move ball outside of wall
ball.x = closest.x + normal.x * ball.radius;
ball.y = closest.y + normal.y * ball.radius;
// Calculate relative velocity
const relativeVelocity = ball.vx * normal.x + ball.vy * normal.y;
// Only bounce if moving towards wall
if (relativeVelocity < 0) {
// Apply bounce with damping
ball.vx -= 2 * relativeVelocity * normal.x;
ball.vy -= 2 * relativeVelocity * normal.y;
ball.vx *= bounceDamping;
ball.vy *= bounceDamping;
// Add rotation effect from spinning hexagon
const tangent = { x: -normal.y, y: normal.x };
const rotationEffect = hexagon.rotationSpeed * hexagon.radius * 0.1;
ball.vx += tangent.x * rotationEffect;
ball.vy += tangent.y * rotationEffect;
}
}
}
}
// Update physics
function update() {
// Apply gravity
ball.vy += gravity;
// Apply friction
ball.vx *= friction;
ball.vy *= friction;
// Update position
ball.x += ball.vx;
ball.y += ball.vy;
// Handle collisions
handleCollision();
// Rotate hexagon
hexagon.rotation += hexagon.rotationSpeed;
// Update trail
ball.trail.push({ x: ball.x, y: ball.y });
if (ball.trail.length > 20) {
ball.trail.shift();
}
}
// Draw everything
function draw() {
// Clear canvas
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw hexagon
ctx.strokeStyle = '#4ecdc4';
ctx.lineWidth = 3;
ctx.beginPath();
const vertices = getHexagonVertices(hexagon.centerX, hexagon.centerY, hexagon.radius, hexagon.rotation);
ctx.moveTo(vertices[0].x, vertices[0].y);
for (let i = 1; i < vertices.length; i++) {
ctx.lineTo(vertices[i].x, vertices[i].y);
}
ctx.closePath();
ctx.stroke();
// Draw ball trail
ctx.strokeStyle = 'rgba(255, 107, 107, 0.3)';
ctx.lineWidth = 2;
ctx.beginPath();
for (let i = 0; i < ball.trail.length; i++) {
if (i === 0) {
ctx.moveTo(ball.trail[i].x, ball.trail[i].y);
} else {
ctx.lineTo(ball.trail[i].x, ball.trail[i].y);
}
}
ctx.stroke();
// Draw ball
ctx.fillStyle = ball.color;
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fill();
// Add glow effect
ctx.shadowBlur = 20;
ctx.shadowColor = ball.color;
ctx.fill();
ctx.shadowBlur = 0;
}
// Animation loop
function animate() {
update();
draw();
requestAnimationFrame(animate);
}
// Control handlers
document.getElementById('rotationSpeed').addEventListener('input', (e) => {
hexagon.rotationSpeed = parseFloat(e.target.value) * 0.01;
document.getElementById('rotationValue').textContent = e.target.value;
});
document.getElementById('gravity').addEventListener('input', (e) => {
gravity = parseFloat(e.target.value);
document.getElementById('gravityValue').textContent = e.target.value;
});
document.getElementById('friction').addEventListener('input', (e) => {
friction = parseFloat(e.target.value);
document.getElementById('frictionValue').textContent = e.target.value;
});
document.getElementById('bounceDamping').addEventListener('input', (e) => {
bounceDamping = parseFloat(e.target.value);
document.getElementById('bounceDampingValue').textContent = e.target.value;
});
// Click to reset ball position
canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect();
ball.x = e.clientX - rect.left;
ball.y = e.clientY - rect.top;
ball.vx = (Math.random() - 0.5) * 10;
ball.vy = (Math.random() - 0.5) * 10;
ball.trail = [];
});
// Start animation
animate();
</script>
</body>
</html>
This program creates a realistic physics simulation with the following features:
-
Spinning Hexagon: The hexagon rotates continuously, and you can control its rotation speed with the slider (including reverse rotation).
-
Realistic Physics:
- Gravity pulls the ball downward
- Friction gradually slows the ball's movement
- Bounce damping reduces velocity after each collision
-
Collision Detection: The program accurately detects when the ball hits any of the hexagon's rotating walls and calculates the proper bounce angle based on the wall's orientation.
-
Interactive Controls:
- Adjust rotation speed (negative values spin counterclockwise)
- Modify gravity strength
- Change friction coefficient
- Control bounce damping
-
Visual Effects:
- Ball trail shows recent movement path
- Glow effect on the ball
- Smooth fade effect for the trail
-
Interactivity: Click anywhere on the canvas to reset the ball's position with a random velocity.
The collision detection accounts for the hexagon's rotation, and the ball inherits some momentum from the spinning walls when it bounces, creating a more realistic simulation. The physics calculations ensure the ball behaves naturally within the rotating container.