原文:http://www.xuebuyuan.com/1907012.html?mobile=1
原文的原文:http://www.ai-blog.net/archives/2008_07.html (并不能打开)
个人翻译,欢迎指正
(正文开始)
我通常会尽力避免说一些会被误解为是对其他游戏或者行业开发者的批评的话。
但是这次我得破个例。
我得谈一谈路径搜索(pathfinding)方面的几个问题。为了证明这些问题的存在,我觉得有必要录个视频(并没有见到,估计在墙外--译者注)......which will hopefully be taken in the humorous and lighthearted spirit in which it was intended(不知道怎么翻译,下同)。
所有这些片段都是上周使用这些游戏的最新版本录制的。
As you can see, 我们与鲁棒的路径规划之间还隔着十万八千里呢。。。。。。甚至一些百万销量/3A质量的游戏中都不乏问题。
虽然并不见得是一个普遍问题,一些现代游戏里确实也有高质量的路径搜索,在“上面”所展示的那些游戏里的大多数情况下,路径搜索工作的还都凑合。
But 依然有非常多的游戏,其中的pathfinding与上世纪90年代的游戏相比毫无进步。
(Note:之所以你在这里看到的游戏大都是PC上的RPG游戏,只是因为方便。我现在刚好有这些游戏。这里所探讨的问题其实是与游戏类型和平台都没有关系的。在主机游戏中也同样存在大量类似问题)
据我所知,这些游戏中的大多数都是利用路径点(waypoint graphs)进行寻路的。我认为这正是你在”上面“视频中发现的一些pathfinding问题的原因所在,同时也是我们在行业中面对的大多数pathfinding问题的罪魁祸首。
我相信现如今waypoint graphs是应废除掉的。本文将阐述waypoint方法的局限,并lays out a five-point argument for a better approach.(译注大概是陈述5条与navmash相比的不足)
Back in the 1980s and 1990s, 受限于技术, 在pathfinding中使用waypoint是有一定道理的。
But, 现如今游戏已经发展成为一个数十亿美元的行业,我们的目标平台也拥有着数个核心和持续增长的内存,这已足够承担起正确的pathfinding。
在现今行业的AI开发者中有着这样一句话:“路径搜索问题已经解决了。(pathfinding is solved.)”在现代游戏中面临的所有pathfinding问题我们都已经有了好的解决方法,我们只是不总是用它们而已。
There's no reason not to have pathfinding that works in every game, every time.
下面我来做一个详细的解释。
为什么waypints已不再适用
让我先给你们看看典型的waypoint长什么样。这里是魔兽世界中暴风城的一角:
图1.魔兽世界中暴风城的一角
这是一个典型的waypoint在该区域中可能看上去的样子:
图2.注有waypoint的图1
There's another way to do it, 它涉及使用凸多边形来决定哪里是AI角色可行走的。这种表示法可以给AI提供更多的环境信息,并提供对做出更加智能化决定的支持。
这里是导航网格(navigation mesh)看上去的样子:
图3.注有navmash的图1
下面是waypoint的5大条“罪状”:(译注:简单翻译,原文语气没这么激烈,莫较真 ;D)
1. Some kinds of game worlds require a ridiculous number of waypoints.(一些游戏中需要的waypoints数量超乎你的想象)
宽阔的区域中通常需要大量的waypoints散落在地图的各个角落以求角色能够灵活的移动。
on the other hand,A navigation mesh 通常仅需要少数的多边形来表示这片区域就足够了。
来一个例子:
下面是魔兽世界中一个叫“Halaa”的城镇,这是一个相当大且颇开阔的区域,NPC们通常需要在该区域内进行寻路。为了简化,我去掉了一些诸如旗帜、喷泉之类的细节。
图4. Halaa镇的简化版
在基于waypoint的系统中,为了覆盖整个地图,我们需要放置大量的waypoints。即便如此,我们的NPC们依然会在行走时出现大量的之字形路线。除非我们放置比这里展示的还要多得多的waypoints。
图5.Halaa with a waypoint graph
on the other hand, 采用 navigation mesh, 我们就可以使用很少的凸多边形来描述这块区域
图6.Halaa with a navigation mesh
navigation mesh的简单意味着当我们在运行时调用pathfinding算法时将不再需要搜寻那么多的节点,而这导致pathfinding会更快。
2. They make characters "zig-zag" as they move.(角色在移动时会走“之字形”路线)
使用waypoints graphs 你的角色必须沿着你创建的路线行走。就像是角色在通过铁轨移动,这意味着几乎从来都无法做到线路最优。。。因为最优路径几乎从来不会与你的图恰好匹配。
这造成了非自然的pathfinding——尤其是,角色会不自然地走起“之字形”路线。
来看个例子,我们希望角色从A走到B:
图7.Halaa中的两点
在waypoint中路径看起来会像这样:
图8.利用waypoint graph从A到B路径
As you can see, 角色在沿着黄色路径行走时将会一而再的转弯。
理想情况下, 在找到路径之后,我们可以利用某种方法对其进行调整使之更平滑一些。。。比如在节点之间创建平滑的样条曲线(Catmull-Rom spline )。
问题在于,waypoint网没有提供关于路径之外的任何信息,这导致想要进行安全可靠的路径平滑和调整是绝不可能的。
当样条线必须创建在waypoint graph之外时,你该怎么去创建它呢?事实上你沿着这样的曲线行走恐怕一不小心就会掉下桥去了。
你可能会想一定存在某种更简单的方法。不幸的是,在waypoint方法中,这样的方法并不存在。我们与waypoint graph哪怕有一丝偏差,就有可能会掉下悬崖或撞上南墙。我们永远无法知道这一点,因为graph没有保存任何相关数据。
因此,为了保全性命,我们必须保持最悲观的姿态,时刻紧紧依附在waypint 网上。
然而,另一方面,navigation mesh形成的路径将看上去像这样:
图9.通过navmesh从A到B
由于我们已知哪里是可安全行走的区域, 所以只要你喜欢,你可以以任何方式平滑路径,只要你保证路径在网格内便可。
(
“哦,”你说,“但是我只需要在waypoint中加入更多的链接来把所有的路线都平滑起来!我把地图中所有的节点都两两连接起来。”
“但那样会照成指数爆炸的,”我答道......“以上图为例,你将需要多添加40-50个额外的连接。随着区域的增加,你需要的连接数会到达O(N^2)级别。”
“OK,”你说,“这样的话,那我就使用3个相互邻接的waypoints作为一组来表示一个‘open’从而标记出一个区域,这样我的AI就可以在该区域中随意行走了。”
“这不正是一个多边形区域吗......而你已经有了navigation mesh了呀”,我答道。
)
navigation mesh可以精确地告诉我们哪些区域是AI可以行走的这一事实意味着我们可以使用样条曲线轻松地平滑路径。我们唯一需要做的就是确保样条曲线始终在网格内部。
如下图,回到我们的例子,红色是waypoint path, 蓝色是在navmesh中的平滑路径:
图10.一条waypoint path(红)和一条平滑了的navigation mesh path (蓝)
3. They don't allow path correction. That makes robust dynamic obstacle avoidance difficult, if not impossible.( (waypoint)不允许路径更正,导致实现出鲁棒的动态障碍物避免虽非不可能,但也是极其困难的)
这使得pathfinding system几乎不可能处理好具有动态障碍物的情况。
让我们在桥上放个大檀木箱子。
在waypoint graph中,呃。。。我们完蛋了——如果箱子刚好挡住了graph,那么应该从左还是从右边绕过去呢?我们将毫无头绪。甚至,如果箱子完全把路堵死了,我们会需要绕一个大弯才行。当然我们也可以猜一个(方向)——只要你不怕淹死。
图11. 在桥上放一个沉重的箱子
然而,在navmesh中就简单多了,因为我们知道哪些区域是可以行走的。我们只消做个简单的光线投影,就可以轻松绕过。(We do a bit of raycasting against the barrel and adjust our path around it, keeping our path (and ourselves) safely on the navigation mesh.)
图12.绕过箱子
当然,如果你在waypoint graph中放的节点比草都密,用waypoint也是可以做到的。(同时你pathfinding的速度也必然慢的离奇)。
我不知道你怎么想, 但我可是宁愿我的A*算法运行在一个大的多边形上而不是数百个waypoints上。
4. They don't work robustly for characters that move differently.(不同角色移动不同时waypoint方法力不从心)
5. They don't have enough data to support what your AI needs beyond pathfinding.(除了pathfinding之外别的什么都不支持)
AI搜索空间不只是用来导航,AI们还需要根据路径数据来决定如何在世界中移动。Q & A
But isn't it slower to do pathfinding on a navigation mesh?(在navmesh中进行pathfinding会不会更慢啊?)
一点也不。和waypoint graph 一样,navmesh也是图,并且二者核心的pathfinding方式是非常相似的。区别在于navmesh中每一个节点有一个多边形与之关联。But don't navigation meshes take a lot more memory than waypoints?(占用更多内存)
正确运用怎不会。Most of the examples you showed were role-playing games. Why don't I see the same kinds of problems in first-person shooters?(如果不是RPG游戏,在FPS游戏中没这问题吧?)
问题依然存在的。只是在很多FPS游戏中比较难发现。Look, I understand everything you said, but designers need to be able to put down cover points, patrol paths, and so on. That's just essential for creating the AI in our games.(你说的这些我都明白,但是关于AI很关键的一点是,设计者需要能够在地图上放置覆盖点、巡逻路径之类的)
你依然能够做到!OK, but isn't it a lot of work to place a navigation mesh?(好吧, 但是弄个导航网格出来肯定很费事吧?)
通常来说,手动放置一个nav mesh不会比放置一个waypoint 网需要更多劳动。无论二者哪一种计数,你创建它所花的时间相对于在游戏中测试它所花的时间来说根本不值一提。What if I want to have AIs walk up and down stairs? My designers don't want to have to lay down a nav mesh polygon for every single step in the staircase.(让AI们爬楼梯会怎样?设计师们可不想在每一级台阶上都放一个 nav mesh)
一个nav mesh仅仅是表示了一块可行走区域。它并不会取代碰撞网格——你的NPC们依然使用碰撞系统处理游戏中的碰撞。Can't I just give each point in my waypoint graph a radius, so I have circles instead of points? That gives me a representation of what areas are walkable. That's how they do it in robotics, too ...(我能把waypoint网中的每个节点一个半径将其替换为一个个的圆形吗?这样我就能表示出哪些区域是可行走的了。在机器人学中他们就是这样做的)
在某些情况下这种方法是可行的。但是游戏中的大多数地图都会有大量的人类建筑,充满了生硬的街道和楼房。圆形不能很好的表示这些区域,想做到多边形那样的覆盖范围会很困难。My game already has a collision mesh. Can't I just use that for pathfinding?(能用碰撞网格代替导航网格不?)
你可以尝试,但这并不是好主意。All the examples as presented show the nav mesh as a 2D projection onto the space. How would you handle a case like a bridge over a road, wherein you could go over or under. Or a building with multiple floors?(3D的场景怎么处理?比如立交桥,6层小楼这样?)
I don't believe in a one-size-fits-all approach. Different games require different representations for pathfinding.(我不相信存在一劳永逸的办法,不同的游戏就需要不同的方法)
如果你是在做一个完全2D的策略游戏,那么基于栅格的方法通常是更好的,因为栅格可以让你随机访问任何一个单元。Suppose I were interested in implementing navigation meshes in my game. Has anything been published to help me figure out how to build the meshes, do pathfinding on them, and optimize them for my game?(假设我信了你,那现在在哪里有哪些东西可以帮助我理解如何建立网格,并在其上进行路径搜索,以及为我的游戏优化它们?(译注:就是read more))
下面是我知道的一些:"Simplified 3D Movement and Pathfinding Using Navigation Meshes" by Greg Snook (Game Programming Gems)
"Polygon Soup for the Programmer's Soul: 3D Pathfinding" by Greg Hjelstrom and Patrick Smith (GamaSutra.com)
"Building a Near-Optimal Navigation Mesh" by Paul Tozour (AI Game Programming Wisdom)
"Efficient Navigation Mesh Implementation" by John C. O'Neill (Journal of Game Development, March 2004)
"Search Space Representations" by Paul Tozour (AI Game Programming Wisdom 2)
"A Fast Approach To Navigation Meshes" by Stephen White and Christopher Christensen (Game Programming Gems 3)
"Choosing a Relationship Between Path-Finding and Collision" by Thomas Young (Game Programming Gems 3)
"Improving on Near-Optimality: More Techniques for Building Navigation Meshes" by Fredrik Farnstrom (AI Game Programming Wisdom 3)
"Smoothing a Navigation Mesh Path" by Geraint Johnson (AI Game Programming Wisdom 3)
"Dynamically Updating a Navigation Mesh via Efficient Polygon Subdivision" by Paul Marden and Forrest Smith (AI Game Programming Wisdom 4)
"Intrinsic Detail in Navigation Mesh Generation" by Colt McAnlis and James Stewart (AI Game Programming Wisdom 4)
"Navigation Mesh Generation: An Empirical Approach" by David Hamm (AI Game Programming Wisdom 4)
"Navigation Mesh Generation in Highly Dynamic Worlds" by Ramon Axelrod (AI Game Programming Wisdom 4)
"Crowds in a Polygon Soup: Next-Gen Path Planning" by David Miles (http://www.navpower.com/gdc2006_miles_david_pathplanning.ppt)
如果你还知道更多,请 e-mail 我。How many games have actually used navigation meshes successfully?(成功案例有哪些?)
简单说几个:Halo 3
First Encounter Assault Recon (F.E.A.R.)
Counter-Strike: Source
Metroid Prime
Metroid Prime 2: Echoes
Metroid Prime 3: Corruption
Jak and Daxter: The Precursor Legacy
Jak II
Jak 3
Uncharted: Drake's Fortune
Scarface: The World is Yours
OK, maybe it works for you, but we used waypoints in all of our previous games, and they worked fine.
Like they say, if it ain't broke, don't fix it ... and we haven't run into any of the problems you're talking about.
(你用着好,我们一直用waypoint也用得挺好的啊。俗话说的好“没破你修啥”。。。你上面说的问题我们也并没有遇到呀)
人要有远见,想象10年20年以后吧。In that kind of time frame, do you think your games might have lots of different types of AI-controlled characters with different shapes, sizes, and movement capabilities?
Will players have AI henchmen that they expect to be just as intelligent as themselves?
Will your game worlds be significantly larger, more complex, and more dynamic than they are today?
Will you have huge crowds of AI characters -- so many that just using simple steering and obstacle avoidance are no longer adequate to make them coordinate with each other effectively?
Will your games have realistic physics and huge amounts of physically-simulated objects, and will players be able to use the physics to mess with the AI characters in every way imaginable?
Will players be able to change the game world until it's virtually unrecognizable?
Will there be AIs in multiplayer that are expected to pass for human players?
(blablabla...自己看吧——译注)