Below is an HTML file that simulates a small ball released from the center of a rotating hexagon. The simulation considers collisions between the ball and the hexagon's edges, gravity acting on the ball, and assumes perfectly elastic collisions. The simulation is implemented using JavaScript and rendered with HTML5 Canvas.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rotating Hexagon Ball Simulation</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="simulationCanvas" width="600" height="600"></canvas>
<script>
const canvas = document.getElementById('simulationCanvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const centerX = width / 2;
const centerY = height / 2;
const hexagonRadius = 150;
const ballRadius = 5;
const gravity = 0.2;
const ballSpeed = 2;
const rotationSpeed = 0.03;
let ball = {
x: centerX,
y: centerY,
vx: ballSpeed,
vy: ballSpeed,
radius: ballRadius
};
let angle = 0;
function drawHexagon(centerX, centerY, radius, angle) {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
const theta = (Math.PI / 3) * i + angle;
const x = centerX + radius * Math.cos(theta);
const y = centerY + radius * Math.sin(theta);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.closePath();
ctx.strokeStyle = '#000';
ctx.stroke();
}
function drawBall(x, y, radius) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = '#f00';
ctx.fill();
}
function rotatePoint(x, y, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return {
x: x * cos - y * sin,
y: x * sin + y * cos
};
}
function checkCollision(ball, hexagonRadius, angle) {
const relativePos = rotatePoint(ball.x - centerX, ball.y - centerY, -angle);
const distFromCenter = Math.sqrt(relativePos.x ** 2 + relativePos.y ** 2);
const hexEdgeDistance = Math.abs(relativePos.y) * 2 / Math.sqrt(3);
if (distFromCenter > hexagonRadius - ball.radius) {
const normal = {
x: relativePos.x / distFromCenter,
y: relativePos.y / distFromCenter
};
ball.vx -= 2 * (ball.vx * normal.x + ball.vy * normal.y) * normal.x;
ball.vy -= 2 * (ball.vx * normal.x + ball.vy * normal.y) * normal.y;
const overlap = (hexagonRadius - ball.radius - distFromCenter) * 0.9;
ball.x += normal.x * overlap;
ball.y += normal.y * overlap;
}
}
function update() {
ctx.clearRect(0, 0, width, height);
angle += rotationSpeed;
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
checkCollision(ball, hexagonRadius, angle);
if (ball.y + ballRadius > height) {
ball.y = height - ballRadius;
ball.vy *= -0.9;
}
drawHexagon(centerX, centerY, hexagonRadius, angle);
drawBall(ball.x, ball.y, ball.radius);
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>
Explanation:
- HTML Structure: The file contains a single
<canvas> element where the simulation is rendered.
- CSS: Centers the canvas on the page and adds a border for visibility.
- JavaScript:
- Hexagon Drawing: The hexagon is drawn using its geometric properties and rotated based on the
angle variable.
- Ball Drawing: The ball is drawn as a red circle.
- Collision Detection: Checks if the ball collides with the hexagon's edges using rotated coordinates.
- Physics: Applies gravity to the ball and handles collisions with the hexagon and the ground.
- Animation Loop: Uses
requestAnimationFrame to continuously update and render the simulation.
This simulation assumes perfectly elastic collisions and simple gravity. The ball bounces off the hexagon's edges and the ground, while the hexagon rotates continuously.