html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>六边形中的球体 - 地球与月球对比</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.container {
display: flex;
gap: 50px;
}
.simulation {
text-align: center;
}
.title {
font-size: 24px;
margin-bottom: 10px;
}
.hexagon-container {
position: relative;
width: 300px;
height: 300px;
border: 1px solid #ccc;
background-color: #fff;
overflow: hidden;
}
.hexagon {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
transform: translate(-50%, -50%);
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
background-color: rgba(0, 0, 0, 0.1);
border: 2px solid #333;
}
.ball {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #ff4500;
transform: translate(-50%, -50%);
}
.controls {
margin-top: 20px;
}
button {
padding: 8px 16px;
margin: 0 5px;
cursor: pointer;
}
.info {
margin-top: 10px;
font-size: 14px;
color: #555;
}
</style>
</head>
<body>
<div class="container">
<div class="simulation">
<div class="title">地球</div>
<div class="hexagon-container" id="earth-container">
<div class="hexagon" id="earth-hexagon"></div>
<div class="ball" id="earth-ball"></div>
</div>
<div class="info">重力: 9.8 m/s²</div>
<div class="controls">
<button onclick="resetEarth()">重置</button>
<button onclick="toggleEarth()">暂停/继续</button>
</div>
</div>
<div class="simulation">
<div class="title">月球</div>
<div class="hexagon-container" id="moon-container">
<div class="hexagon" id="moon-hexagon"></div>
<div class="ball" id="moon-ball"></div>
</div>
<div class="info">重力: 1.6 m/s²</div>
<div class="controls">
<button onclick="resetMoon()">重置</button>
<button onclick="toggleMoon()">暂停/继续</button>
</div>
</div>
</div>
<script>
// 物理常数
const EARTH_GRAVITY = 9.8;
const MOON_GRAVITY = 1.6;
const FRICTION = 0.98;
const ELASTICITY = 0.7;
const HEXAGON_SIZE = 100; // 半径(像素)
const BALL_RADIUS = 10;
// 六边形顶点
function getHexagonVertices(size) {
const vertices = [];
for (let i = 0; i < 6; i++) {
const angle = (i * Math.PI) / 3;
vertices.push({
x: size * Math.cos(angle),
y: size * Math.sin(angle)
});
}
return vertices;
}
// 球体类
class Ball {
constructor(containerId, ballId, gravity) {
this.container = document.getElementById(containerId);
this.ball = document.getElementById(ballId);
this.gravity = gravity;
this.vertices = getHexagonVertices(HEXAGON_SIZE);
// 物理属性
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
this.rotation = 0;
this.rotationSpeed = 0.01;
// 动画控制
this.animationId = null;
this.isRunning = false;
this.reset();
}
reset() {
// 从顶部开始
this.x = 0;
this.y = -HEXAGON_SIZE + BALL_RADIUS;
this.vx = 0;
this.vy = 0;
this.rotation = 0;
this.updatePosition();
if (this.animationId) {
cancelAnimationFrame(this.animationId);
this.animationId = null;
}
this.isRunning = true;
this.animate();
}
toggle() {
if (this.isRunning) {
cancelAnimationFrame(this.animationId);
this.animationId = null;
} else {
this.animate();
}
this.isRunning = !this.isRunning;
}
updatePosition() {
// 应用旋转
const rotatedX = this.x * Math.cos(this.rotation) - this.y * Math.sin(this.rotation);
const rotatedY = this.x * Math.sin(this.rotation) + this.y * Math.cos(this.rotation);
// 更新DOM位置
this.ball.style.left = (150 + rotatedX) + 'px';
this.ball.style.top = (150 + rotatedY) + 'px';
}
animate() {
if (!this.isRunning) return;
// 应用重力(相对于旋转坐标)
const gravityX = this.gravity * Math.sin(this.rotation);
const gravityY = this.gravity * Math.cos(this.rotation);
this.vx += gravityX * 0.1;
this.vy += gravityY * 0.1;
// 应用摩擦力
this.vx *= FRICTION;
this.vy *= FRICTION;
// 更新位置
this.x += this.vx;
this.y += this.vy;
// 检查与六边形边的碰撞
this.checkCollision();
// 更新六边形旋转
this.rotation += this.rotationSpeed;
// 更新球体位置
this.updatePosition();
// 继续动画
this.animationId = requestAnimationFrame(() => this.animate());
}
checkCollision() {
// 计算球体到中心的距离
const distance = Math.sqrt(this.x * this.x + this.y * this.y);
if (distance + BALL_RADIUS > HEXAGON_SIZE) {
// 找到碰撞的边
const angle = Math.atan2(this.y, this.x);
// 计算碰撞后的位置
const normalX = this.x / distance;
const normalY = this.y / distance;
// 将位置调整到边界
this.x = normalX * (HEXAGON_SIZE - BALL_RADIUS);
this.y = normalY * (HEXAGON_SIZE - BALL_RADIUS);
// 计算速度的法向分量
const dotProduct = this.vx * normalX + this.vy * normalY;
// 应用弹力
this.vx = (this.vx - 2 * dotProduct * normalX) * ELASTICITY;
this.vy = (this.vy - 2 * dotProduct * normalY) * ELASTICITY;
}
}
}
// 创建球体实例
const earthBall = new Ball('earth-container', 'earth-ball', EARTH_GRAVITY);
const moonBall = new Ball('moon-container', 'moon-ball', MOON_GRAVITY);
// 控制函数
function resetEarth() {
earthBall.reset();
}
function toggleEarth() {
earthBall.toggle();
}
function resetMoon() {
moonBall.reset();
}
function toggleMoon() {
moonBall.toggle();
}
// 初始化动画
window.onload = function() {
earthBall.reset();
moonBall.reset();
};
</script>
</body>
</html>