wiki.sine.space | sinespace

Difference between revisions of "Scripting/SQuaternion"

From wiki.sine.space
Jump to: navigation, search
(24 intermediate revisions by the same user not shown)
Line 62: Line 62:
 
local newQuat = '''Quaternion.New'''(0.0, 0.707, 0.0, 0.707);<br>
 
local newQuat = '''Quaternion.New'''(0.0, 0.707, 0.0, 0.707);<br>
 
''-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]''<br>  
 
''-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]''<br>  
''-- which is [0, 90.0, 0.0] Euler angles''}}
+
''-- which is [0.0, 90.0, 0.0] Euler angles''}}
  
{{ScriptFunction|SQuaternion|__new|(float x, float y, float z)|Creates a quaternion from 3 Euler floats (i.e. 3x 0-360' angles)}}
+
{{ScriptFunction|SQuaternion|__new|(float x, float y, float z)|Creates a quaternion from 3 Euler floats (i.e. 3x 0-360' angles)|5=
{{ScriptFunction|SQuaternion|__new|(SVector angle, float axis)|Creates a quaternion from a Angle / Axis pair}}
+
local newQuat = '''Quaternion.Euler'''(0.0, 90.0, 0.0);
{{ScriptFunction|SQuaternion|__new|(SVector forward)|Creates a quaternion a forward vector; presuming up is (0,1,0)}}
+
''-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]''<br>
{{ScriptFunction|SQuaternion|__new|(SVector forward, SVector up)|Creates a quaternion a forward and up vector pair}}
+
''-- which is [0.0, 90.0, 0.0] Euler angles''}}
 +
 
 +
{{ScriptFunction|SQuaternion|__new|(SVector axis, float angle)|Creates a quaternion from a Angle / Axis pair|5=
 +
local newVector = Vector.New(0.0, 90.0, 0.0);
 +
local newQuat = '''Quaternion.AngleAxis'''(newVector, 90.0);<br>
 +
''-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]''}}
 +
 
 +
{{ScriptFunction|SQuaternion|__new|(SVector forward)|Creates a quaternion a forward vector; presuming up is (0,1,0)|5=
 +
local newQuat = '''Quaternion.LookRotation'''(Vector.Forward);}}
 +
 
 +
{{ScriptFunction|SQuaternion|__new|(SVector forward, SVector up)|Creates a quaternion a forward and up vector pair|5=
 +
local newQuat = '''Quaternion.LookRotation'''(Vector.Forward, Vector.Up);}}
  
 
==Members==
 
==Members==
 
Note: Add/Scale/Divide are also implemented as operators (e.g. A + B, A += B)
 
Note: Add/Scale/Divide are also implemented as operators (e.g. A + B, A += B)
  
{{ScriptFunction|SVector|EulerAngles|{ get; }|Returns the Euler rotation for this Quaternion}}
+
{{ScriptFunction|SVector|EulerAngles|{ get; }|Returns the Euler rotation for this Quaternion|5=
{{ScriptFunction|SVector|OVERLOAD|*(SQuaternion, SVector)|Rotates a vector by a quaternion}}
+
local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
{{ScriptFunction|SQuaternion|OVERLOAD|*(SQuaternion, SQuaternion)|Rotates a quaternion by a quaternion}}
+
local euler = newQuat.'''EulerAngles''';<br>
{{ScriptFunction|float|Angle|(SQuaternion other);|Returns the angle between two quaternions}}
+
Space.Log(euler);<br>
{{ScriptFunction|SQuaternion|Lerp|(SQuaternion other, float t);|Linearly interpolates between this and other quaternion, by factor t and returns the result}}
+
''-- prints [0, 90, 0]''}}
{{ScriptFunction|SQuaternion|Slerp|(SQuaternion other, float t);|Spherically interpolates between this and other quaternion, by factor t and returns the result}}
+
 
{{ScriptFunction|SQuaternion|RotateTowards|(SQuaternion other, float t);|Rotates this towards other, by no more than t degrees}}
+
{{ScriptFunction|SVector|OVERLOAD|*(SQuaternion, SVector)|Rotates a vector by a quaternion|5=
{{ScriptFunction|float|Dot|(SQuaternion other);|Returns the dot product of this and another quaternion}}
+
local newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
{{ScriptFunction|SQuaternion|Inverse|{ get; }|Returns the inverse of this quaternion}}
+
local newVector = Vector.Forward;<br>
 +
rotatedVector =  newQuat '''*''' newVector;<br><br>
 +
 
 +
Space.Log(rotatedVector);<br>
 +
''-- prints [0, 0, -1]''}}
 +
 
 +
{{ScriptFunction|SQuaternion|OVERLOAD|*(SQuaternion, SQuaternion)|Rotates a quaternion by a quaternion|5=
 +
local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
 +
local newVector = Vector.New(0.0, 0.0, 0.0);<br>
 +
rotatedVector = newQuat '''*''' newQuat;<br><br>
 +
 
 +
''-- rotatedVector is a quaternion with value [0.0, 1.0, 0.0, 0.0]''<br>
 +
''-- which is [0.0, 180.0, 0.0] Euler angles''}}
 +
 
 +
{{ScriptFunction|float|Angle|(SQuaternion other);|Returns the angle between two quaternions|5=
 +
local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
 +
local otherQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);<br>
 +
angle =  newQuat.'''Angle'''(otherQuat);<br><br>
 +
 
 +
Space.Log(angle);<br>
 +
''-- prints 90.0173034667969''}}
 +
 
 +
{{ScriptFunction|SQuaternion|Lerp|(SQuaternion other, float t);|Linearly interpolates between this and other quaternion, by factor t and returns the result|5=
 +
''-- Lua Translation of Unity C# Documentation''
 +
''-- Attach this script to a cube or any object.''<br><br>
 +
 
 +
local cube = Space.Host.ExecutingObject;<br>
 +
cube.SubscribeToEvents();<br><br>
 +
 
 +
local fromQuat = cube.LocalRotation; <br>
 +
local toQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);<br>
 +
local speed = 0.1;<br><br>
 +
 
 +
''-- The cube will rotate 90 degrees from current rotation by speed amount.''<br>
 +
local moveCube = function()<br>
 +
&nbsp;&nbsp;cube.LocalRotation = fromQuat.'''Lerp'''(toQuat, Space.Time * speed);<br>
 +
end<br><br>
 +
 
 +
cube.OnUpdate(moveCube);}}
 +
 
 +
{{ScriptFunction|SQuaternion|Slerp|(SQuaternion other, float t);|Spherically interpolates between this and other quaternion, by factor t and returns the result|5=
 +
''-- Lua Translation of Unity C# Documentation''
 +
''-- Attach this script to a cube or any object.''<br><br>
 +
 
 +
local cube = Space.Host.ExecutingObject;<br>
 +
cube.SubscribeToEvents();<br><br>
 +
 
 +
local fromQuat = cube.LocalRotation; <br>
 +
local toQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);<br>
 +
local speed = 0.1;<br><br>
 +
 
 +
''-- The cube will rotate 180 degrees from current rotation by speed amount.''<br>
 +
local moveCube = function()<br>
 +
&nbsp;&nbsp;cube.LocalRotation = fromQuat.'''Slerp'''(toQuat, Space.Time * speed);<br>
 +
end<br><br>
 +
 
 +
cube.OnUpdate(moveCube);}}
 +
 
 +
{{ScriptFunction|SQuaternion|RotateTowards|(SQuaternion other, float t);|Rotates this towards other, by no more than t degrees|5=
 +
''-- Lua Translation of Unity C# Documentation''
 +
''-- Attach this script to a cube or any object.''<br><br>
 +
 
 +
local cube = Space.Host.ExecutingObject;<br>
 +
cube.SubscribeToEvents();<br><br>
 +
 
 +
local fromQuat = cube.LocalRotation; <br>
 +
local target = Quaternion.New(0.0, 0.707, 0.0, 0.707);<br>
 +
local speed = 10.0;<br><br>
 +
 
 +
''-- The cube will rotate 90 degrees from current rotation by step amount.''<br>
 +
local moveCube = function()<br>
 +
&nbsp;&nbsp;local step = speed * Space.Time;<br>
 +
&nbsp;&nbsp;cube.LocalRotation = fromQuat.'''RotateTowards'''(target, step);<br>
 +
end<br><br>
 +
 
 +
cube.OnUpdate(moveCube);}}
 +
 
 +
{{ScriptFunction|float|Dot|(SQuaternion other);|Returns the dot product of this and another quaternion|5=
 +
local quat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
 +
local quatOther = Quaternion.New(0.0, 0.0, 0.0, 1.0);<br>
 +
Space.Log(quat.'''Dot'''(quatOther));<br>
 +
''-- prints 0.707000017166138''}}
 +
 
 +
{{ScriptFunction|SQuaternion|Inverse|{ get; }|Returns the inverse of this quaternion|5=
 +
local quat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
 +
local inverseQuat = quat.'''Inverse''';<br>
 +
''-- inverseQuat is a quaternion with value [0.0, -0.707, 0.0, 0.707]''}}
  
 
==Static Members==
 
