I'll create an interactive HTML file with nested rotating hexagons and bouncing balls. Here's the complete implementation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nested Hexagons with Bouncing Balls</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #1a1a1a;
overflow: hidden;
}
canvas {
border: 2px solid #333;
background-color: #0a0a0a;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 800;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Hexagon configurations
const hexagons = [
{ radius: 350, rotation: 0, rotationSpeed: 0.005, color: '#ff0000', lineWidth: 8 },
{ radius: 280, rotation: 0, rotationSpeed: -0.008, color: '#00ff00', lineWidth: 6 },
{ radius: 210, rotation: 0, rotationSpeed: 0.011, color: '#0088ff', lineWidth: 4 },
{ radius: 140, rotation: 0, rotationSpeed: -0.014, color: '#ffff00', lineWidth: 3 }
];
// Ball class
class Ball {
constructor() {
this.reset();
this.color = `hsl(${Math.random() * 360}, 70%, 50%)`;
this.radius = 8 + Math.random() * 7;
this.vx = (Math.random() - 0.5) * 4;
this.vy = (Math.random() - 0.5) * 4;
this.isResetting = false;
this.resetProgress = 0;
this.startX = this.x;
this.startY = this.y;
}
reset() {
this.x = centerX + (Math.random() - 0.5) * 50;
this.y = centerY + (Math.random() - 0.5) * 50;
this.isResetting = false;
this.resetProgress = 0;
}
startReset() {
if (!this.isResetting) {
this.isResetting = true;
this.resetProgress = 0;
this.startX = this.x;
this.startY = this.y;
}
}
update() {
if (this.isResetting) {
this.resetProgress += 0.03;
if (this.resetProgress >= 1) {
this.reset();
this.vx = (Math.random() - 0.5) * 4;
this.vy = (Math.random() - 0.5) * 4;
} else {
// Smooth animation to center
const t = this.resetProgress;
const easeT = t * t * (3 - 2 * t); // Smooth step function
this.x = this.startX + (centerX - this.startX) * easeT;
this.y = this.startY + (centerY - this.startY) * easeT;
}
return;
}
// Apply gravity
this.vy += 0.15;
// Update position
this.x += this.vx;
this.y += this.vy;
// Apply some damping
this.vx *= 0.999;
this.vy *= 0.999;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
ctx.lineWidth = 2;
ctx.stroke();
}
}
// Create balls
const balls = [];
for (let i = 0; i < 15; i++) {
balls.push(new Ball());
}
// Get hexagon vertices (missing one side)
function getHexagonVertices(centerX, centerY, radius, rotation) {
const vertices = [];
for (let i = 0; i < 5; i++) { // Only 5 vertices for open hexagon
const angle = rotation + (i * Math.PI / 3) - Math.PI / 2;
vertices.push({
x: centerX + radius * Math.cos(angle),
y: centerY + radius * Math.sin(angle)
});
}
return vertices;
}
// Check if point is on the left side of a line
function isLeftOfLine(px, py, x1, y1, x2, y2) {
return ((x2 - x1) * (py - y1) - (y2 - y1) * (px - x1)) > 0;
}
// Get distance from point to line segment
function distanceToLineSegment(px, py, x1, y1, x2, y2) {
const A = px - x1;
const B = py - y1;
const C = x2 - x1;
const D = y2 - y1;
const dot = A * C + B * D;
const lenSq = C * C + D * D;
let param = -1;
if (lenSq !== 0) param = dot / lenSq;
let xx, yy;
if (param < 0) {
xx = x1;
yy = y1;
} else if (param > 1) {
xx = x2;
yy = y2;
} else {
xx = x1 + param * C;
yy = y1 + param * D;
}
const dx = px - xx;
const dy = py - yy;
return Math.sqrt(dx * dx + dy * dy);
}
// Check collision with hexagon walls
function checkHexagonCollision(ball, hexagon, hexIndex) {
const vertices = getHexagonVertices(centerX, centerY, hexagon.radius, hexagon.rotation);
for (let i = 0; i < vertices.length - 1; i++) {
const v1 = vertices[i];
const v2 = vertices[i + 1];
const dist = distanceToLineSegment(ball.x, ball.y, v1.x, v1.y, v2.x, v2.y);
if (dist < ball.radius) {
// Calculate normal vector
const dx = v2.x - v1.x;
const dy = v2.y - v1.y;
const len = Math.sqrt(dx * dx + dy * dy);
const nx = -dy / len;
const ny = dx / len;
// Make sure normal points inward
const centerDir = {
x: centerX - (v1.x + v2.x) / 2,
y: centerY - (v1.y + v2.y) / 2
};
if (nx * centerDir.x + ny * centerDir.y < 0) {
nx *= -1;
ny *= -1;
}
// Reflect velocity
const dot = ball.vx * nx + ball.vy * ny;
ball.vx -= 2 * dot * nx;
ball.vy -= 2 * dot * ny;
// Push ball away from wall
const overlap = ball.radius - dist;
ball.x += nx * overlap;
ball.y += ny * overlap;
// Add some bounce
ball.vx *= 0.8;
ball.vy *= 0.8;
}
}
// Check collision with the missing side (red wall for outermost hexagon)
if (hexIndex === 0) {
const v1 = vertices[vertices.length - 1];
const v2 = vertices[0];
const dist = distanceToLineSegment(ball.x, ball.y, v1.x, v1.y, v2.x, v2.y);
if (dist < ball.radius + hexagon.lineWidth / 2) {
ball.startReset();
}
}
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update hexagon rotations
hexagons.forEach(hex => {
hex.rotation += hex.rotationSpeed;
});
// Draw hexagons
hexagons.forEach((hexagon, index) => {
const vertices = getHexagonVertices(centerX, centerY, hexagon.radius, hexagon.rotation);
ctx.beginPath();
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.strokeStyle = hexagon.color;
ctx.lineWidth = hexagon.lineWidth;
ctx.stroke();
// Draw the missing side for the outermost hexagon in red
if (index === 0) {
ctx.beginPath();
ctx.moveTo(vertices[vertices.length - 1].x, vertices[vertices.length - 1].y);
ctx.lineTo(vertices[0].x, vertices[0].y);
ctx.strokeStyle = '#ff0000';
ctx.lineWidth = hexagon.lineWidth;
ctx.stroke();
}
});
// Update and draw balls
balls.forEach(ball => {
ball.update();
// Check collisions with all hexagons
if (!ball.isResetting) {
for (let i = 0; i < hexagons.length; i++) {
checkHexagonCollision(ball, hexagons[i], i);
}
// Keep balls within the outermost hexagon
const distFromCenter = Math.sqrt(
Math.pow(ball.x - centerX, 2) +
Math.pow(ball.y - centerY, 2)
);
if (distFromCenter + ball.radius > hexagons[0].radius) {
const angle = Math.atan2(ball.y - centerY, ball.x - centerX);
ball.x = centerX + (hexagons[0].radius - ball.radius) * Math.cos(angle);
ball.y = centerY + (hexagons[0].radius - ball.radius) * Math.sin(angle);
// Reflect velocity
const normal = {
x: Math.cos(angle),
y: Math.sin(angle)
};
const dot = ball.vx * normal.x + ball.vy * normal.y;
ball.vx -= 2 * dot * normal.x;
ball.vy -= 2 * dot * normal.y;
ball.vx *= 0.8;
ball.vy *= 0.8;
}
}
ball.draw();
});
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
This implementation features:
- Four nested hexagons - Each missing one side and rotating in opposite directions at different speeds
- Multi-colored balls - 15 balls with random colors that bounce around inside the hexagons
- Gravity - Balls are affected by downward gravity
- Collision detection - Balls bounce off the walls of all hexagons
- Red outer wall - The missing side of the outermost hexagon is drawn in solid red
- Reset animation - When a ball touches the red wall, it smoothly animates back to the center
- Realistic physics - Includes velocity damping and proper collision response
The balls will bounce around inside the nested hexagons, affected by gravity, and if they touch the red outer wall, they'll be smoothly animated back to the center before resuming their motion.