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

path

路径(Path)是指在图形学中对于一个物体的图形描述,通常指定物体边缘和形状的集合。

在Yuka js库中,OnPathBehavior组件可以让游戏物体跟随指定路径移动。为了使用OnPathBehavior组件,需要先创建一个路径对象,这个对象可以通过路径数据创建,数据格式可以是一个数组,每个子项代表路径上的一个点。

下面是一个路径数据示例:

const pathData = [
  [ 0, 0 ],
  [ 10, 0 ],
  [ 10, 10 ],
  [ 0, 10 ],
  [ -10, 10 ],
  [ -20, 10 ],
  [ -20, 0 ],
  [ -10, 0 ]
];

为了在游戏中使用这个路径对象,需要对OnPathBehavior进行配置,在配置中需要传入路径对象以及相关参数,例如是否循环、移动速度等。同时,在运行时,OnPathBehavior会根据配置自动计算当前位置和方向,并更新游戏物体的实际位置和朝向。

下面是OnPathBehavior组件配置的一个示例:

const pathBehavior = new OnPathBehavior( pathObject, {
  loop: true,
  speed: 10,
  startDirection: new Vector3( 0, 0, -1 ),
  endDirection: new Vector3( 0, 0, 1 )
} );

在这个示例中,我们传入了路径对象pathObject,并对loopspeedstartDirectionendDirection等参数进行了配置。具体参数的含义如下:

  • loop: 布尔值,指示是否循环移动,默认为false
  • speed: 移动速度,单位为米每秒,默认为1
  • startDirection: 初始方向向量,指示物体的起始朝向,默认为new Vector3( 0, 0, -1 )
  • endDirection: 终止方向向量,指示物体的终止朝向,默认为new Vector3( 0, 0, 1 )

配置好OnPathBehavior组件后,可以调用其update()方法,在每次游戏循环中更新游戏物体的位置和朝向。这个方法接受一个时间增量参数,用于计算物体的移动距离。例如:

function update( deltaTime ) {
  pathBehavior.update( deltaTime );
}

在以上示例中,我们将deltaTime作为参数传递给了pathBehavior.update()方法,这样OnPathBehavior组件就会根据移动速度计算出物体的移动距离,并更新其位置和朝向。

总之,通过OnPathBehavior组件,可以让游戏物体沿着路径移动,提供了丰富的移动方式,可以让游戏中的物体更加丰富和生动。