==Static Members==
{{ScriptFunction|SQuaternion|Identity|{ get; }|Equivalent of new SQuaternion(0,0,0,1)}}
+
{{ScriptFunction|SQuaternion|Identity|{ get; }|Equivalent of new SQuaternion(0,0,0,1)|5=
 +
local identity = '''Quaternion.Identity''';<br>
 +
 
 +
''-- identity is a quaternion with value [0, 0, 0, 1]''}}
  
 
{{Scripting Navbox}}
 
{{Scripting Navbox}}

Revision as of 01:00, 11 July 2017

The SQuaternion struct contains a simple 4D vector of X, Y, Z and W floats designed to represent rotations with orientation.

Fields

X

float X { get; set; }

X axis

local obj = Space.Host.ExecutingObject;

local originalRot = obj.LocalRotation;
obj.SubscribeToEvents();

local 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

local obj = Space.Host.ExecutingObject;

local originalRot = obj.LocalRotation;
obj.SubscribeToEvents();

local 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

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

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);


Constructors

__new

SQuaternion __new (float x, float y, float z, float w)

Initialises quaternion from four floats

local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);

-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]

-- which is [0.0, 90.0, 0.0] Euler angles


__new

SQuaternion __new (float x, float y, float z)

Creates a quaternion from 3 Euler floats (i.e. 3x 0-360' angles)

local newQuat = Quaternion.Euler(0.0, 90.0, 0.0);

-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]

