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

intersectsOBB

说明

intersectsOBB函数用于判断两个OBB(有向包围盒)是否相交。

OBB是一种轴对齐的包围盒,是由一个中心点、三个轴和三个半轴长度来定义的。要求轴必须是彼此相互垂直,半轴长度必须是正数。

本函数假设两个OBB的各个轴已经彼此垂直,并且都是单位向量。

语法

intersectsOBB(A: OBB, B: OBB): boolean

参数

  • A: OBB:第一个OBB
  • B: OBB:第二个OBB

返回值

  • boolean:表示两个OBB是否相交。相交返回 true,否则返回 false

示例

const A = {
  center: [0, 0, 0],  // 中心点
  axis: [
    [1, 0, 0],        // x 轴
    [0, 1, 0],        // y 轴
    [0, 0, 1],        // z 轴
  ],
  halfSize: [1, 1, 1], // x 方向半轴长度、y 方向半轴长度、z 方向半轴长度
};

const B = {
  center: [2, 0, 0],
  axis: [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
  ],
  halfSize: [1, 1, 1],
};

const result = intersectsOBB(A, B); // 判断 A、B 是否相交

console.log(result); // false

实现

function intersectsOBB(A, B) {
  for (let i = 0; i < 3; i++) { // 遍历三个轴
    const aAxis = A.axis[i];
    const bAxis = B.axis[i];
    const interAxis = [
      aAxis[0] * bAxis[0] + aAxis[1] * bAxis[1] + aAxis[2] * bAxis[2],
      aAxis[0] * bAxis[1] - aAxis[1] * bAxis[0],
      aAxis[0] * bAxis[2] - aAxis[2] * bAxis[0],
    ];

    let n = 0;
    let dA = Math.abs(aAxis[0] * A.halfSize[0]) +
             Math.abs(aAxis[1] * A.halfSize[1]) +
             Math.abs(aAxis[2] * A.halfSize[2]);
    let dB = Math.abs(bAxis[0] * B.halfSize[0]) +
             Math.abs(bAxis[1] * B.halfSize[1]) +
             Math.abs(bAxis[2] * B.halfSize[2]);
    let d = Math.abs(interAxis[0] * A.center[0] + interAxis[1] * A.center[1] + interAxis[2] * A.center[2] - 
                     interAxis[0] * B.center[0] - interAxis[1] * B.center[1] - interAxis[2] * B.center[2]);
    if (d > dA + dB) { // 未相交
      return false;
    }
  }

  return true; // 相交
}

参考资料