wiki.sine.space | sinespace

Difference between revisions of "Scripting/SQuaternion"

From wiki.sine.space
Jump to: navigation, search
(Replaced content with "This page has moved to: https://docs.sine.space/v/scripting/client-scripting/types/squaternion")
 
(7 intermediate revisions by one other user not shown)
Line 1: Line 1:
The SQuaternion struct contains a simple 4D vector of X, Y, Z and W floats designed to represent rotations with orientation.
+
This page has moved to: https://docs.sine.space/v/scripting/client-scripting/types/squaternion
 
+
==Fields==
+
{{ScriptFunction|float|X|{ get; set; }|X axis|5=
+
local obj = Space.Host.ExecutingObject;<br>
+
local originalRot = obj.LocalRotation;<br>
+
obj.SubscribeToEvents();<br><br>
+
local onStartMethod = function()<br>
+
&nbsp;&nbsp;Space.Log(originalRot.'''x''');<br>
+
&nbsp;&nbsp;''-- prints the X component of this object as a float''<br><br>
+
&nbsp;&nbsp;originalRot.'''x''' = 0.25;<br>
+
&nbsp;&nbsp;''-- assigns 0.25 value to the X component''<br><br>
+
&nbsp;&nbsp;obj.LocalRotation = originalRot;<br>
+
&nbsp;&nbsp;''-- sets the the new rotation''<br>
+
end<br><br>
+
obj.OnStart(onStartMethod);}}
+
 
+
{{ScriptFunction|float|Y|{ get; set; }|Y axis|5=
+
local obj = Space.Host.ExecutingObject;<br>
+
local originalRot = obj.LocalRotation;<br>
+
obj.SubscribeToEvents();<br><br>
+
local onStartMethod = function()<br>
+
&nbsp;&nbsp;Space.Log(originalRot.'''y''');<br>
+
&nbsp;&nbsp;''-- prints the Y component of this object as a float''<br><br>
+
&nbsp;&nbsp;originalRot.'''y''' = 0.25;<br>
+
&nbsp;&nbsp;''-- assigns 0.25 value to the Y component''<br><br>
+
&nbsp;&nbsp;obj.LocalRotation = originalRot;<br>
+
&nbsp;&nbsp;''-- sets the the new rotation''<br>
+
end<br><br>
+
obj.OnStart(onStartMethod);}}
+
 
+
{{ScriptFunction|float|Z|{ get; set; }|Z axis|5=
+
local obj = Space.Host.ExecutingObject;<br>
+
local originalRot = obj.LocalRotation;<br>
+
obj.SubscribeToEvents();<br><br>
+
local onStartMethod = function()<br>
+
&nbsp;&nbsp;Space.Log(originalRot.'''z''');<br>
+
&nbsp;&nbsp;''-- prints the Z component of this object as a float''<br><br>
+
&nbsp;&nbsp;originalRot.'''z''' = 0.25;<br>
+
&nbsp;&nbsp;''-- assigns 0.25 value to the Z component''<br><br>
+
&nbsp;&nbsp;obj.LocalRotation = originalRot;<br>
+
&nbsp;&nbsp;''-- sets the the new rotation''<br>
+
end<br><br>
+
obj.OnStart(onStartMethod);}}
+
 
+
{{ScriptFunction|float|W|{ get; set; }|W axis|5=
+
local obj = Space.Host.ExecutingObject;<br>
+
local originalRot = obj.LocalRotation;<br>
+
obj.SubscribeToEvents();<br><br>
+
local onStartMethod = function()<br>
+
&nbsp;&nbsp;Space.Log(originalRot.'''w''');<br>
+
&nbsp;&nbsp;''-- prints the W component of this object as a float''<br><br>
+
&nbsp;&nbsp;originalRot.'''w''' = 0.25;<br>
+
&nbsp;&nbsp;''-- assigns 0.25 value to the W component''<br><br>
+
&nbsp;&nbsp;obj.LocalRotation = originalRot;<br>
+
&nbsp;&nbsp;''-- sets the the new rotation''<br>
+
end<br><br>
+
obj.OnStart(onStartMethod);}}
+
 
+
==Constructors==
+
{{ScriptFunction|SQuaternion|__new|(float x, float y, float z, float w)|Initialises quaternion from four floats|5=
+
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>
+
''-- 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|(SVector angle, float axis)|Creates a quaternion from a Angle / Axis pair}}
+
{{ScriptFunction|SQuaternion|__new|(SVector forward)|Creates a quaternion a forward vector; presuming up is (0,1,0)}}
+
{{ScriptFunction|SQuaternion|__new|(SVector forward, SVector up)|Creates a quaternion a forward and up vector pair}}
+
 
+
==Members==
+
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|5=
+
local newQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
+
local euler = newQuat.'''EulerAngles''';<br>
+
Space.Log(euler);<br>
+
''-- prints [0, 90, 0]''}}
+
 
+
{{ScriptFunction|SVector|OVERLOAD|*(SQuaternion, SVector)|Rotates a vector by a quaternion|5=
+
local newQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
+
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==
+
{{ScriptFunction|SQuaternion|Identity|{ get; }|Equivalent of new SQuaternion(0,0,0,1)}}
+
 
+
{{Scripting Navbox}}
+

Latest revision as of 08:04, 19 September 2022

This page has moved to: https://docs.sine.space/v/scripting/client-scripting/types/squaternion