自律エージェントの操舵行動|HTML5

自律エージェントの操舵行動|HTML5

自律エージェントの操舵行動(HTML5, JavaScript, CSS3) : デモ

自律エージェントの操舵行動(HTML5, JavaScript, CSS3) : ZIPファイル(7kb)

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
        <title>自律エージェントの操舵行動</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/base.js"></script>
        <script src="js/devicecheck.js"></script>
    </head>
    <body>
        <div id="contents">
            <canvas id="canvas" width="300" height="300">
            </canvas>
            <div id="deg"></div>
        </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: 300px;
    height:300px;
    border: 1px solid #000;
    overflow:hidden;
}
 
#canvas
{
    position: absolute;
    top: 0;
    left: 0;
    width: 300px;
    height:300px;
}

#rad
{
	position: absolute;
    top: 10px;
    left: 10px;
    width: 10px;
    height:10px;
}

3.JavaScript

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;
}

base.js

window.addEventListener("load", init, false);
 
var objX;
var objY;
var theCanvas;
var ctx;
var currentX0;
var currentY0;
var currentX1;
var currentY1;
var currentX2;
var currentY2;
var points = new Array();
var mouseX;
var mouseY;
var flag = false;
var mouseX1 = 0;
var mouseY1 = 0;
var mouseX=0;
var mouseY=0;
 
function init()
{
    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';
    }
     
    theCanvas = document.getElementById("contents");
    objX = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).width;
    objX = Number(objX.replace('px',''));
    objX =  objX / 2;
    objY = (theCanvas.currentStyle || document.defaultView.getComputedStyle(theCanvas,'')).height;
    objY = Number(objY.replace('px',''));
    objY =  objY / 2;
     
    theCanvas = document.getElementById("canvas");
     
    theCanvas.addEventListener(buttonMove, mouseDownHandler, false);
     
    function mouseDownHandler(e) 
    {
        
        if(g_mobiledevice)
        {
            var rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.touches[0].pageX - rect.left-objX;
            mouseY = event.touches[0].pageY - rect.top-objY;
            
            mouseX1 =  event.touches[0].pageX - rect.left;
        	mouseY1 =  event.touches[0].pageY - rect.top;
        }
        else
		if(g_browsername == 'MSIE' || g_browsername == 'Opera')
		{
			rect = document.getElementById("canvas").getBoundingClientRect();
			mouseX = event.clientX - rect.left - objX;
			mouseY = event.clientY - rect.top - objY;
	                
	        mouseX1 =  event.clientX - rect.left;
	        mouseY1 =  event.clientY - rect.top;
		}
		else
		if(g_browsername == 'Firefox')
		{
			rect = document.getElementById("canvas").getBoundingClientRect();
			mouseX = e.clientX - rect.left - objX;
			mouseY = e.clientY - rect.top - objY;
	                
	        mouseX1 =  e.clientX - rect.left;
	        mouseY1 =  e.clientY - rect.top;
		}
        else
        {
        	rect = event.currentTarget.getBoundingClientRect();
            mouseX =  event.clientX - rect.left-objX;
            mouseY =  event.clientY - rect.top-objY;
            
            mouseX1 =  event.clientX - rect.left;
        	mouseY1 =  event.clientY - rect.top;
        }
    }
    ctx =  theCanvas.getContext("2d");
     
    var x = Math.cos(18*Math.PI/180)*10*1.62;
    var y = Math.sin(18*Math.PI/180)*10*1.62;
 
    points.push({x:-(x/2),y:-y});
    points.push({x:x/2,y:0});
    points.push({x:-(x/2),y:y});
    
    currentX0 = points[0].x;
    currentY0 = points[0].y;
    currentX1 = points[1].x;
    currentY1 = points[1].y;
    currentX2 = points[2].x;
    currentY2 = points[2].y;
    mouseX = 0;
    mouseY = 0;
    mouseX1 = 300;
    mouseY1 = 150;
    set();
}
 
