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

updateNeighborhood

功能描述

MovingEntity类是Yuka js库提供的一个实体类,updateNeighborhood方法可以更新实体周围的邻居列表。

方法定义

updateNeighborhood( entities, radius );

参数

  • entities: Array: 待检查邻居的实体列表。
  • radius: Number: 检查邻居的半径范围。

使用示例

const entity = new MovingEntity();
const entities = [ entity1, entity2, entity3 ];

entity.updateNeighborhood( entities, 10 );

方法实现

在updateNeighborhood方法内部,首先会清空当前实体的邻居列表,然后遍历待检查邻居的实体列表。对于每一个待检查的实体,通过距离方式检查它和当前实体之间的距离。如果距离在检查半径范围内,就认为它是当前实体的邻居,加入到邻居列表中。

MovingEntity.prototype.updateNeighborhood = function ( entities, radius ) {

    this.neighborhood.length = 0;
    const length = entities.length;

    for ( let i = 0; i < length; i ++ ) {

        const entity = entities[ i ];
        const distance = this.position.distanceTo( entity.position );
        
        if ( entity !== this && distance <= radius ) {

            this.neighborhood.push( entity );

        }

    }

};

注意事项

  • updateNeighborhood方法只适用于MovingEntity类,不适用于其他实体类。
  • 输入的实体列表中必须包含当前实体本身,否则会导致检查出错。
  • 检查半径范围的单位与实体位置的单位一致。例如,如果实体位置使用的是米,检查半径也应该以米为单位。