四元数是实数{x,y,z,w}的四元组。四元数在数学上是欧拉角表示的一种方便的替代方法。可以在不经历万向节锁定的情况下对四元数进行插值。也可以使用四元数将一系列旋转使用到单个四元素中。
local valid, quat = Quaternion.LookAt(ZDir, YDir)
valid, quat = Quaternion.LookAt(ZDir, 0 - YDir)
valid, quat = Quaternion.LookAt(XDir, YDir)
local roll,yaw, pitch
local quat = Quaternion.FromEuler(pitch,yaw,roll)
local newq = Quaternion.New(0,0,2,1)
print("newq:"..tostring(newq))
Vector3 relativePos = target.position - transform.position;
local up = Vector3.New(0,1,0)
local b, rotation = Quaternion.LookAt(relativePos, up);
print("使用指定的向前方向和向上方向转换成四元数,转换是否成功:"..tostring(b))
Quaternion qfe = Quaternion.FromEuler(0, 30, 0);
local up = Vector3.New(0,1,0)
local rotation = Quaternion.FromAxisAngle(30, up);
local fromTf = SandboxNode.New('Transform')
fromTf:SetParent(workspace)
fromTf.Position = Vector3.New(100, 100, 100)
local toTf = SandboxNode.New('Transform')
toTf:SetParent(workspace)
toTf.Position = Vector3.New(50, 50, 50)
local speed = 0.01;
local timeCount = 0.0f;
local deltaTime = 1.0f
timeCount = timeCount + deltaTime;
local lerpRotation1 = Quaternion.Lerp(fromTf.rotation, toTf.rotation, timeCount * speed);
local newq = Quaternion.New(0,0,2,1)
local lerpRotation2 = newq:Lerp(fromTf.rotation, toTf.rotation, timeCount * speed);
local newq = Quaternion.New(0,0,2,1)
local dir = Vector3.New(0,0,1)
local up = Vector3.New(0,1,0)
local angle = 0.01;
local rotation = newq:RotateAxisAngle(dir, up, angle);
local newq = Quaternion.New(0,0,1,1)
local targetq = Quaternion.New(0,0,2,1)
local dir = newq:LookDir(targetq);
local workspace = game.WorkSpace
local targetTf = SandboxNode.New('Transform')
targetTf:SetParent(workspace)
targetTf.Position = Vector3.New(100, 100, 100)
local transform = SandboxNode.New('Transform')
transform:SetParent(workspace)
transform.Position = Vector3.New(50, 50, 50)
local newq = Quaternion.New(0,0,0,1)
local dir = newq:RotateToDir(transform.Rotation, targetTf.Rotation);