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

applyMatrix4

Yuka.js库中AABB(轴对齐包围盒)的applyMatrix4方法可用于将指定的矩阵应用于AABB的八个顶点,并返回新的AABB。

语法

applyMatrix4(matrix4: Matrix4): AABB

参数

  • matrix4: Matrix4 - 将要应用于AABB的4x4变换矩阵。

返回值

AABB - 应用矩阵变换后的新AABB。

示例

const aabb = new YUKA.AABB();
const matrix = new YUKA.Matrix4().rotationY(Math.PI/2); // 绕y轴旋转90度

const transformedAABB = aabb.applyMatrix4(matrix);
console.log(transformedAABB); // 输出变换后的AABB

实现方式

直接用矩阵乘法来将AABB的八个顶点与矩阵相乘即可。

const applyMatrix4 = function (matrix4) {

    const min = this.min;
    const max = this.max;
    const newMin = new Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
    const newMax = new Vector3(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
    const localPoints = this._localPoints;

    // 将AABB的八个点乘以矩阵
    for (let i = 0, l = localPoints.length; i < l; i++) {
        const point = localPoints[i].clone().applyMatrix4(matrix4);
        newMin.x = Math.min(newMin.x, point.x);
        newMin.y = Math.min(newMin.y, point.y);
        newMin.z = Math.min(newMin.z, point.z);
        newMax.x = Math.max(newMax.x, point.x);
        newMax.y = Math.max(newMax.y, point.y);
        newMax.z = Math.max(newMax.z, point.z);
    }

    // 新建一个计算出的AABB并返回
    const aabb = new AABB(newMin, newMax);
    return aabb;
};

注意事项

  • 变换后的AABB需要进行碰撞检测时,多个物体的AABB都要采用同样的变换矩阵,否则可能会出现检测失败的情况。