寻路基础

Time to talk about AI is approaching, but before diving into such a deep topic, there is one thing we 

must understand first in order to make things easier. And that is pathfinding.

I will only cover pathfinding using navigation meshes, and I will assume that you already know how to set those up.

As usual, here’s the documentation I’m assuming you’ve read:

You might be wondering how can we talk about pathfinding without talking about AI ? Well, the ability to

 make path searches can be useful to the player too. In Unreal Tournament 3′s CTF games,  a button

 press shows you the path to the flag. In Dead Space or Fable 2 and 3, you can have the path to your 

next objective. In Crazy Taxi, the arrow tells you when to turn according to the layout of the streets. 

I’ll be using that last example as a case in point.

The labyrinth

We have the simple maze pictured below:

maze

In this maze, we have to reach a goal (the green ball).

maze_goal

I want an arrow that tells me how to get to the goal.

The obivous solution

That’s fairly trivial. An actor is attached to the player and always turn to face the goal. I’m updating the arrow’s location in the controller’s PlayerMove, which is called every tick. Here’s the code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class SandboxPlayerController extends UDKPlayerController;
 
var SandboxNavigationActor NavArrow;
 
event PostBeginPlay()
{
     super .PostBeginPlay();
     NavArrow = Spawn( class 'Sandbox.SandboxNavigationActor' , self );
}
 
event Possess(Pawn aPawn, bool bVehicleTransition)
{
     //Here I'm attaching the arrow to the Pawn.
     local Vector arrow_loc;
     super .Possess(aPawn, bVehicleTransition);
     arrow_loc.Z = 64 ;
     if (NavArrow != none )
     {
         NavArrow.SetBase(Pawn);
         NavArrow.SetRelativeLocation(arrow_loc);
     }
}
 
state PlayerWalking
{
     function PlayerMove( float DeltaTime)
     {
         local Vector arrow_loc, arrow_dir;
         local Rotator final_dir;
         super .PlayerMove(DeltaTime);
         if (NavArrow != none )
         {
             if (SandboxGame(WorldInfo.Game).CurrentObjective != none )
             {
                 //This custom GameInfo simply contains a reference to the actor that represents my goal.
                 arrow_dir = SandboxGame(WorldInfo.Game).CurrentObjective.Location - NavArrow.Location;
                 final_dir = Rotator(arrow_dir);
             }
             NavArrow.SetRotation(final_dir);
         }
     }
}
The less obvious solution

The above solution is nice and easy, but let’s do something more sophisticated. I want the arrow to tell me where to turn in the maze. 

This can be done with pathfinding. Note that the following solution is not better than the first one, it’s just different. It all depends on what you want in your game.

As stated in the documentation, all pathfinding functionality is handled by a native class called NavigationHandle.

 Note that the Controller class (which is parent of both AIController and PlayerController) has a variable that stores a reference to a NavigationHandle and is called… NavigationHandle.

So, in order to gain pathfinding ability, you just need to initialize that variable at some point (PostBeginPlay sounds like a nice place):


1
2
3
4
5
6
7
event PostBeginPlay()
{
     super .PostBeginPlay();
 
     NavArrow = Spawn( class 'Sandbox.SandboxNavigationActor' , self );
     NavigationHandle = new( self ) class 'NavigationHandle' ;
}


Now the Controller is ready to do path searches. It’s a 4 step process:

  1. Decide where you want to go.
  2. Decide how you want to go there.
  3. Search for a path.
  4. Once the path is found, do something with it!

You choose where you want to go by adding a Goal Evaluator to the Navigation Handle. During the path search,

 the goal evaluator will be asked if the currently processed navmesh polygon fits the goal conditions. 

The documentation lists the different goal evaluators. Most of the time, the NavMeshGoal_At should be just what you need.

All goal evaluator classes have a wrapper static function that you use to add the evaluator to the handle, for instance:



1
class'NavMeshGoal_At'.static.AtActor(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective);



Note that the function that makes the actual evaluation is in native code, so you can’t override it.

 Meaning that you can’t make your own goal evaluators. Same goes for the path constraints I’ll be talking about a bit later.

There is a special kind of goal evaluator the documentation doesn’t talk about: The goal filters.

 They are evaluators you add on top of you main goal evaluator. Their role is to filter out possible goals that don’t match specific criteria. 

