プログラミングとイラストレーション » 変わり続ける角度と衝突と跳ね返り|HTML5
プログラミングとイラストレーション > HTML5 > 変わり続ける角度と衝突と跳ね返り|HTML5

変わり続ける角度と衝突と跳ね返り|HTML5

変わり続ける角度と衝突と跳ね返り|HTML5

変わり続ける角度と衝突と跳ね返り|HTML5(HTML5, JavaScript, CSS3) : デモ

変わり続ける角度と衝突と跳ね返り|HTML5(HTML5, JavaScript, CSS3) : ZIPファイル(5kb)

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/devicecheck.js"></script>
        <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:10px;
    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,
    offsetY = 0,
    ctx,
    theCanvas,
    theContents,
    line = [],
    balls = [],
    length = 200,
    numPoints = 2,
    numBalls = 1,
    degreeMax = 15,
    bounce = -0.95,
    gravity = 0.01,
    radian = 0.001,
    degree = 0,
    radianLimit = 0.001,
    buttonStart,
    buttonMove,
    buttonEnd;
     
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
     
    /*
     * ボタンタイプ設定
     */
    if(g_mobiledevice === true)
    {
        buttonStart = 'touchstart';
        buttonMove = 'touchmove';
        buttonEnd = 'touchend';
    }
    else
    {
        buttonStart = 'mousedown';
        buttonMove = 'mousemove';
        buttonEnd = 'mouseup';
    }
     
    document.getElementById('canvas').addEventListener(buttonEnd, mouseUpHandler, false);
     
    function mouseUpHandler()
    {
        line = [];
        balls = [];
        setInitBall();
    }
     
    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',''));
     
    ctx = document.getElementById('canvas').getContext("2d");
    setInitBall();
    setBallAnimation();
     
    function setInitBall()
    {
        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.x = offsetX / 2;
        ball.y = 0;
        ball.vx = 0;
        ball.vy = 0;
        ball.r = 10;
        balls.push(ball);
     
        for(var i = 0; i < numPoints; i++)
        {
            if(i == 0)
            {
                var x = offsetX / 2 - length;
                var y = offsetY / 2;
            }
            else
            {
                x = offsetX / 2 + length;
                y = offsetY / 2;
            }
            var point = new Object();
            point.x = x;
            point.y = y;
            line.push(point);
        }
    }
     
    function setLineRotateMatrix()
    {
        var dx = line[1].x - line[0].x;
        var dy = line[1].y - line[0].y;
        var degree = Math.atan2(dy, dx) * 180 / Math.PI;
         
        if(degree > degreeMax)
        {
            radian = -radianLimit;
        }
        else
        if(degree < -degreeMax)
        {
            radian = radianLimit;
        }
         
        //line Satrt x y
        var x1 = line[0].x - offsetX / 2,
        y1 = line[0].y - offsetY / 2,
        x2 = x1 * Math.cos(radian) - y1 *  Math.sin(radian),
        y2 = x1 * Math.sin(radian) + y1 *  Math.cos(radian);
        line[0].x = x2 + offsetX / 2;
        line[0].y = y2 + offsetY / 2;
         
        //line End x y
        var x1 = line[1].x - offsetX / 2,
        y1 = line[1].y - offsetY / 2,
        x2 = x1 * Math.cos(radian) - y1 *  Math.sin(radian),
        y2 = x1 * Math.sin(radian) + y1 *  Math.cos(radian);
        line[1].x = x2 + offsetX / 2;
        line[1].y = y2 + offsetY / 2;
    }
     
    function setBallAnimation()
    {
        setLineRotateMatrix();
        animation();
        requestAnimationFrame(setBallAnimation);
    }
      
    function animation()
    {
        ctx.clearRect(0, 0, 300, 300);
        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);
        }
        draw();
    }
     
    function draw()
    {
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(line[0].x, line[0].y);
        ctx.lineTo(line[1].x, line[1].y);
        ctx.fill();
        ctx.stroke();
    }
     
    function move(ball)
    {
        ball.vy += gravity;
        ball.x += ball.vx;
        ball.y += ball.vy;
         
        var dx = line[1].x - line[0].x;
        var dy = line[1].y - line[0].y;
        var radian = Math.atan2(dy, dx);
         
        var cos = Math.cos(radian);
        var sin = Math.sin(radian);
         
        // 線を基準にしたボールの位置の取得
        var x1 = ball.x - line[0].x;
        var y1 = ball.y - line[0].y;
         
        // 座標の回転
        var x2 = cos * x1 + sin * y1;
        var y2 = cos * y1 - sin * x1;
             
        // 速度の回転
        var vx1 = cos * ball.vx + sin * ball.vy;
        var vy1 = cos * ball.vy - sin * ball.vx;
         
        // 回転させた値を使った跳ね返りの実行
        if(y2 > -ball.r)
        {
            y2 = -ball.r;
            vy1 *= bounce;
        }
             
        // すべてを回転させ元に戻す
        x1 = cos * x2 - sin * y2;
        y1 = cos * y2 + sin * x2;
        ball.vx = cos * vx1 - sin * vy1;
        ball.vy = cos * vy1 + sin * vx1;
        ball.x = line[0].x + x1;
        ball.y = line[0].y + y1;
         
         
        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;
        }
    }
}
 
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000/60);
    };
}());

