AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

source

source 是 Yuka.js 中的一个模块,它提供了一种算法,用于在网格中查找路径。

bfs

bfs 函数是 source 模块中的主要函数,它使用广度优先搜索算法查找两个坐标之间的最短路径。该函数接受以下参数:

  • start:一个包含两个属性 xy 的对象,表示路径的起点。
  • end:一个包含两个属性 xy 的对象,表示路径的终点。
  • grid:一个包含多个包含两个属性 xy 的对象的数组,表示网格中各个格子的坐标。
  • options:一个包含以下可选属性的对象:
    • allowDiagonal:一个布尔值,表示是否允许斜向移动。默认为 false
    • treatAsWall:一个数组,包含被视为墙壁的格子的坐标。默认为空数组。
    • coordinateMode:一个字符串,表示坐标系类型。可选值为 "cartesian""matrix"。默认为 "cartesian"
    • wrap:一个布尔值,表示是否允许从网格的一侧穿越到另一侧。默认为 false
    • diagonalCost:一个数字,表示斜向移动的路径代价。默认为 1.4

bfs 函数将返回一个数组,表示找到的最短路径。如果找不到可行的路径,则返回空数组。

示例代码

以下是使用 bfs 函数的示例代码:

import { source } from 'yuka';

// 定义网格
const grid = [
  { x: 0, y: 0 }, { x: 0, y: 1 }, { x: 0, y: 2 },
  { x: 1, y: 0 }, { x: 1, y: 1 }, { x: 1, y: 2 },
  { x: 2, y: 0 }, { x: 2, y: 1 }, { x: 2, y: 2 }
];

// 定义起点和终点
const start = { x: 0, y: 0 };
const end = { x: 2, y: 2 };

// 使用广度优先搜索算法查找最短路径
const path = source.bfs( start, end, grid );

console.log( path );    // 输出 [{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 1 }, { x: 2, y: 2 }]

参考文献