物理重力弹性动画

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>物理重力弹性动画</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            overflow: hidden;
            background: linear-gradient(135deg, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
            font-family: Arial, sans-serif;
        }
        
        #canvas {
            display: block;
            width: 100%;
            height: 100vh;
            cursor: pointer;
        }
        
        .controls {
            position: absolute;
            top: 20px;
            left: 20px;
            background: rgba(255, 255, 255, 0.8);
            padding: 15px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }
        
        .control-group {
            margin-bottom: 10px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        
        input[type="range"] {
            width: 200px;
        }
        
        .value-display {
            font-size: 12px;
            color: #666;
        }
        
        .ad-container {
            position: absolute;
            bottom: 20px;
            left: 0;
            right: 0;
            text-align: center;
            color: white;
            font-size: 14px;
            background: rgba(0, 0, 0, 0.5);
            padding: 10px;
            border-radius: 5px;
            margin: 0 auto;
            width: fit-content;
        }
        
        .ad-container a {
            color: #ffcc00;
            text-decoration: none;
            font-weight: bold;
        }
        
        .ad-container a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="controls">
        <div class="control-group">
            <label for="gravity">重力</label>
            <input type="range" id="gravity" min="0" max="2" step="0.1" value="0.5">
            <span class="value-display" id="gravity-value">0.5</span>
        </div>
        <div class="control-group">
            <label for="elasticity">弹性</label>
            <input type="range" id="elasticity" min="0" max="1" step="0.1" value="0.7">
            <span class="value-display" id="elasticity-value">0.7</span>
        </div>
        <div class="control-group">
            <label for="friction">摩擦力</label>
            <input type="range" id="friction" min="0" max="0.99" step="0.01" value="0.98">
            <span class="value-display" id="friction-value">0.98</span>
        </div>
        <button id="reset-btn">重置</button>
    </div>
    
    <canvas id="canvas"></canvas>
    
 

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('canvas');
            const ctx = canvas.getContext('2d');
            
            // 设置画布大小为窗口大小
            function resizeCanvas() {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
            }
            
            window.addEventListener('resize', resizeCanvas);
            resizeCanvas();
            
            // 物理参数
            let gravity = 0.5;
            let elasticity = 0.7;
            let friction = 0.98;
            
            // 控制元素
            const gravitySlider = document.getElementById('gravity');
            const elasticitySlider = document.getElementById('elasticity');
            const frictionSlider = document.getElementById('friction');
            const gravityValue = document.getElementById('gravity-value');
            const elasticityValue = document.getElementById('elasticity-value');
            const frictionValue = document.getElementById('friction-value');
            const resetBtn = document.getElementById('reset-btn');
            
            // 更新参数显示
            function updateControls() {
                gravityValue.textContent = gravity;
                elasticityValue.textContent = elasticity;
                frictionValue.textContent = friction;
            }
            
            // 监听滑块变化
            gravitySlider.addEventListener('input', function() {
                gravity = parseFloat(this.value);
                updateControls();
            });
            
            elasticitySlider.addEventListener('input', function() {
                elasticity = parseFloat(this.value);
                updateControls();
            });
            
            frictionSlider.addEventListener('input', function() {
                friction = parseFloat(this.value);
                updateControls();
            });
            
            resetBtn.addEventListener('click', function() {
                balls = [];
                addBall(canvas.width / 2, 100);
            });
            
            // 球体类
            class Ball {
                constructor(x, y) {
                    this.x = x;
                    this.y = y;
                    this.radius = 15 + Math.random() * 20;
                    this.color = getRandomColor();
                    this.vx = (Math.random() - 0.5) * 10;
                    this.vy = (Math.random() - 0.5) * 10;
                    this.mass = this.radius * 0.2;
                }
                
                update() {
                    // 应用重力
                    this.vy += gravity;
                    
                    // 应用摩擦力
                    this.vx *= friction;
                    this.vy *= friction;
                    
                    // 更新位置
                    this.x += this.vx;
                    this.y += this.vy;
                    
                    // 边界碰撞检测
                    if (this.x - this.radius < 0) {
                        this.x = this.radius;
                        this.vx = -this.vx * elasticity;
                    } else if (this.x + this.radius > canvas.width) {
                        this.x = canvas.width - this.radius;
                        this.vx = -this.vx * elasticity;
                    }
                    
                    if (this.y - this.radius < 0) {
                        this.y = this.radius;
                        this.vy = -this.vy * elasticity;
                    } else if (this.y + this.radius > canvas.height) {
                        this.y = canvas.height - this.radius;
                        this.vy = -this.vy * elasticity;
                    }
                }
                
                draw() {
                    ctx.beginPath();
                    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
                    ctx.fillStyle = this.color;
                    ctx.fill();
                    ctx.closePath();
                    
                    // 添加高光效果
                    ctx.beginPath();
                    ctx.arc(
                        this.x - this.radius * 0.3,
                        this.y - this.radius * 0.3,
                        this.radius * 0.2,
                        0,
                        Math.PI * 2
                    );
                    ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
                    ctx.fill();
                    ctx.closePath();
                }
            }
            
            // 生成随机颜色
            function getRandomColor() {
                const colors = [
                    '#FF5252', '#FF4081', '#E040FB', '#7C4DFF',
                    '#536DFE', '#448AFF', '#40C4FF', '#18FFFF',
                    '#64FFDA', '#69F0AE', '#B2FF59', '#EEFF41',
                    '#FFFF00', '#FFD740', '#FFAB40', '#FF6E40'
                ];
                return colors[Math.floor(Math.random() * colors.length)];
            }
            
            // 球体数组
            let balls = [];
            
            // 添加球体
            function addBall(x, y) {
                balls.push(new Ball(x, y));
            }
            
            // 初始球体
            addBall(canvas.width / 2, 100);
            
            // 鼠标/触摸交互
            canvas.addEventListener('click', function(e) {
                addBall(e.clientX, e.clientY);
            });
            
            canvas.addEventListener('touchstart', function(e) {
                e.preventDefault();
                const touch = e.touches[0];
                addBall(touch.clientX, touch.clientY);
            });
            
            // 动画循环
            function animate() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                
                // 更新和绘制所有球体
                for (let i = 0; i < balls.length; i++) {
                    balls[i].update();
                    balls[i].draw();
                    
                    // 简单的球体碰撞检测
                    for (let j = i + 1; j < balls.length; j++) {
                        checkCollision(balls[i], balls[j]);
                    }
                }
                
                requestAnimationFrame(animate);
            }
            
            // 球体碰撞检测
            function checkCollision(ball1, ball2) {
                const dx = ball2.x - ball1.x;
                const dy = ball2.y - ball1.y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                
                if (distance < ball1.radius + ball2.radius) {
                    // 碰撞发生
                    const angle = Math.atan2(dy, dx);
                    const sin = Math.sin(angle);
                    const cos = Math.cos(angle);
                    
                    // 旋转坐标系
                    const x1 = 0;
                    const y1 = 0;
                    const x2 = dx * cos + dy * sin;
                    const y2 = dy * cos - dx * sin;
                    
                    // 旋转速度
                    const vx1 = ball1.vx * cos + ball1.vy * sin;
                    const vy1 = ball1.vy * cos - ball1.vx * sin;
                    const vx2 = ball2.vx * cos + ball2.vy * sin;
                    const vy2 = ball2.vy * cos - ball2.vx * sin;
                    
                    // 碰撞后速度
                    const vx1Final = ((ball1.mass - ball2.mass) * vx1 + 2 * ball2.mass * vx2) / (ball1.mass + ball2.mass);
                    const vx2Final = ((ball2.mass - ball1.mass) * vx2 + 2 * ball1.mass * vx1) / (ball1.mass + ball2.mass);
                    
                    // 应用弹性系数
                    const vy1Final = vy1 * elasticity;
                    const vy2Final = vy2 * elasticity;
                    
                    // 防止球体重叠
                    const overlap = (ball1.radius + ball2.radius) - distance;
                    const moveX = overlap * cos * 0.5;
                    const moveY = overlap * sin * 0.5;
                    
                    ball1.x -= moveX;
                    ball1.y -= moveY;
                    ball2.x += moveX;
                    ball2.y += moveY;
                    
                    // 旋转回原坐标系
                    ball1.vx = vx1Final * cos - vy1Final * sin;
                    ball1.vy = vy1Final * cos + vx1Final * sin;
                    ball2.vx = vx2Final * cos - vy2Final * sin;
                    ball2.vy = vy2Final * cos + vx2Final * sin;
                }
            }
            
            // 启动动画
            animate();
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值