Below is a sample list of the available filters:

  • NavMeshGoalFilter_MinPathDistance
  • NavMeshGoalFilter_NotNearOtherAI
  • NavMeshGoalFilter_OutOfViewFrom

All these classes can be found in the Engine package.

Now that we know where we want to go, we need to choose how to get there. 

This is done by the Path constraints, which work the same way as goal evaluators. 

I remind you that you can’t create your own path constraints, so you’ll need to be imaginative 

with the paramaters of the one you’ve got at your disposal. In most simple cases, the NavMeshPathToward will be your best bet:


1
class 'NavmeshPath_Toward' . static .TowardGoal(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective);


Now you’ve got your path, you need to follow it. In our case, we will just orientate our arrow to follow the path. 

This is done by NavigationHandle.GetNextMoveLocation(). It tells you where to go according to your current position. 

However, if you wander off the path, it tends to get lost. To counter that, I request a new path each tick (certainly very inefficient,

 it would be best to make a new request only if we are lost (when GetNextMoveLocation() returns a null vector)):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//We're still in the PlayerController
state PlayerWalking
{
     function PlayerMove( float DeltaTime)
     {
         local Vector arrow_loc, arrow_dir;
         local Rotator final_dir;
         super .PlayerMove(DeltaTime);
             final_dir = NavArrow.Rotation; //keeping the previous rotation in case the path fails.
         if (NavArrow != none )
         {
             if (SandboxGame(WorldInfo.Game).CurrentObjective != none )
             {
                 class 'NavmeshPath_Toward' . static .TowardGoal(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective);
                 class 'NavMeshGoal_At' . static .AtLocation(NavigationHandle,SandboxGame(WorldInfo.Game).CurrentObjective.Location);
 
                 if (!NavigationHandle.FindPath())
                 {
                     `log( "COULD NOT FIND PATH" );
                 }
 
                 if (NavigationHandle.GetNextMoveLocation(arrow_dir, 64.0 ))
                 {
                     DrawDebugLine(Pawn.Location,arrow_dir, 255 , 0 , 0 , false );
                     arrow_dir -= NavArrow.Location;
                     final_dir = Rotator(arrow_dir);
                     final_dir.Pitch = 0 ; // GetNextMoveLocation returns a position near the ground, so I zero the pitch so the arrow doesn't point to the groung.
                 }
             }
             NavArrow.SetRotation(final_dir);
         }
     }
}


And we get the following:

The arrow is a bit jumpy, but that’s because I’m enforcing the rotation and the Navigation Mesh’s resolution

 is probably too low in regard of the level geometry. Making a smooth rotation would help making it look nicer but hey, it works!

The point here is to make sure we can get pathfinding to work. This will be one less thing to worry about when learning about AI.

【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
先看效果: https://pan.quark.cn/s/3756295eddc9 在C#软件开发过程中,DateTimePicker组件被视为一种常见且关键的构成部分,它为用户提供了图形化的途径来选取日期与时间。 此类控件多应用于需要用户输入日期或时间数据的场景,例如日程管理、订单管理或时间记录等情境。 针对这一主题,我们将细致研究DateTimePicker的操作方法、具备的功能以及相关的C#编程理念。 DateTimePicker控件是由.NET Framework所支持的一种界面组件,适用于在Windows Forms应用程序中部署。 在构建阶段,程序员能够通过调整属性来设定其视觉形态及运作模式,诸如设定日期的显示格式、是否展现时间选项、预设的初始值等。 在执行阶段,用户能够通过点击日历图标的下拉列表来选定日期,或是在文本区域直接键入日期信息,随后按下Tab键或回车键以确认所选定的内容。 在C#语言中,DateTime结构是处理日期与时间数据的核心,而DateTimePicker控件的值则表现为DateTime类型的实例。 用户能够借助`Value`属性来读取或设定用户所选择的日期与时间。 例如,以下代码片段展示了如何为DateTimePicker设定初始的日期值:```csharpDateTimePicker dateTimePicker = new DateTimePicker();dateTimePicker.Value = DateTime.Now;```再者,DateTimePicker控件还内置了事件响应机制,比如`ValueChanged`事件,当用户修改日期或时间时会自动激活。 开发者可以注册该事件以执行特定的功能,例如进行输入验证或更新关联的数据:``...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值