使用 XNA/AI/AI 引擎建立遊戲
如今,建立一個針對特定主題的整體解決方案是普遍的做法。這些解決方案通常被稱為引擎。每天都會湧現出新的引擎。例如,針對 3D 圖形、聲音、網路甚至人工智慧,尤其是遊戲的人工智慧。這些用於遊戲的 AI 引擎解決了遊戲開發過程中經常出現的幾個問題,例如尋路、決策、學習、移動、戰術和轉向行為等。因此,存在著許多 AI 引擎和庫,它們提供了一些這些演算法。這些演算法使您能夠使您的遊戲更加智慧和具有挑戰性。
SharpSteer 由 Bjoern Graf 和 Michael Coles 開發,它是 OpenSteer(C++)的 C# 版本,OpenSteer 是一個開源庫,用於幫助構建遊戲和動畫中自主角色的轉向行為,它根據 MIT 許可證進行分發 [1]。它的最後一個版本釋出於 2008 年 3 月,專為 XNA 2.0 設計,但它也適用於 3.1。人們希望將其移植到 XNA 4.0,但轉換尚未完成 [2]。顧名思義,SharpSteer 的職責在於轉向行為,例如凝聚、分離、對齊等等。它的當前版本包含 200 個模擬鳥群物體(也稱為鳥群)的演示 [3]。在 SharpSteer 中,鳥群可以是遊戲中任何東西(足球運動員、敵方士兵、汽車)。它至少需要具有 SharpSteer 的 IVehicle 介面。最重要的類是 SteerLibrary.cs。它是 SharpSteer 的核心,包含了轉向行為的主要演算法。
- 對齊行為:沿附近其他車輛的平均方向移動

