友情博客

搜索

最新评论

RSS

我的 Blog:
mrG 最新的 20 条日志
[关于我]
[FLASH]
[酷站推荐]
[好贴转载]
[web标准]
[News]
[网页制作]
[资料翻译]
全站 Blog:
全站最新的 20 条日志

第二章 追逐和躲避

这一章我们讨论在游戏中普遍存在的追逐和躲避的问题。不管你是开发一个飞船射击游戏,或一个战略模拟游戏,还是一个第一人称模拟游戏,你都有可能要遇到想让电脑人物追逐或者避开玩家的人物的情况。在动作或电子游戏中都会有宇宙飞船跟踪或进攻玩家的飞船。在角色扮演游戏中会让你旋转或一些其他可爱的生物追逐玩家的游戏人物。在第一人称射击和飞行模拟游戏中你必须让导弹跟踪和进攻玩家的人物或玩家的飞行器。这种情况下,你就需要一些逻辑,使得电脑的掠食者去追逐,而他们的猎物要逃跑。

追逐和躲避问题包含两部分。第一部分是决定要开始追逐还是躲避。第二部分是怎样有效的追逐或是躲避——也就是说让你的捕食者去捕食,而让猎物在不被抓到的情况下跑的越远越好。在某种意义上说,有人可能会讨论追逐/躲避含有的第三中要素:躲避障碍。当追逐或躲避的时候躲避障碍的确使问题变复杂了,使得算法更难的编程。在这一章我们讨论问题的第二部分:怎么有效的追逐和躲避。在后面的几章中我们会讨论问题的第一部分——决定如何行动:是追逐还是躲避。

最简单,最容易编程,使用最普遍的方法是,让捕食者追逐他的猎物,通过每个游戏循环刷新捕食者的坐标这样捕食者的坐标和猎物之间的坐标就会越来越小。这个算法没考虑捕食者和猎物各自的航向(他们移动的方向)和速度。尽管这个方法在捕食者持续向猎物移动时非常有效除非被障碍物阻止,否则会一直的向猎物移动,这个算法有他的局限性,所以我们会有较少的篇幅介绍。

除了这个最基本的算法,还介绍了一些其他的方法,这些方法可能更适合你游戏环境的需要。比如在游戏中你可以使用实时的物理引擎来计算捕食者和猎物的坐标和速度,这样这样捕食者就可以拦截他的猎物,而不必一味的在后面追逐他。这样相对位置和相对速度的信息作为算法的输入量,计算出适合的转向力,比如——引导捕食者追逐到他的猎物。另一个方法是使用势能函数在某种意义上去影响捕食者追逐猎物的行为,或更特别的行为,让猎物吸引捕食者。同样的你可以同样可以使用势能函数去使猎物跑开或阻止捕食者。我们会在第五章介绍势能函数。

这一章我们会探讨几个不同的追逐和躲避的算法,我们从最基本的方法开始。我们也给出了样例代码,这些代码可以在瓦片游戏和连续的移动环境中执行。

【原文】

Chapter 2. Chasing and Evading

In this chapter we focus on the ubiquitous problem of chasing and evading. Whether you're developing a spaceship shooter, a strategy simulation, or a role-playing game, chances are you will be faced with trying to make your game's nonplayer characters either chase down or run from your player character. In an action or arcade game the situation might involve having enemy spaceships track and engage the player's ship. In an adventure role-playing game it might involve having a troll or some other lovely creature chase down your player's character. In first-person shooters and flight simulations you might have to make guided missiles track and strike the player or his aircraft. In any case, you need some logic that enables nonplayer character predators to chase, and their prey to run.

The chasing/evading problem consists of two parts. The first part involves the decision to initiate a chase or to evade. The second part involves effecting the chase or evasion—that is, getting your predator to the prey, or having the prey get as far from the predator as possible without getting caught. In a sense, one could argue that the chasing/evading problem contains a third element: obstacle avoidance. Having to avoid obstacles while chasing or evading definitely complicates matters, making the algorithms more difficult to program. Although we don't cover obstacle avoidance in this chapter, we will come back to it in Chapters 5 and 6. In this chapter we focus on the second part of the problem: effecting the chase or evasion. We'll discuss the first part of the problem—decision making—in later chapters, when we explore such topics as state machines and neural networks, among others.

The simplest, easiest-to-program, and most common method you can use to make a predator chase its prey involves updating the predator's coordinates through each game loop such that the difference between the predator's coordinates and the prey's coordinates gets increasingly small. This algorithm pays no attention to the predator and prey's respective headings (the direction in which they're traveling) or their speeds. Although this method is relentlessly effective in that the predator constantly moves toward its prey unless it's impeded by an obstacle, it does have its limitations, as we'll discuss shortly.

In addition to this very basic method, other methods are available to you that might better serve your needs, depending on your game's requirements. For example, in games that incorporate real-time physics engines you can employ methods that consider the positions and velocities of both the predator and its prey so that the predator can try to intercept its prey instead of relentlessly chasing it. In this case the relative position and velocity information can be used as input to an algorithm that will determine appropriate force actuation—steering forces, for example—to guide the predator to the target. Yet another method involves using potential functions to influence the behavior of the predator in a manner that makes it chase its prey, or more specifically, makes the prey attract the predator. Similarly, you can use such potential functions to cause the prey to run from or repel a predator. We cover potential functions in Chapter 5.

In this chapter we explore several chase and evade methods, starting with the most basic method. We also give you example code that implements these methods in the context of tile-based and continuous-movement environments.

作者:Mr.Greedy 发表时间:2006-4-1  [所属栏目:资料翻译] | [返回首页]
日志-1  每页显示-1 

评论(共 条) 我要评论
{CommentTime} | {CommentAuthor} {CommentUrl}
{CommentContent}