Certainly! Below is an HTML file that simulates the scenario of a small ball released from the center of a rotating hexagon. It includes the collision between the ball and the hexagon's edges, gravity acting on the ball, and assumes all collisions are perfectly elastic.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ball in Rotating Hexagon</title>
<style>
canvas {
display: block;
margin: 0 auto;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<canvas id="simulationCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('simulationCanvas');
const ctx = canvas.getContext('2d');
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 10,
vx: 0,
vy: 0,
color: 'red'
};
const hexagon = {
centerX: canvas.width / 2,
centerY: canvas.height / 2,
radius: 150,
rotation: 0,
rotationSpeed: Math.PI / 180, // 1 degree per frame
color: 'blue'
};
const gravity = 0.5;
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.fill();
ctx.closePath();
}
function drawHexagon() {
ctx.save();
ctx.translate(hexagon.centerX, hexagon.centerY);
ctx.rotate(hexagon.rotation);
ctx.beginPath();
for (let i = 0; i < 6; i++) {
const angle = Math.PI / 3 * i;
const x = hexagon.radius * Math.cos(angle);
const y = hexagon.radius * Math.sin(angle);
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.fillStyle = hexagon.color;
ctx.fill();
ctx.closePath();
ctx.restore();
}
function checkCollision() {
ctx.save();
ctx.translate(hexagon.centerX, hexagon.centerY);
ctx.rotate(hexagon.rotation);
const ballX = ball.x - hexagon.centerX;
const ballY = ball.y - hexagon.centerY;
let minDistance = Infinity;
let closestPoint = { x: 0, y: 0 };
for (let i = 0; i < 6; i++) {
const angle1 = Math.PI / 3 * i;
const angle2 = Math.PI / 3 * (i + 1);
const x1 = hexagon.radius * Math.cos(angle1);
const y1 = hexagon.radius * Math.sin(angle1);
const x2 = hexagon.radius * Math.cos(angle2);
const y2 = hexagon.radius * Math.sin(angle2);
const distance = distanceToLine(ballX, ballY, x1, y1, x2, y2);
if (distance < minDistance) {
minDistance = distance;
closestPoint = { x: x1, y: y1 };
}
}
if (minDistance < ball.radius) {
const normalX = (closestPoint.x - ballX) / minDistance;
const normalY = (closestPoint.y - ballY) / minDistance;
const relativeVelocityX = ball.vx;
const relativeVelocityY = ball.vy;
const dotProduct = relativeVelocityX * normalX + relativeVelocityY * normalY;
const impulse = 2 * dotProduct;
ball.vx -= impulse * normalX;
ball.vy -= impulse * normalY;
}
ctx.restore();
}
function distanceToLine(px, py, x1, y1, x2, y2) {
const A = y2 - y1;
const B = x1 - x2;
const C = x2 * y1 - x1 * y2;
return Math.abs(A * px + B * py + C) / Math.sqrt(A * A + B * B);
}
function update() {
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
hexagon.rotation += hexagon.rotationSpeed;
checkCollision();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawHexagon();
drawBall();
}
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
This code creates a canvas where a small red ball is released from the center of a rotating blue hexagon. The ball is affected by gravity and will collide with the hexagon's edges in a perfectly elastic manner. The hexagon rotates continuously, and the ball's motion is updated in each frame of the animation.