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

computeBoundingVolume

功能

计算 BVHNode 的边界盒体积。

语法

computeBoundingVolume()

参数

无。

返回值

无。

示例代码

const node = new BVHNode(objects);
node.computeBoundingVolume();

描述

BVHNode 的边界盒体积信息存储于 bv 中,在构建 BVH 树时需要计算每个 BVHNode 的边界盒体积。该函数用于计算该边界盒体积。

计算 BVHNode 的边界盒体积的方式如下:

  1. 如果该节点是叶子节点,则边界盒就是该节点包含的物体的边界盒;
  2. 如果该节点不是叶子节点,则边界盒是左右子树的边界盒的并集。

示例

class BVHNode {
  constructor(objects) {
    this.bv = new BoundingBox();
    this.objects = objects;
    this.left = null;
    this.right = null;
    this.construct(objects);
  }

  construct(objects) {
    // 构建 BVH 树

    // ...
  }

  computeBoundingVolume() {
    if (this.objects.length === 1) {
      this.bv.copy(this.objects[0].getBoundingBox());
    } else {
      const leftBV = new BoundingBox();
      const rightBV = new BoundingBox();
      this.left.computeBoundingVolume(leftBV);
      this.right.computeBoundingVolume(rightBV);
      this.bv.union(leftBV, rightBV);
    }
  }
}

附加说明

该函数通常在 BVH 树构建完毕后调用。在构建 BVH 树时建议将 BVHNode 的 boundingVolume 前置为一个空的 bounding box,计算完和左右子树的 bounding box 后再赋值到 boundingVolume 上,以避免不必要的计算量。