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

target

target 方法是 Yuka.js 库中的一种寻路算法,基于 Dijkstra 算法实现。该方法可以用于找到两点之间最短路径。

语法

target(startPosition, targetPosition, costFunction);
  • startPosition:起点坐标信息。
  • targetPosition:终点坐标信息。
  • costFunction:代价函数,用于计算两点之间的距离。

参数

startPosition

  • 类型:Vector3

起点坐标信息,包括三个分量(x、y、z)。该参数为必选项。

targetPosition

  • 类型:Vector3

终点坐标信息,包括三个分量(x、y、z)。该参数为必选项。

costFunction

  • 类型:Function

代价函数,计算两点之间的距离。该函数有两个参数,表示两个坐标点,返回值为它们之间的距离。默认的代价函数使用欧几里得距离计算方法。

返回值

target 方法返回一个包含最短路径信息的对象。该对象包含以下属性:

  • path:最短路径的节点数组。
  • totalCost:起点到终点的总代价。
  • openList:openList 堆的状态信息。
  • closedList:closedList 堆的状态信息。

示例

以下示例展示了如何使用 target 方法计算两点之间的最短路径:

import { target } from 'yuka';

const startPosition = new Vector3(0,0,0);
const targetPosition = new Vector3(5,0,5);
const costFunction = function(a, b) {
    const dx = a.x - b.x;
    const dz = a.z - b.z;
    return Math.sqrt(dx * dx + dz * dz);
};

const result = target(startPosition, targetPosition, costFunction);
console.log(result.path);

参考文献

  • Dijkstra, E. W. (1959). "A note on two problems in connexion with graphs". Numerische Mathematik. 1: 269–271.