Breakroom is now Sinewave Campus. Book a meeting

SQuaternion

客户端类型
在此类中ToString
  1. 函数
  2. ToString
  3. Angle
  4. Lerp
  5. Slerp
  6. RotateTowards
  7. Dot
  8. Equals
  9. New
  10. Euler
  11. AngleAxis
  12. LookRotation
  13. operator*
  14. FromToRotation
  15. 属性
  16. X
  17. Y
  18. Z
  19. W
  20. EulerAngles
  21. Inverse
  22. Identity
成员 20
函数ToStringConverts a quaternion to a human readable string
函数AngleReturns the angle between two quaternions
函数LerpLinearly interpolates between this and other quaternion, by factor t and returns the result
函数SlerpSpherically interpolates between this and other quaternion, by factor t and returns the result
函数RotateTowardsRotates this towards other, by no more than t degrees
函数DotReturns the dot product of this and another quaternion
函数Equals–
函数NewCreates a new Quaternion
函数EulerCreates a quaternion using Euler angles.
函数AngleAxisCreates a quaternion from an Angle/Axis pair
函数LookRotationCreates a quaternion a forward vector; presuming up is (0,1,0)
函数operator*The result of using the * operator.
函数FromToRotationCreates a rotation which rotates from a to b.
属性XX axis
属性YY axis
属性ZZ axis
属性WW axis
属性EulerAnglesReturns the Euler rotation for this Quaternion
属性InverseReturns the inverse of this quaternion
属性IdentityEquivalent of new SQuaternion(0,0,0,1)

函数​

ToString​

string ToString()

Converts a quaternion to a human readable string

返回值

string

示例

Lua
aQuaternion = Quaternion.New(0.0, 1.0, 0.0, 0.0)
theString = aQuaternion.ToString()

Angle​

float Angle(SQuaternion b)

Returns the angle between two quaternions

参数

名称类型默认值
bSQuaternion–

返回值

float

示例

Lua
local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
local otherQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
angle = newQuat.Angle(otherQuat);

Space.Log(angle);
-- prints 90.0173034667969

Lerp​

SQuaternion Lerp(SQuaternion b, float t)

Linearly interpolates between this and other quaternion, by factor t and returns the result

参数

名称类型默认值
bSQuaternion–
tfloat–

返回值

SQuaternion

示例

Lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
QuatLerpAB = QuatA.Lerp(QuatB, Space.Time * 0.1)
Lua
local cube = Space.Host.ExecutingObject

local fromQuat = cube.LocalRotation
local toQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
local speed = 0.1

-- The cube will rotate 90 degrees from current rotation by speed amount.
local moveCube = function()
cube.LocalRotation = fromQuat.Lerp(toQuat, Space.Time * speed);
end

cube.OnUpdate(moveCube)

Slerp​

SQuaternion Slerp(SQuaternion b, float t)

Spherically interpolates between this and other quaternion, by factor t and returns the result

参数

名称类型默认值
bSQuaternion–
tfloat–

返回值

SQuaternion

示例

Lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
QuatLerpAB = QuatA.Slerp(QuatB, Space.Time * 0.1)
Lua
local cube = Space.Host.ExecutingObject;

local fromQuat = cube.LocalRotation;
local toQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
local speed = 0.1;

-- The cube will rotate 180 degrees from current rotation by speed amount.
local moveCube = function()
cube.LocalRotation = fromQuat.Slerp(toQuat, Space.Time * speed)
end

cube.OnUpdate(moveCube)

RotateTowards​

SQuaternion RotateTowards(SQuaternion b, float delta)

Rotates this towards other, by no more than t degrees

参数

名称类型默认值
bSQuaternion–
deltafloat–

返回值

SQuaternion

示例

Lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
ARotatedTowardB = QuatA.RotateTowards(QuatB, Space.Time * 0.1)
Lua
local cube = Space.Host.ExecutingObject

local fromQuat = cube.LocalRotation
local target = Quaternion.New(0.0, 0.707, 0.0, 0.707)
local speed = 10.0

-- The cube will rotate 90 degrees from current rotation by step amount.
local moveCube = function()
local step = speed * Space.Time
cube.LocalRotation = fromQuat.RotateTowards(target, step)
end

cube.OnUpdate(moveCube)

Dot​

float Dot(SQuaternion b)

Returns the dot product of this and another quaternion

参数

名称类型默认值
bSQuaternion–

返回值

float

示例

Lua
local quat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
local quatOther = Quaternion.New(0.0, 0.0, 0.0, 1.0);
Space.Log(quat.Dot(quatOther));
-- prints 0.707000017166138

Equals​

bool Equals(SQuaternion other)

参数

参数类型说明
otherSQuaternionThe other Quaternion that we are comparing with.

返回值

bool

示例

Lua
QuatA = Quaternion.New(0.0, 0.707, 0.0, 0.707)
QuatB = Quaternion.New(0.0, 1.0, 0.0, 0.0)
isEqual = QuatA.Equals(QuatB)

