複数のオブジェクトの衝突によるバネ運動|HTML5

複数のオブジェクトの衝突によるバネ運動|HTML5

複数のオブジェクトの衝突によるバネ運動|HTML5(HTML5, JavaScript, CSS3) : デモ

複数のオブジェクトの衝突によるバネ運動|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(4kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>複数のオブジェクトの衝突によるバネ運動</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/base.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="275" height="275"></canvas>
        </div>
    </body>
</html>

2.CSS

@charset "utf-8";
 
body
{
    margin:0;
    padding:0;
    background-color: #fff;
    font-family:Helvetica, HiraKakuProN-W3, sans-serif; 
    font-size:12px;
    color:#000;
}
 
#contents
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height:275px;
    border: 1px solid #000;
    overflow:hidden;
}
 
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height:275px;
}

3.JavaScript

base.js

window.addEventListener("load", init, false);
 
function init()
{
    var offsetX = 0;
    var offsetY = 0;
    var ctx = null;
    var spring = 0.1;
    var bounce = -0.5;
    var gravity = 0.1;
    var theCanvas = null;
     
    var balls = new Array();
    var numBalls = 50;
     
    var theContents = document.getElementById("contents");
    offsetX = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).width;
    offsetX = Number(offsetX.replace('px',''));
 
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px',''));
     
     
    for(var i = 0; i < numBalls; i++)
    {
         
        var ball = new Object();
        ball.red = Math.floor(Math.random()*256);
        ball.green = Math.floor(Math.random()*256);
        ball.blue = Math.floor(Math.random()*256);
        ball.f = false;
        ball.x = Math.random() * offsetX;
        ball.y = Math.random() * offsetY;
        ball.vx = Math.random() * 6-3;
        ball.vy = Math.random() * 6-3;
        ball.r = 10;
        ball.d = 0;
        balls.push(ball);
    }
    
    setBallAnimation();
     
    function setBallAnimation()
    {
        ctx = document.getElementById('canvas').getContext("2d");
        setInterval(animation, 1000/60);
    }
     
    function animation()
    {
        ctx.clearRect(0,0,300,300);
         
        for(var i = 0; i<numBalls; i++)
        {
            var ball0 = balls[i];
            for(var j = i+1; j< numBalls; j++)
            {
                var ball1 = balls[j];
                 
                 
                 
                var dx = ball1.x - ball0.x;
                var dy = ball1.y - ball0.y;
                var dist = Math.sqrt(dx*dx+dy*dy);
                var minDist = ball0.r+ball1.r;
                if(dist<minDist)
                {
                    var angle = Math.atan2(dy,dx);
                    var tx = ball0.x + Math.cos(angle)*minDist;
                    var ty = ball0.y + Math.sin(angle)*minDist;
                    var ax = (tx - ball1.x) * spring;
                    var ay = (ty - ball1.y) * spring;
                    ball0.vx -= ax;
                    ball0.vy -= ay;
                    ball1.vx += ax;
                    ball1.vy += ay;
                }
                 
                 
            }
             
        }
         
        for(i=0; i < numBalls; i++)
        {
            var ball = balls[i];
            ctx.strokeStyle ="rgb("+ball.red+","+ball.green+","+ball.blue+")";
            ctx.lineWidth = 1;
            ctx.beginPath();
            ctx.arc(ball.x,ball.y, ball.r,0,Math.PI*2,true);
            ctx.fillStyle="rgb("+ball.red+","+ball.green+","+ball.blue+")";
            ctx.fill();
            ctx.stroke();
            move(ball);
        }
         
    }
     
    function move(ball)
    {
        ball.vy += gravity;
        ball.x += ball.vx;
        ball.y += ball.vy;
        if(ball.x + ball.r > offsetX)
        {
            ball.x = offsetX - ball.r;
            ball.vx *= bounce;
        }
        else
        if(ball.x - ball.r < 0)
        {
            ball.x = ball.r;
            ball.vx *= bounce;
        }
         
        if(ball.y + ball.r > offsetY)
        {
            ball.y = offsetY - ball.r;
            ball.vy *= bounce;
        }
        else
        if(ball.y - ball.r < 0)
        {
            ball.y = ball.r;
            ball.vy *= bounce;
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です