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

calculate

函数calculate是Yuka js库中SeparationBehavior类的一个方法。该方法的作用是计算物体之间的分离力(repulsion force),以避免碰撞。

语法

calculate( owner, neighbors, resultVector )

参数

  • owner:类型为Steerable的对象,表示当前物体。
  • neighbors:类型为Array,表示与当前物体相邻的物体数组。
  • resultVector:类型为Vector3,表示计算的结果向量。

返回值

该方法没有返回值,直接修改resultVector向量的值。

描述

对于一个物体而言,分离力是一种与其他物体相互作用的力,其方向指向物体相对的方向,力的大小取决于两个物体之间的距离,距离越小,则力越大。

该方法将遍历所有邻居物体,并计算出它们相对于当前物体的向量,然后将这些向量加和并归一化,所得的向量即为分离力向量。该方法将修改传入的结果向量,使其值为分离力向量。

示例

const owner = new Steerable();
const neighbor1 = new Steerable();
const neighbor2 = new Steerable();
const neighbors = [ neighbor1, neighbor2 ];
const resultVector = new Vector3();
const separationBehavior = new SeparationBehavior();

separationBehavior.calculate( owner, neighbors, resultVector );

参考