New​

static SQuaternion New(float x, float y, float z, float w)

Creates a new Quaternion

参数

名称类型默认值
xfloat–
yfloat–
zfloat–
wfloat–

返回值

SQuaternion

示例

Lua
newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)

Euler​

static SQuaternion Euler(float x, float y, float z)

Creates a quaternion using Euler angles.

参数

名称类型默认值
xfloat–
yfloat–
zfloat–

返回值

SQuaternion

示例

Lua
newQuat = Quaternion.Euler(0.0, 90.0, 0.0)

AngleAxis​

static SQuaternion AngleAxis(SVector axis, float angle)

Creates a quaternion from an Angle/Axis pair

参数

名称类型默认值
axisSVector–
anglefloat–

返回值

SQuaternion

示例

Lua
newVector = Vector.New(0.0, 90.0, 0.0)
newQuat = Quaternion.AngleAxis(newVector, 90.0)

LookRotation​

static SQuaternion LookRotation(SVector forward)
static SQuaternion LookRotation(SVector forward, SVector up)

Creates a quaternion a forward vector; presuming up is (0,1,0)

参数

名称类型默认值
forwardSVector–
upSVector–

返回值

SQuaternion

示例

Lua
local newQuat = Quaternion.LookRotation(Vector.Forward);
-- or
local newQuat = Quaternion.LookRotation(Vector.Forward, Vector.Up);

operator*​

static SVector operator*(SQuaternion a, SVector b)
static SQuaternion operator*(SQuaternion a, SQuaternion b)

The result of using the * operator.

参数

名称类型默认值
aSQuaternion–
bSVector–
bSQuaternion–

返回值

SVector SQuaternion

示例

Lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newVector = Vector.Forward
rotatedVector = newQuat * newVector

--or

newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
newVector = Vector.New(0.0, 0.0, 0.0)
rotatedVector = newQuat * newQuat

FromToRotation​

static SQuaternion FromToRotation(SVector a, SVector b)

Creates a rotation which rotates from a to b.

Usually you use this to rotate a transform so that one of its axes eg. the y-axis - follows a target direction b in world space.

参数

名称类型默认值
aSVector–
bSVector–

返回值

SQuaternion

示例

Lua
VectorA = Vector.New(0.0,1.0,0.0)
VectorB = Vector.New(1.0,0.0,0.0)
QuaternionFromToRotation = Quaternion.FromToRotation(VectorA, VectorB)

属性​

X​

float X {get; set; }

X axis

示例

Lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.X = 40.0
Lua
obj = Space.Host.ExecutingObject
originalRot = obj.LocalRotation

onStartMethod = function()
Space.Log(originalRot.x)
-- prints the X component of this object as a float

originalRot.x = 0.25
-- assigns 0.25 value to the X component

obj.LocalRotation = originalRot
-- sets the the new rotation
end

obj.OnStart(onStartMethod)

Y​

float Y {get; set; }

Y axis

示例

Lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.Y = 40.0
Lua
obj = Space.Host.ExecutingObject
originalRot = obj.LocalRotation

onStartMethod = function()
Space.Log(originalRot.y);
-- prints the Y component of this object as a float

originalRot.y = 0.25;
-- assigns 0.25 value to the Y component

obj.LocalRotation = originalRot;
-- sets the the new rotation
end

obj.OnStart(onStartMethod);

Z​

float Z {get; set; }

Z axis

示例

Lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.Z = 40.0
Lua
local obj = Space.Host.ExecutingObject;
local originalRot = obj.LocalRotation;
obj.SubscribeToEvents();

local onStartMethod = function()
Space.Log(originalRot.z);
-- prints the Z component of this object as a float

originalRot.z = 0.25;
-- assigns 0.25 value to the Z component

obj.LocalRotation = originalRot;
-- sets the the new rotation
end

obj.OnStart(onStartMethod);

W​

float W {get; set; }

W axis

示例

Lua
newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0)
newQuat.W = 40.0
Lua
local obj = Space.Host.ExecutingObject;
local originalRot = obj.LocalRotation;
obj.SubscribeToEvents();

local onStartMethod = function()
Space.Log(originalRot.w);
-- prints the W component of this object as a float

originalRot.w = 0.25;
-- assigns 0.25 value to the W component

obj.LocalRotation = originalRot;
-- sets the the new rotation
end

obj.OnStart(onStartMethod);

EulerAngles只读​

SVector EulerAngles {get; }

Returns the Euler rotation for this Quaternion

示例

Lua
newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
euler = newQuat.EulerAngles

Inverse只读​

SQuaternion Inverse {get; }

Returns the inverse of this quaternion

示例

Lua
quat = Quaternion.New(0.0, 0.707, 0.0, 0.707)
inverseQuat = quat.Inverse

Identity只读​

static SQuaternion Identity {get; }

Equivalent of new SQuaternion(0,0,0,1)

示例

Lua
identity = Quaternion.Identity