简体   繁体   English

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

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

I'm creating a game where I'm trying to use Bresenham's line algorithm(http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm ) to have enemies chase a player on a 2D map. 我正在创建一个游戏,尝试使用布雷森纳姆的线算法(http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm)让敌人在2D地图上追逐玩家。 The concept of the game is similar to the one found below. 游戏的概念类似于以下内容。 The pseudo code below is from http://herselfsai.com/2007/07/simple-predator-prey-chase-algorithms.html 下面的伪代码来自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
}

Enemies chase the player around the map but the it's ether in 0, 45, 90, etc degree angles and not straight line. 敌人会在地图上追逐玩家,但它是以0、45、90等角度而不是直线的以太。 Also, in my code the enemies also have random speed(between 0 and 5) and are sometimes over shooting the player, then trying to correct and over shoot again and again. 另外,在我的代码中,敌人的速度也是随机的(介于0和5之间),有时玩家过度射击,然后试图纠正并一次又一次过度射击。 That might be a separate issue. 那可能是一个单独的问题。

I sure just not fully grasping the concept of the algorithm. 我肯定只是没有完全掌握算法的概念。 What is the correct way to implement this? 实施此方法的正确方法是什么?

Thanks in advance. 提前致谢。

Bresenham's Line Algorithm is an easy to understand and easy to calculate algorithm to make your characters move through a route that's closest to eye to a straight line. 布雷森汉姆(Bresenham)的线算法是一种易于理解和易于计算的算法,可让您的角色沿着最接近眼睛的直线移动。

The algorithm will work for your case if cost of going 45 degrees is the same as 90 or 0 degree. 如果旋转45度的成本等于90或0度,则该算法将适用于您的情况。 Otherwise the route Bresenham takes won't be the fastest. 否则,布雷森纳姆(Bresenham)采取的路线不会是最快的。

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

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