<!DOCTYPE html>
<html>
<head>
<title>Hexagon Balls</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Constants
const gravity = 0.5;
const friction = 0.99;
const ballCount = 10;
const hexagons = [];
// Function to generate hexagon points
function generateHexagonPoints(radius, missingSideIndex) {
const points = [];
for (let i = 0; i < 6; i++) {
const angle = (i * Math.PI / 3) - Math.PI / 2; // Start from top
const x = radius * Math.cos(angle);
const y = radius * Math.sin(angle);
points.push({x, y});
}
return points;
}
// Function to create a hexagon with missing side
function createHexagon(cx, cy, radius, rotation, missingSideIndex, isRed = false) {
const points = generateHexagonPoints(radius, missingSideIndex);
const edges = [];
for (let i = 0; i < 6; i++) {
const a = i;
const b = (i + 1) % 6;
// Skip the missing side
if (i === missingSideIndex) continue;
edges.push({a: points[a], b: points[b]});
}
return {cx, cy, radius, rotation, edges, isRed, points, missingSideIndex};
}
// Initialize hexagons
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const hexagonRadii = [200, 150, 100, 50];
for (let i = 0; i < 4; i++) {
const radius = hexagonRadii[i];
const missingSideIndex = i % 6; // For simplicity, each hexagon missing a different side
const isRed = i === 0; // Only the first (outermost) is red
const hexagon = createHexagon(centerX, centerY, radius, 0, missingSideIndex, isRed);
hexagons.push(hexagon);
}
// Balls
const balls = [];
for (let i = 0; i < ballCount; i++) {
const radius = 10 + Math.random() * 10;
const x = centerX + (Math.random() - 0.5) * 100;
const y = centerY + (Math.random() - 0.5) * 100;
const vx = (Math.random() - 0.5) * 2;
const vy = (Math.random() - 0.5) * 2;
const color = `hsl(${Math.random() * 360}, 100%, 50%)`;
balls.push({x, y, vx, vy, radius, color, isReturning: false, returnTarget: {x: centerX, y: centerY}});
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update hexagons' rotation
for (let hex of hexagons) {
hex.rotation += 0.01 * (hex === hexagons[0] ? 1 : -1); // Alternate rotation directions
}
// Update balls
for (let ball of balls) {
// Apply gravity
ball.vy += gravity;
// Move ball
ball.x += ball.vx;
ball.y += ball.vy;
// Check for collision with hexagons
for (let hex of hexagons) {
for (let edge of hex.edges) {
// Get the global coordinates of edge's a and b
const a = edge.a;
const b = edge.b;
const cx = hex.cx;
const cy = hex.cy;
const rotation = hex.rotation;
// Rotate and translate the points
const aGlobalX = cx + a.x * Math.cos(rotation) - a.y * Math.sin(rotation);
const aGlobalY = cy + a.x * Math.sin(rotation) + a.y * Math.cos(rotation);
const bGlobalX = cx + b.x * Math.cos(rotation) - b.y * Math.sin(rotation);
const bGlobalY = cy + b.x * Math.sin(rotation) + b.y * Math.cos(rotation);
// Now check collision with the line segment from aGlobal to bGlobal
const distance = distanceToLineSegment(ball.x, ball.y, aGlobalX, aGlobalY, bGlobalX, bGlobalY);
if (distance < ball.radius) {
// Collision detected
// Check if it's the red edge
if (hex.isRed) {
// Trigger return animation
ball.isReturning = true;
ball.returnTarget = {x: centerX, y: centerY};
} else {
// Reflect velocity
const normal = getNormal({x: aGlobalX, y: aGlobalY}, {x: bGlobalX, y: bGlobalY});
const velocity = {x: ball.vx, y: ball.vy};
const reflected = reflectVelocity(velocity, normal);
ball.vx = reflected.x;
ball.vy = reflected.y;
}
}
}
}
// Handle return animation
if (ball.isReturning) {
const dx = ball.returnTarget.x - ball.x;
const dy = ball.returnTarget.y - ball.y;
const dist = Math.sqrt(dx*dx + dy*dy);
if (dist < 1) {
ball.isReturning = false;
} else {
ball.vx = (dx / dist) * 2;
ball.vy = (dy / dist) * 2;
}
}
// Apply friction
ball.vx *= friction;
ball.vy *= friction;
// Draw ball
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI*2);
ctx.fillStyle = ball.color;
ctx.fill();
}
// Draw hexagons
for (let hex of hexagons) {
ctx.save();
ctx.translate(hex.cx, hex.cy);
ctx.rotate(hex.rotation);
ctx.beginPath();
const points = hex.points;
const missingSideIndex = hex.missingSideIndex;
ctx.moveTo(points[0].x, points[0].y);
for (let i = 1; i < 6; i++) {
if (i - 1 === missingSideIndex) {
// Skip the side between i-1 and i
ctx.moveTo(points[i].x, points[i].y);
} else {
ctx.lineTo(points[i].x, points[i].y);
}
}
ctx.closePath();
if (hex.isRed) {
ctx.strokeStyle = 'red';
ctx.lineWidth = 4;
ctx.stroke();
} else {
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.stroke();
}
ctx.restore();
}
}
// Helper functions
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 t = dot / lenSq;
if (t < 0) {
t = 0;
} else if (t > 1) {
t = 1;
}
const closestX = x1 + t * C;
const closestY = y1 + t * D;
const dx = px - closestX;
const dy = py - closestY;
return Math.sqrt(dx * dx + dy * dy);
}
function getNormal(a, b) {
const dx = b.x - a.x;
const dy = b.y - a.y;
// Normal vector pointing to the right of the direction from a to b
return {x: -dy, y: dx};
}
function reflectVelocity(velocity, normal) {
const dotProduct = velocity.x * normal.x + velocity.y * normal.y;
const reflection = {
x: velocity.x - 2 * dotProduct * normal.x,
y: velocity.y - 2 * dotProduct * normal.y
};
return reflection;
}
animate();
</script>
</body>
</html>