function set()
{
    var radian = 0;
    var dx = 0;
    var dy = 0;
    var speed = 0;
    var speedMax = 1.5;
    var accel = 0.01;
    var rotationCurrent = 0;
    var rotationMax = 360;
    var steer = 0.01
    var forceRight = 0;
    var forceLeft = 0;
    var forceMax = 180;

    function animation()
    {
        ctx.clearRect(0,0,300,300);

        if(rotationCurrent<-rotationMax || rotationCurrent>rotationMax){
            rotationCurrent=0;
        }

        dx = mouseX1-objX;
        dy = mouseY1-objY;
        
        var targetRotation = Math.atan2(dy,dx)*180/Math.PI;

        var distance = Math.sqrt(dx*dx+dy*dy);

        // オブジェクトの追求行動
        accel = 0.01;
        speed += accel;
        speed = Math.min(speed, speedMax);

        forceRight += steer;
        forceRight = Math.min(forceRight, forceMax);
        forceLeft += steer;
        forceLeft = Math.min(forceLeft, forceMax);
        //accelFlag = false;


        // オブジェクトの進行処理
        radian = rotationCurrent*Math.PI/180;
        var vx = Math.cos(radian)*speed;
        var vy = Math.sin(radian)*speed;
        objX += vx;
        objY += vy;

        var rotationObj = radian*180/Math.PI;

        // ターゲットの角度の補正処理
        if(targetRotation>-180 && targetRotation<0) {
           targetRotation = rotationMax+targetRotation; 
        }

        var offset = -1*(rotationObj-targetRotation);
        if(offset<0) {
            var targetRotationOffset = rotationMax+offset;
        } else {
            targetRotationOffset = offset;
        }

        if(targetRotationOffset>360) {
            targetRotationOffset -= 360;
        }
        
        // オブジェクトの回転処理
        if(targetRotationOffset>=0 && targetRotationOffset<=180) {
            forceLeft = 0;
            rotationCurrent += forceRight;
        } else {
            forceRight = 0;
            rotationCurrent -= forceLeft;
        }

        if(rotationCurrent<-360) {
            rotationCurrent = rotationCurrent+360;
        }

        if(rotationCurrent>360) {
            rotationCurrent = rotationCurrent-360;
        }

        currentX0 = Math.cos(radian)*points[0].x - Math.sin(radian)*points[0].y;
        currentY0 = Math.sin(radian)*points[0].x + Math.cos(radian)*points[0].y;
        currentX1 = Math.cos(radian)*points[1].x - Math.sin(radian)*points[1].y;
        currentY1 = Math.sin(radian)*points[1].x + Math.cos(radian)*points[1].y;
        currentX2 = Math.cos(radian)*points[2].x - Math.sin(radian)*points[2].y;
        currentY2 = Math.sin(radian)*points[2].x + Math.cos(radian)*points[2].y;

        //document.getElementById('deg').innerHTML="object degree: "+rotationCurrent;
        //document.getElementById('deg').innerHTML="distance: " + distance + " speed: " + speed;
        //document.getElementById('deg').innerHTML="speed: " + speed + "<br>" + "distance:" + distance;

        
        draw(currentX0, currentY0, currentX1, currentY1, currentX2, currentY2);
    }
    setInterval(animation, 1000/60);
}
 
function draw(currentX0,currentY0,currentX1,currentY1,currentX2,currentY2)
{
    ctx.strokeStyle="#000000";
    ctx.lineWidth=1;
    ctx.beginPath();
    ctx.moveTo(objX+currentX0, objY+currentY0);
    ctx.lineTo(objX+currentX1, objY+currentY1);
    ctx.lineTo(objX+currentX2, objY+currentY2);
    ctx.lineTo(objX+currentX0, objY+currentY0);
    ctx.stroke();
}

コメントを残す

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