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

intersectsBoundingSphere

该函数用于判断一个OBB与一个球是否相交。

语法

OBB.intersectsBoundingSphere(sphere);

参数

  • sphere - 一个Sphere对象,表示球。

返回值

  • true - 如果OBB与球相交。
  • false - 如果OBB和球不相交。

示例

const obb = new THREE.OBB(new THREE.Vector3(0, 0, 0), new THREE.Vector3(1, 1, 1), new THREE.Matrix4());
const sphere = new THREE.Sphere(new THREE.Vector3(1.5, 0, 0), 1);
const isIntersecting = obb.intersectsBoundingSphere(sphere); 
// 返回 false

实现

要检测一个OBB是否与一个球相交,我们需要沿着球心到OBB中心的方向投射球,确定它是否与OBB相交。如果同一方向的两个向量的距离大于球的半径,则OBB与球不相交。

实现代码如下:

intersectsBoundingSphere(sphere) {
  const center = this.center;
  const r = sphere.radius;

  // 计算球心到OBB中心的方向
  const toCenter = new THREE.Vector3().subVectors(center, sphere.center);

  // 将球心沿球心到OBB中心的方向投射到OBB的边线上
  const closestPoint = this.clampPoint(toCenter, new THREE.Vector3());

  // 计算球心到OBB的边线的距离
  const distance = new THREE.Vector3().subVectors(closestPoint, center).length();

  // 判断球心到OBB的边线的距离是否小于等于球半径
  return distance <= r;
}

参考文献