人間/バクテリアのグループがお互いを追いかけるアプリケーションを作ろうとしています。しかし、ターゲットに向かって真っ直ぐに行かせようとすると、代わりにすべてが左に移動します。(「ターゲット」と言うのは、一方が他方のメンバーをターゲットにしているためです。) 最初の x、y 座標がある三角法を使用してみましたが、勾配と移動距離で次の x、y 座標を見つける必要があります。知られています。関連するコードは次のとおりです。
/*Functions for calculating next x, y*/
function calculate_nextX(startx, length, slope)
{
var x = 0;
var degree = Math.atan(slope);
x = (startx + (length * Math.sin(degree * 0.0174)));
return x;
}
function calculate_nextY(starty, length, slope)
{
var y = 0;
var degree = Math.atan(slope);
y = (starty + (length * Math.cos(degree * 0.0174)));
return y;
}
/*Where the functions get used*/
function updateeverything()
{
for(var i=0;i<=49;i++)
{
if(bacteriaGroup[i].dead == false)
{
if(humanGroup[bacteriaGroup[i].target].dead == true)
{
bacteriaGroup[i].target = settarget(true,i);
}
var left = parseInt(document.getElementById("bacteria"+i).style.left);
var left1 = parseInt(document.getElementById("human"+bacteriaGroup[i].target).style.left);
var top = parseInt(document.getElementById("bacteria"+i).style.top);
var top1 = parseInt(document.getElementById("human"+bacteriaGroup[i].target).style.top);
var finalleft = calculate_nextX(left,1,(top1-top)/(left1-left));
var finaltop = calculate_nextY(top,1,(top1-top)/(left1-left));
document.getElementById("bacteria"+i).style.left = finalleft;
document.getElementById("bacteria"+i).style.top = finaltop;
}
if(humanGroup[i].dead == false)
{
if(bacteriaGroup[humanGroup[i].target].dead == true)
{
humanGroup[i].target = settarget(false,i);
}
var left = parseInt(document.getElementById("human"+i).style.left);
var left1 = parseInt(document.getElementById("bacteria"+humanGroup[i].target).style.left);
var top = parseInt(document.getElementById("human"+i).style.top);
var top1 = parseInt(document.getElementById("bacteria"+humanGroup[i].target).style.top);
var finalleft = calculate_nextX(left,1,(top1-top)/(left1-left));
var finaltop = calculate_nextY(top,1,(top1-top)/(left1-left));
document.getElementById("human"+i).style.left = finalleft;
document.getElementById("human"+i).style.top = finaltop;
}
}
}