繁体   English   中英

使用布雷森汉姆线算法的简单追逐游戏运动

[英]Simple Chase Game Movement Using Bresenham’s Line Algorithm

我正在创建一个游戏,尝试使用布雷森纳姆的线算法(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之间),有时玩家过度射击,然后试图纠正并一次又一次过度射击。 那可能是一个单独的问题。

我肯定只是没有完全掌握算法的概念。 实施此方法的正确方法是什么?

提前致谢。

布雷森汉姆(Bresenham)的线算法是一种易于理解和易于计算的算法,可让您的角色沿着最接近眼睛的直线移动。

如果旋转45度的成本等于90或0度,则该算法将适用于您的情况。 否则,布雷森纳姆(Bresenham)采取的路线不会是最快的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM