Below is a self-contained HTML file.
Save it as bouncing-ball-hexagon.html, open it in any modern browser, and you’ll get a ball that (1) is pulled down by gravity, (2) loses a bit of speed because of air-friction, and (3) keeps bouncing realistically off the walls of a continuously spinning hexagon. 🏀⬡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bouncing Ball in a Spinning Hexagon</title>
<style>
html,body{
margin:0;
height:100%;
background:#111;
overflow:hidden;
display:flex;
align-items:center;
justify-content:center;
}
canvas{
background:#202020;
border:2px solid #555;
box-shadow:0 0 15px #000 inset;
}
h1{
position:fixed;
top:10px;left:50%;
transform:translateX(-50%);
color:#eee;font-family:sans-serif;font-size:18px;
letter-spacing:.05em;
pointer-events:none;
margin:0;
}
</style>
</head>
<body>
<canvas id="c" width="600" height="600"></canvas>
<h1>Bouncing Ball inside a Spinning Hexagon</h1>
<script>
// -- Canvas -------------------------------------------------------------
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
const W = canvas.width;
const H = canvas.height;
const CENTER = {x: W/2, y: H/2};
// -- Hexagon parameters -------------------------------------------------
const HEX_RADIUS = 220; // distance from center to a corner
let hexAngle = 0; // current rotation
const ROT_SPEED = 0.006; // rad / frame
// -- Ball parameters ----------------------------------------------------
const BALL_RADIUS = 14;
let ballPos = {x: W/2, y: H/2 - 150};
let ballVel = {x: 3, y: 0};
const GRAVITY = 0.30; // px / frame²
const FRICTION_AIR = 0.999; // (air drag)
const RESTITUTION = 0.90; // energy kept in bounce (1 = perfectly elastic)
// -- Utility helpers ----------------------------------------------------
const vec = {
add :(a,b)=>({x:a.x+b.x, y:a.y+b.y}),
sub :(a,b)=>({x:a.x-b.x, y:a.y-b.y}),
mul :(v,s)=>({x:v.x*s, y:v.y*s}),
dot :(a,b)=> a.x*b.x + a.y*b.y,
len :(v) => Math.hypot(v.x, v.y),
norm:(v)=>{
const l=Math.hypot(v.x,v.y);
return l?{x:v.x/l,y:v.y/l}:{x:0,y:0};
},
perp:(v)=>({x: v.y, y:-v.x}) // clockwise perpendicular
};
// -- Build hexagon points for a given angle -----------------------------
function hexPoints(angle){
const pts=[];
for(let i=0;i<6;i++){
const a = angle + i*Math.PI/3; // 60°
pts.push({
x: CENTER.x + HEX_RADIUS*Math.cos(a),
y: CENTER.y + HEX_RADIUS*Math.sin(a)
});
}
return pts;
}
// -- Physics & Collision ------------------------------------------------
function stepPhysics(){
// 1. Gravity & Air Friction
ballVel.y += GRAVITY;
ballVel.x *= FRICTION_AIR;
ballVel.y *= FRICTION_AIR;
// 2. Integrate position
ballPos.x += ballVel.x;
ballPos.y += ballVel.y;
// 3. Collision vs each hex-edge
const pts = hexPoints(hexAngle);
for(let i=0;i<6;i++){
const p1 = pts[i];
const p2 = pts[(i+1)%6];
const edge = vec.sub(p2,p1);
const normalCW = vec.norm(vec.perp(edge)); // Outward normal for CW polygon
// Signed distance from ball center to the line
const dist = vec.dot(vec.sub(ballPos,p1), normalCW);
// If ball crossed outside this edge (dist > -r) => collide & correct
if(dist > -BALL_RADIUS){
// Push ball back inside
ballPos = vec.add(ballPos, vec.mul(normalCW, -(dist + BALL_RADIUS)));
// Reflect velocity if moving outward
const vn = vec.dot(ballVel, normalCW);
if(vn > 0){
ballVel = vec.sub(ballVel, vec.mul(normalCW, (1+RESTITUTION)*vn));
}
}
}
}
// -- Drawing ------------------------------------------------------------
function draw(){
ctx.clearRect(0,0,W,H);
// Hexagon
const pts = hexPoints(hexAngle);
ctx.beginPath();
ctx.moveTo(pts[0].x, pts[0].y);
for(let i=1;i<6;i++) ctx.lineTo(pts[i].x, pts[i].y);
ctx.closePath();
ctx.strokeStyle = '#6cf';
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = 'rgba(60,60,60,0.15)';
ctx.fill();
// Ball
ctx.beginPath();
ctx.arc(ballPos.x, ballPos.y, BALL_RADIUS, 0, Math.PI*2);
ctx.fillStyle = '#f95';
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = '#fff';
ctx.stroke();
}
// -- Main loop ----------------------------------------------------------
function animate(){
requestAnimationFrame(animate);
hexAngle += ROT_SPEED; // spin the hexagon
stepPhysics(); // update ball
draw(); // render everything
}
animate();
</script>
</body>
</html>
How it works (quick overview)
- A canvas of 600 × 600 px is set up.
- On each animation frame (
requestAnimationFrame):
• The hexagon’s rotation angle grows by ROT_SPEED, generating six corner points in hexPoints().
• Gravity accelerates the ball downward; its velocity then gets slightly reduced by FRICTION_AIR.
• The ball’s tentative position is found.
• For each of the six edges we compute (a) the outward normal and (b) the signed distance from the ball’s center to that edge.
• If the distance is smaller than the radius, penetration is resolved by moving the ball back inside and reflecting the velocity vector across the edge’s normal—tempered by a RESTITUTION coefficient.
- Finally the hexagon and the ball are drawn.
Tweak the constants up top (gravity, friction, restitution, spin speed, sizes) to get exactly the feel you want. Have fun! 🎉