Certainly! Below is a simple HTML page that renders a rotating octagon with a red ball inside it. The ball is affected by gravity and bounces inside the rotating octagon.
This example uses the HTML5 <canvas> element and JavaScript for animation and physics simulation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Rotating Octagon with Gravity Ball</title>
<style>
body, html {
margin: 0; padding: 0; overflow: hidden; background: #222;
display: flex; justify-content: center; align-items: center; height: 100vh;
}
canvas {
background: #111;
display: block;
}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
<script>
(() => {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const W = canvas.width;
const H = canvas.height;
// Octagon parameters
const octagonRadius = 200;
const octagonCenter = { x: W / 2, y: H / 2 };
let rotation = 0;
const rotationSpeed = 0.01; // radians per frame
// Ball parameters
const ballRadius = 15;
let ball = {
x: octagonCenter.x,
y: octagonCenter.y - octagonRadius / 2,
vx: 2,
vy: 0,
ay: 0.3, // gravity acceleration
};
// Calculate octagon vertices
function getOctagonVertices(cx, cy, r, angle = 0) {
const vertices = [];
for (let i = 0; i < 8; i++) {
const theta = (Math.PI / 4) * i + angle;
vertices.push({
x: cx + r * Math.cos(theta),
y: cy + r * Math.sin(theta),
});
}
return vertices;
}
// Draw octagon
function drawOctagon(vertices) {
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.closePath();
ctx.strokeStyle = 'white';
ctx.lineWidth = 3;
ctx.stroke();
}
// Draw ball
function drawBall(ball) {
ctx.beginPath();
ctx.fillStyle = 'red';
ctx.arc(ball.x, ball.y, ballRadius, 0, Math.PI * 2);
ctx.fill();
}
// Check if point is inside polygon (ray-casting algorithm)
function pointInPolygon(point, polygon) {
let x = point.x, y = point.y;
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
let xi = polygon[i].x, yi = polygon[i].y;
let xj = polygon[j].x, yj = polygon[j].y;
let intersect = ((yi > y) !== (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
// Find closest point on polygon edge to a point
function closestPointOnSegment(px, py, x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
if (dx === 0 && dy === 0) return { x: x1, y: y1 };
const t = ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy);
if (t < 0) return { x: x1, y: y1 };
else if (t > 1) return { x: x2, y: y2 };
return { x: x1 + t * dx, y: y1 + t * dy };
}
// Reflect velocity vector based on normal
function reflectVelocity(vx, vy, nx, ny) {
// Normalize normal vector
const length = Math.sqrt(nx * nx + ny * ny);
nx /= length;
ny /= length;
// Dot product
const dot = vx * nx + vy * ny;
// Reflect
return {
vx: vx - 2 * dot * nx,
vy: vy - 2 * dot * ny,
};
}
function update() {
// Update rotation
rotation += rotationSpeed;
// Get rotated octagon vertices
const vertices = getOctagonVertices(octagonCenter.x, octagonCenter.y, octagonRadius, rotation);
// Apply gravity
ball.vy += ball.ay;
ball.x += ball.vx;
ball.y += ball.vy;
// Check collision with octagon edges
// If ball center is outside polygon, find closest edge and reflect velocity
if (!pointInPolygon({ x: ball.x, y: ball.y }, vertices)) {
// Find closest edge and normal
let closestDist = Infinity;
let closestNormal = null;
let closestPoint = null;
for (let i = 0; i < vertices.length; i++) {
const v1 = vertices[i];
const v2 = vertices[(i + 1) % vertices.length];
const cp = closestPointOnSegment(ball.x, ball.y, v1.x, v1.y, v2.x, v2.y);
const dx = ball.x - cp.x;
const dy = ball.y - cp.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < closestDist) {
closestDist = dist;
closestPoint = cp;
// Edge vector
const ex = v2.x - v1.x;
const ey = v2.y - v1.y;
// Normal vector (perpendicular to edge)
const nx = -ey;
const ny = ex;
closestNormal = { x: nx, y: ny };
}
}
if (closestDist < ballRadius) {
// Push ball back inside
const overlap = ballRadius - closestDist;
const normLength = Math.sqrt(closestNormal.x * closestNormal.x + closestNormal.y * closestNormal.y);
const nx = closestNormal.x / normLength;
const ny = closestNormal.y / normLength;
ball.x += nx * overlap;
ball.y += ny * overlap;
// Reflect velocity
const reflected = reflectVelocity(ball.vx, ball.vy, nx, ny);
ball.vx = reflected.vx * 0.8; // some energy loss
ball.vy = reflected.vy * 0.8;
}
}
// Simple friction to slow horizontal velocity
ball.vx *= 0.99;
}
function draw() {
ctx.clearRect(0, 0, W, H);
// Draw octagon
const vertices = getOctagonVertices(octagonCenter.x, octagonCenter.y, octagonRadius, rotation);
drawOctagon(vertices);
// Draw ball
drawBall(ball);
}
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
loop();
})();
</script>
</body>
</html>
How it works:
- The octagon rotates continuously.
- The red ball is affected by gravity (
ay).
- The ball bounces inside the octagon, reflecting off the edges.
- The collision detection uses polygon point-inclusion and closest edge distance.
- Velocity is reflected with some energy loss to simulate bouncing.
Open this file in any modern browser to see the rotating octagon with a bouncing red ball inside. Let me know if you want it enhanced!