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

intersectPlane

描述

intersectPlane 是 Yuka js 库中用于计算射线与平面相交点的函数。

语法

intersectPlane(plane: Plane, ray: Ray): Vector3 | null

参数

  • plane:表示一个平面的 Plane 对象实例。
  • ray:表示一个射线的 Ray 对象实例。

返回值

如果射线与平面相交,则返回交点的三维向量坐标,否则返回 null

示例

import { Vector3, Ray, Plane } from 'yuka';

const position = new Vector3( 0, 0, 0 );
const direction = new Vector3( 0, 1, 0 );
const ray = new Ray( position, direction );
const normal = new Vector3( 0, 1, 2 );
const distance = 5;
const plane = new Plane( normal, distance );
const intersectionPoint = intersectPlane( plane, ray );

if ( intersectionPoint !== null ) {
	console.log( intersectionPoint );
}

实现原理

intersectPlane 函数计算射线与平面的交点时,首先需要求出射线的参数方程:

$$ \vec p(t) = \vec P + t\vec d $$

其中 $\vec P$ 表示射线的起点,$\vec d$ 表示射线的方向向量,$t$ 表示未知的参数。

接下来,我们需要将射线的参数方程代入平面的方程:

$$ \vec n \cdot (\vec p - \vec q) = 0 $$

其中,$\vec n$ 表示平面的法线向量,$\vec q$ 表示平面上的任意一点。

将射线的参数方程带入上式,得到:

$$ \vec n \cdot (\vec P + t\vec d - \vec q) = 0 $$

移项可得:

$$ t = \frac{\vec n \cdot (\vec q - \vec P)}{\vec n \cdot \vec d} $$

如果上式的分母 $\vec n \cdot \vec d=0$,则说明射线与平面平行,此时返回 null

如果分母不为 0,则代入射线的参数方程得到交点坐标:

$$ \vec p(t) = \vec P + \left(\frac{\vec n \cdot (\vec q - \vec P)}{\vec n \cdot \vec d}\right)\vec d $$

相关链接