-- which is [0.0, 90.0, 0.0] Euler angles


__new

SQuaternion __new (SVector axis, float angle)

Creates a quaternion from a Angle / Axis pair

local newVector = Vector.New(0.0, 90.0, 0.0);

local newQuat = Quaternion.AngleAxis(newVector, 90.0);

-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]


__new

SQuaternion __new (SVector forward)

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

local newQuat = Quaternion.LookRotation(Vector.Forward);


__new

SQuaternion __new (SVector forward, SVector up)

Creates a quaternion a forward and up vector pair

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


Members

Note: Add/Scale/Divide are also implemented as operators (e.g. A + B, A += B)

EulerAngles

SVector EulerAngles { get; }

Returns the Euler rotation for this Quaternion

local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);

local euler = newQuat.EulerAngles;
Space.Log(euler);

-- prints [0, 90, 0]


OVERLOAD

SVector OVERLOAD *(SQuaternion, SVector)

Rotates a vector by a quaternion

local newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);

local newVector = Vector.Forward;
rotatedVector = newQuat * newVector;

Space.Log(rotatedVector);

-- prints [0, 0, -1]


OVERLOAD

SQuaternion OVERLOAD *(SQuaternion, SQuaternion)

Rotates a quaternion by a quaternion

local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);

local newVector = Vector.New(0.0, 0.0, 0.0);
rotatedVector = newQuat * newQuat;

-- rotatedVector is a quaternion with value [0.0, 1.0, 0.0, 0.0]

-- which is [0.0, 180.0, 0.0] Euler angles


Angle

float Angle (SQuaternion other);

Returns the angle between two quaternions

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 other, float t);

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

-- Lua Translation of Unity C# Documentation

-- Attach this script to a cube or any object.

local cube = Space.Host.ExecutingObject;
cube.SubscribeToEvents();

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 other, float t);

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

-- Lua Translation of Unity C# Documentation

-- Attach this script to a cube or any object.

local cube = Space.Host.ExecutingObject;
cube.SubscribeToEvents();

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 other, float t);

Rotates this towards other, by no more than t degrees

-- Lua Translation of Unity C# Documentation

-- Attach this script to a cube or any object.

local cube = Space.Host.ExecutingObject;
cube.SubscribeToEvents();

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 other);

Returns the dot product of this and another quaternion

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


Inverse

SQuaternion Inverse { get; }

Returns the inverse of this quaternion

local quat = Quaternion.New(0.0, 0.707, 0.0, 0.707);

local inverseQuat = quat.Inverse;

-- inverseQuat is a quaternion with value [0.0, -0.707, 0.0, 0.707]


Static Members

Identity

SQuaternion Identity { get; }

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

local identity = Quaternion.Identity;
-- identity is a quaternion with value [0, 0, 0, 1]