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

normalize

normalize() 方法将四元数单位化(即其长度变为1)。此方法可以确保一个四元数保持在单位球面(或者说单位四元数)中。

语法

Quaternion.normalize();

返回值

当前四元数。

示例

var q = new Quaternion(0, 1, 0, 1);
q.normalize();
console.log(q); // Quaternion { x: 0, y: 0.7071067811865475, z: 0, w: 0.7071067811865475 }

实现原理

四元数的 normalize 方法实际上就是对四元数的各个分量进行归一化操作,使得其模长为1,从而将其转化为单位四元数。

设四元数 q = xi + yj + zk + w,则:

四元数的模长 |q| = √(x^2 + y^2 + z^2 + w^2)。

若将四元数除以其模长,则有 q/|q| = (xi + yj + zk + w) / √(x^2 + y^2 + z^2 + w^2),若此时将其各分量均除以 |q|,则可得归一化之后的四元数。

即有:q' = xi' + yj' + zk' + w',其中:

  • xi' = x / |q|
  • yj' = y / |q|
  • zk' = z / |q|
  • w' = w / |q|

注意事项

  • 此方法将修改当前四元数的值,不会返回任何新的四元数对象。
  • 注意在对一个已经为单位四元数的四元数使用 normalize() 方法时,该操作将不会对其进行任何改变。