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

lookAt

描述

该方法用于计算一个MovingEntity对象朝向目标位置时应该旋转的角度。

语法

lookAt(targetPosition: Vector2): void

参数

  • targetPosition:目标位置,类型为 Vector2。

返回值

该方法没有返回值。

示例

const entity = new MovingEntity();
const target = new Vector2(100, 200);
entity.lookAt(target);

实现过程

该方法的实现过程如下:

  1. 计算目标位置与实体当前位置之间的向量。
  2. 将这个向量转换为角度,即 Math.atan2(targetPosition.y - this.position.y, targetPosition.x - this.position.x) * (180 / Math.PI),这个角度表示了实体应该朝向的方向。
  3. 将实体的朝向角度设置为上一步计算的角度。

异常

如果传入的参数不是 Vector2 类型,则会抛出 TypeError 异常。