Here's a single HTML file that simulates the scenario of a small ball being released from the center of a rotating hexagon. The simulation includes:
- A rotating regular hexagon.
- A ball starting at the center of the hexagon and falling due to gravity.
- Elastic collisions between the ball and the edges of the hexagon.
- Basic 2D physics (gravity, velocity, position updates).
- Collision detection using polygon edge normals.
This is implemented using JavaScript and the <canvas> element. It’s a simplified 2D simulation assuming no rotation on the ball and perfect elasticity.
✅ Save this as hexagon_ball_simulation.html and open in your browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Ball in Rotating Hexagon</title>
<style>
body { margin: 0; overflow: hidden; background: #111; }
canvas { display: block; background: #222; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let width, height;
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
// Physics constants
const gravity = 0.5;
const ballRadius = 8;
const hexSize = 200;
// Ball properties
let ballPos = { x: 0, y: 0 };
let ballVel = { x: 0, y: 0 };
// Rotation variables
let angle = 0;
let angularSpeed = 0.01; // radians per frame
// Hexagon vertices
function getHexagonVertices(cx, cy, size, angleOffset) {
const vertices = [];
for (let i = 0; i < 6; i++) {
const theta = (Math.PI / 3) * i + angleOffset;
vertices.push({
x: cx + size * Math.cos(theta),
y: cy + size * Math.sin(theta)
});
}
return vertices;
}
// Line intersection helper
function dot(a, b) {
return a.x * b.x + a.y * b.y;
}
function reflect(velocity, normal) {
const dotProduct = dot(velocity, normal);
return {
x: velocity.x - 2 * dotProduct * normal.x,
y: velocity.y - 2 * dotProduct * normal.y
};
}
function normalize(v) {
const len = Math.hypot(v.x, v.y);
return { x: v.x / len, y: v.y / len };
}
function lineNormal(p1, p2) {
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
return normalize({ x: dy, y: -dx }); // perpendicular
}
// Point-in-polygon check
function pointInPolygon(point, polygon) {
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const xi = polygon[i].x;
const yi = polygon[i].y;
const xj = polygon[j].x;
const yj = polygon[j].y;
const intersect = ((yi > point.y) !== (yj > point.y)) &&
(point.x < (xj - xi) * (point.y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
// Main update function
function update() {
// Clear canvas
ctx.clearRect(0, 0, width, height);
const centerX = width / 2;
const centerY = height / 2;
// Update hexagon rotation
angle += angularSpeed;
// Get current hexagon vertices
const hexVertices = getHexagonVertices(centerX, centerY, hexSize, angle);
// Initialize ball at center only once
if (ballPos.x === 0 && ballPos.y === 0) {
ballPos = { x: centerX, y: centerY };
}
// Apply gravity
ballVel.y += gravity;
// Move ball
const nextPos = {
x: ballPos.x + ballVel.x,
y: ballPos.y + ballVel.y
};
// Check collision with each edge
for (let i = 0; i < hexVertices.length; i++) {
const p1 = hexVertices[i];
const p2 = hexVertices[(i + 1) % hexVertices.length];
const edgeNormal = lineNormal(p1, p2);
// Distance from ball to edge
const distToEdge = (edgeNormal.x * (nextPos.x - p1.x) + edgeNormal.y * (nextPos.y - p1.y));
if (distToEdge < ballRadius) {
// Reflect velocity
ballVel = reflect(ballVel, edgeNormal);
// Move ball just outside the edge
const penetration = ballRadius - distToEdge;
nextPos.x += edgeNormal.x * penetration;
nextPos.y += edgeNormal.y * penetration;
}
}
// Update ball position
ballPos = nextPos;
// Redraw hexagon
ctx.beginPath();
ctx.moveTo(hexVertices[0].x, hexVertices[0].y);
for (let i = 1; i < hexVertices.length; i++) {
ctx.lineTo(hexVertices[i].x, hexVertices[i].y);
}
ctx.closePath();
ctx.strokeStyle = '#0ff';
ctx.lineWidth = 2;
ctx.stroke();
// Draw ball
ctx.beginPath();
ctx.arc(ballPos.x, ballPos.y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>
🔍 Features Included:
- Gravity: Pulls the ball downward continuously.
- Collision Detection: Detects when the ball intersects a hexagon edge.
- Elastic Reflection: Ball reflects off edges with conservation of kinetic energy.
- Hexagon Rotation: The hexagon rotates slowly over time.
- Canvas Rendering: All drawing is done directly on an HTML canvas.
🧪 Tips for Experimentation:
- Adjust
angularSpeed to change how fast the hexagon rotates.
- Modify
gravity to make it stronger or weaker.
- You can also add friction or spin by extending the physics logic.
Let me know if you'd like enhancements like trail effects, multiple balls, or sound!