devicecheck.js

var g_mobiledevice = false; //iPhone、iPad、Androidのときtrue
var g_browsername = 'unknown';
var g_browserver = -1;
 
if(checkUserAgent() == false)
{
    window.alert('このブラウザは対象外です');
}
 
function checkUserAgent()
{
    var ua = navigator.userAgent;
 
    //iPhoneか?  
    var mstr = ua.match(/iPhone OS \d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 3) 
        {
            g_mobiledevice = true;
            g_browsername = 'iPhone';
            //alert('major-version ' + vstr);
            return true;
        }
    }
    //iPadか?
    if(ua.indexOf('iPad') > -1)
    {
        mstr = ua.match(/CPU OS \d+/);
        if(mstr != null)
        {
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 3) {
                g_mobiledevice = true;
                g_browsername = 'iPad';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }   
    //Androidか? 
    var mstr = ua.match(/Android \d+\.\d+/);
    if(mstr != null)
    {
        g_browsername = 'Android';
        var vstr = mstr[0].match(/\d+\.\d+/);
        g_browserver = parseFloat(vstr[0]);
        g_mobiledevice = true;
        if(pg_browserver > 2.1) 
        {
            //alert('version ' + vstr);
            return true;
        }
    }
    //Chromeか?
    mstr = ua.match(/Chrome\/\d+/);
    if(mstr != null)
    {
        g_browsername = 'Chrome';
        var vstr = mstr[0].match(/\d+/);
        g_browserver = parseInt(vstr[0]);
        if(g_browserver >= 9) 
        {
            //alert('major-version ' + vstr);
             
            return true;
        }       
    }
    //Safariか?
    if(ua.indexOf('Safari') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 5) 
            {
                g_browsername = 'Safari';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
    //Internet Explorerか?
    mstr = ua.match(/MSIE \d+/);
    if(mstr != null)
    {
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 9) 
        {
            g_browsername = 'MSIE';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Firefoxか?
    mstr = ua.match(/Firefox\/\d+/);
    if(mstr != null){
        var vstr = mstr[0].match(/\d+/);
        if(parseInt(vstr[0]) >= 4) 
        {
            g_browsername = 'Firefox';
            //alert('major-version ' + vstr);
            return true;
        }       
    }
    //Operaか?
    if(ua.indexOf('Opera') > -1)
    {
        mstr = ua.match(/Version\/\d+/);
        if(mstr != null){
            var vstr = mstr[0].match(/\d+/);
            if(parseInt(vstr[0]) >= 11) 
            {
                g_browsername = 'Opera';
                //alert('major-version ' + vstr);
                return true;
            }       
        }       
    }
     
    return false;
}

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e5%a4%89%e3%82%8f%e3%82%8a%e7%b6%9a%e3%81%91%e3%82%8b%e8%a7%92%e5%ba%a6%e3%81%a8%e8%a1%9d%e7%aa%81%e3%81%a8%e8%b7%b3%e3%81%ad%e8%bf%94%e3%82%8a%ef%bd%9chtml5/trackback/