如果有另一輛車在淡藍色區域內,它將影響這輛白色的車。 [3]
// Alignment behavior
public Vector3 SteerForAlignment(float maxDistance, float cosMaxAngle, List<IVehicle> flock)
- 凝聚行為:向附近車輛的平均位置移動。
// Cohesion behavior
public Vector3 SteerForCohesion(float maxDistance, float cosMaxAngle, List<IVehicle> flock)
- 分離行為:遠離附近其他車輛以避免擁擠
// Separation behavior -- determines the direction away from nearby boids
public Vector3 SteerForSeparation(float maxDistance, float cosMaxAngle, List<IVehicle> flock)
這三個函式獲得了三個相等的引數:浮點值 maxDistance 定義了其他車輛影響此車輛的臨近區域。浮點值 cosMaxangle 定義了其他車輛不會影響此車輛的角度邊界。顯然,IVehicle 列表包含所有可能影響此車輛的車輛。但這還不是全部。還有
- 迴避行為:遠離特定車輛。
// evasion of another this
public Vector3 SteerForEvasion(IVehicle menace, float maxPredictionTime)
值 menace 是應該避免的車輛。值 maxPredictionTime 是預測 menace 未來位置的時間點。
此外還有數十種其他行為
public Vector3 SteerForWander(float dt)
// Seek behavior
public Vector3 SteerForSeek(Vector3 target)
// Flee behavior
public Vector3 SteerForFlee(Vector3 target)
// Path Following behavior
public Vector3 SteerToFollowPath(int direction, float predictionTime, Pathway path)
public Vector3 SteerToStayOnPath(float predictionTime, Pathway path)
// Obstacle Avoidance behavior
public Vector3 SteerToAvoidObstacle(float minTimeToCollision, IObstacle obstacle)
// avoids all obstacles in an ObstacleGroup
public Vector3 SteerToAvoidObstacles<Obstacle>(float minTimeToCollision, List<Obstacle> obstacles)
where Obstacle : IObstacle
// Unaligned collision avoidance behavior: avoid colliding with other
// nearby vehicles moving in unconstrained directions. Determine which
// (if any) other other this we would collide with first, then steers
// to avoid the site of that potential collision. Returns a steering
// force vector, which is zero length if there is no impending collision.
public Vector3 SteerToAvoidNeighbors<TVehicle>(float minTimeToCollision, List<TVehicle> others)
where TVehicle : IVehicle
// Given two vehicles, based on their current positions and velocities,
// determine the time until nearest approach
public float PredictNearestApproachTime(IVehicle other)
// Given the time until nearest approach (predictNearestApproachTime)
// determine position of each this at that time, and the distance
// between them
public float ComputeNearestApproachPositions(IVehicle other, float time)
// avoidance of "close neighbors" -- used only by steerToAvoidNeighbors
// XXX Does a hard steer away from any other agent who comes withing a
// XXX critical distance. Ideally this should be replaced with a call
// XXX to steerForSeparation.
public Vector3 SteerToAvoidCloseNeighbors<TVehicle>(float minSeparationDistance, List<TVehicle> others)
where TVehicle : IVehicle
// ------------------------------------------------------------------------
// pursuit of another this (& version with ceiling on prediction time)
public Vector3 SteerForPursuit(IVehicle quarry)
public Vector3 SteerForPursuit(IVehicle quarry, float maxPredictionTime)
public Vector3 SteerForTargetSpeed(float targetSpeed)
Simple AI 是 Piotr Witkowski 為 XNA 開發的引擎,它具有網格地圖、尋路演算法、路徑跟蹤以及尋路、跟隨路徑、到達、保持編隊等行為。 [4] 與專門針對凝聚、對齊和分離等轉向行為的 SharpSteer 相反,該引擎專注於使用 A* 進行各種圖和尋路。因此,行為與表示為網格的圖相關聯,並且它們依賴於尋路演算法。
該庫非常輕量級。它只提供了 A*、深度和廣度搜索。它適用於 XNA 3.1 和 4.0。由於體積小巧,它具有高度適應性。
如果您想編寫自己的引擎,您需要考慮其結構設計。您應該考慮其用途,即它是一個更通用的解決方案還是專門針對特定型別的遊戲。此外,必須有一個用於決策的通用機制,即哪種行為是當前行為,即使用了哪種 AI 演算法。因此,每個演算法都必須服從這種機制。儘管如此,您的 AI 引擎機制包含一個介面,用於您的演算法與遊戲世界互動,因為沒有感官(輸入)就無法做出適當的反應(輸出)。當然,行為和智慧行動遊戲物件的動畫之間應該存在聯絡。此外,您的引擎架構使它適用於擴充套件引擎本身及其演算法,任何希望編寫自己的行為或安裝新 AI 技術的人都可以這樣做。與往常一樣,您應該重視可重用性、適應性和可維護性。因此,完整的 AI 引擎將擁有一箇中心池,其中包含可以應用於任何型別的遊戲物件的任何型別的遊戲(無論是射擊遊戲還是文字冒險遊戲)的預定義 AI 演算法,如果 AI 引擎是一個通用解決方案。
應該討論可用的引擎(可能不僅僅是 XNA),用於處理 AI。
例如,Nelxon 網站 [7] 列出了以下用於人工智慧的引擎
- Engine Nine – 包含尋路和轉向行為
- SharpSteer – 我發現它有很多用途……
- 基於狀態機的行為模型 - 好文章,樣本舊了……
- 轉向行為、避障 - 好的示例(西班牙語)
可以在此處找到一些有趣的程式碼示例:http://create.msdn.com/en-US/education/catalog/?devarea=11
nexuschild 深入研究
- ↑ http://opensteer.sourceforge.net/
- ↑ http://sharpsteer.codeplex.com/
- ↑ a b http://www.red3d.com/cwr/boids/
- ↑ http://simpleai.codeplex.com/
- ↑ http://xnapathfindinglib.codeplex.com/team/view
- ↑ 伊恩·米林頓著《遊戲人工智慧》
- ↑ http://www.nelxon.com/681/xdsk2/ Nelxon,XSDK2 - 用於使用 XNA 4.0 的開發人員的資源
