I'll help you create an HTML/JavaScript program that simulates a ball bouncing inside a spinning hexagon with realistic physics. Here's a comprehensive implementation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bouncing Ball in Spinning Hexagon</title>
<style>
canvas {
border: 2px solid black;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="600"></canvas>
<script>
class BouncingBall {
constructor(x, y, radius, hexagon) {
this.x = x;
this.y = y;
this.radius = radius;
this.hexagon = hexagon;
// Physics properties
this.vx = Math.random() * 5 - 2.5;
this.vy = Math.random() * 5 - 2.5;
this.gravity = 0.2;
this.friction = 0.99;
this.restitution = 0.8; // Energy loss on bounce
}
update() {
// Apply gravity
this.vy += this.gravity;
// Apply friction
this.vx *= this.friction;
this.vy *= this.friction;
// Update position
this.x += this.vx;
this.y += this.vy;
// Check collision with hexagon walls
this.checkHexagonCollision();
}
checkHexagonCollision() {
// Rotate the ball's position relative to hexagon's rotation
const rotatedPoint = this.rotatePoint(
this.x,
this.y,
this.hexagon.centerX,
this.hexagon.centerY,
this.hexagon.rotation
);
// Hexagon wall collision detection
const vertices = this.hexagon.vertices;
for (let i = 0; i < vertices.length; i++) {
const j = (i + 1) % vertices.length;
const wall = this.lineIntersection(
rotatedPoint.x,
rotatedPoint.y,
vertices[i].x,
vertices[i].y,
vertices[j].x,
vertices[j].y
);
if (wall.collision) {
// Calculate normal vector
const dx = vertices[j].x - vertices[i].x;
const dy = vertices[j].y - vertices[i].y;
const wallAngle = Math.atan2(dy, dx);
// Reflect velocity
const speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
const angle = Math.atan2(this.vy, this.vx);
const reflectedAngle = 2 * wallAngle - angle;
this.vx = speed * Math.cos(reflectedAngle) * this.restitution;
this.vy = speed * Math.sin(reflectedAngle) * this.restitution;
}
}
}
rotatePoint(x, y, cx, cy, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const nx = cos * (x - cx) - sin * (y - cy) + cx;
const ny = sin * (x - cx) + cos * (y - cy) + cy;
return { x: nx, y: ny };
}
lineIntersection(x1, y1, x2, y2, x3, y3) {
const eps = this.radius;
const d1 = this.distancePointToLine(x1, y1, x2, y2, x3, y3);
return {
collision: d1 <= eps,
distance: d1
};
}
distancePointToLine(x, y, x1, y1, x2, y2) {
const A = x - x1;
const B = y - y1;
const C = x2 - x1;
const D = y2 - y1;
const dot = A * C + B * D;
const lenSq = C * C + D * D;
let param = -1;
if (lenSq !== 0) {
param = dot / lenSq;
}
let xx, yy;
if (param < 0) {
xx = x1;
yy = y1;
} else if (param > 1) {
xx = x2;
yy = y2;
} else {
xx = x1 + param * C;
yy = y1 + param * D;
}
const dx = x - xx;
const dy = y - yy;
return Math.sqrt(dx * dx + dy * dy);
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = 'red';
ctx.fill();
ctx.closePath();
}
}
class SpinningHexagon {
constructor(centerX, centerY, radius) {
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
this.rotation = 0;
this.rotationSpeed = 0.02;
this.vertices = this.calculateVertices();
}
calculateVertices() {
const vertices = [];
for (let i = 0; i < 6; i++) {
const angle = (Math.PI / 3) * i;
vertices.push({
x: this.centerX + this.radius * Math.cos(angle),
y: this.centerY + this.radius * Math.sin(angle)
});
}
return vertices;
}
update() {
this.rotation += this.rotationSpeed;
this.vertices = this.calculateRotatedVertices();
}
calculateRotatedVertices() {
const rotatedVertices = [];
for (let vertex of this.calculateVertices()) {
const rotated = this.rotatePoint(
vertex.x,
vertex.y,
this.centerX,
this.centerY,
this.rotation
);
rotatedVertices.push(rotated);
}
return rotatedVertices;
}
rotatePoint(x, y, cx, cy, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const nx = cos * (x - cx) - sin * (y - cy) + cx;
const ny = sin * (x - cx) + cos * (y - cy) + cy;
return { x: nx, y: ny };
}
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.vertices[0].x, this.vertices[0].y);
for (let i = 1; i < this.vertices.length; i++) {
ctx.lineTo(this.vertices[i].x, this.vertices[i].y);
}
ctx.closePath();
ctx.strokeStyle = 'blue';
ctx.lineWidth = 3;
ctx.stroke();
}
}
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const hexagon = new SpinningHexagon(canvas.width / 2, canvas.height / 2, 250);
const ball = new BouncingBall(canvas.width / 2, 50, 15, hexagon);
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
hexagon.update();
ball.update();
hexagon.draw(ctx);
ball.draw(ctx);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
This program creates a sophisticated physics simulation with the following key features:
-
Spinning Hexagon:
- Rotates continuously
- Calculates vertices dynamically
- Handles rotation transformations
-
Bouncing Ball:
- Simulates gravity
- Applies friction
- Handles realistic bounces
- Detects collisions with hexagon walls
- Reflects velocity based on wall angle
-
Physics Simulation:
- Realistic velocity reflection
- Energy loss on bounce (restitution)
- Friction and gravity effects
-
Rendering:
- Uses HTML5 Canvas
- Animates using
requestAnimationFrame
- Draws hexagon and ball
The simulation demonstrates complex physics interactions, including:
- Rotational transformations
- Collision detection
- Velocity reflection
- Gravitational and friction effects
When you run this HTML file, you'll see a red ball bouncing inside a blue spinning hexagon, with realistic physics governing its movement.