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

lineOfSightTest

MovingEntity(lineOfSightTest)方法是Yuka js库的一部分,用于测试两个实体之间是否存在可见路径。该方法使用Bresenham算法,可计算两个点之间的最短路径。此方法返回一个 Boolean 值,true表示实体之间存在可见路径,否则为false。

用法

entity.lineOfSightTest(targetEntity, obstacles)

参数

  • targetEntity: {MovingEntity} - 目标实体,需要验证是否存在可见路径。
  • obstacles: {array} - 障碍物的数组,该实体和目标实体之间的路径会与这些障碍物进行碰撞检测。

返回

  • result: {boolean} - 如果存在可见路径则返回true,否则返回false。

示例

import { MovingEntity } from 'yuka';

const entity = new MovingEntity();

entity.position.set( 1, 1, 1 );
entity.setBoundingRadius( 1 );
entity.setMaxSpeed( 5 );

const targetEntity = new MovingEntity();
targetEntity.position.set( 10, 10, 10 );
targetEntity.setBoundingRadius( 1 );

const obstacles = [ new THREE.Mesh( new THREE.BoxBufferGeometry( 2, 2, 2 ) ) ];
obstacles[ 0 ].position.set( 6, 6, 6 );

if ( entity.lineOfSightTest( targetEntity, obstacles ) ) {
  console.log( 'There is a visible path to target entity!' );
} else {
  console.log( 'There is no visible path to target entity!' );
}

异常

  • 如果targetEntity不是MovingEntity实例,则抛出“TypeError: targetEntity is not an instance of MovingEntity”异常。
  • 如果obstacles不是数组,则抛出“TypeError: obstacles is not an array”异常。