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

cost

cost是NavEdge的一个方法,用于计算两个节点之间距离的成本。本文档将介绍cost的参数和用法。

参数

  • start:起点节点对象(必选)
  • end:终点节点对象(必选)
  • distanceFunction:计算距离的函数(可选)

返回值

  • 成本(number类型)

用法

基本用法

const node1 = { x: 0, y: 0 };
const node2 = { x: 3, y: 4 };

const cost = NavEdge.cost(node1, node2);

console.log(cost); // 5

以上代码将输出5,因为节点1和节点2之间距离是5。

自定义距离计算函数

如果要使用自定义的距离计算函数,可以将函数作为distanceFunction参数传入。这个函数的参数是两个节点对象,返回值是节点间的距离(number类型)。

const node1 = { x: 0, y: 0 };
const node2 = { x: 3, y: 4 };

const distanceFunction = (start, end) => {
  const dx = end.x - start.x;
  const dy = end.y - start.y;

  return Math.sqrt(dx * dx + dy * dy);
};

const cost = NavEdge.cost(node1, node2, distanceFunction);

console.log(cost); // 5

以上代码将输出5,因为自定义的距离计算函数也满足节点1到节点2距离为5。