Bresenham のライン アルゴリズム (http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm ) を使用して、敵が 2D マップでプレイヤーを追いかけるゲームを作成しています。ゲームのコンセプトは、以下にあるものと似ています。以下の疑似コードは、http://herselfsai.com/2007/07/simple-predator-prey-chase-algorithms.htmlからのものです。
prey current position ( xp, yp )
predator current position ( xP, yP )
x = x position to move to
y = y position to move to
dx = xp – xP
dy = yp – yP
Adx = AbsoluteValue ( dx )
Ady = AbsoluteValue (dy )
if ( xp > xP ) stepX = 1 else stepX = -1
if ( yp > yP ) stepY = 1 else stepY = -1
if ( Ady > Adx ){ //the y distance from prey is larger than the x distance
fraction = 2*dx – dy;
if (( yP != yp ) && ( fraction > 0 )){
x += stepX
}
y += stepY
}else{
fraction = 2*dy – dx;
if (( xP != xp ) && ( fraction > 0 )){
y += stepY
}
x += stepX
}
敵はマップの周りでプレイヤーを追いかけますが、それは0、45、90度などの角度であり、直線ではありません。また、私のコードでは、敵もランダムな速度 (0 から 5 の間) を持っており、時々プレイヤーを撃ちすぎてから、何度も何度も修正して撃ち過ぎようとします。それは別の問題かもしれません。
アルゴリズムの概念を完全に把握していないだけです。これを実装する正しい方法は何ですか?
前もって感謝します。