Ashasekayi (Talk | contribs) |
Ashasekayi (Talk | contribs) |
||
Line 102: | Line 102: | ||
''-- prints 90.0173034667969''}} | ''-- prints 90.0173034667969''}} | ||
− | {{ScriptFunction|SQuaternion|Lerp|(SQuaternion other, float t);|Linearly interpolates between this and other quaternion, by factor t and returns the result}} | + | {{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 toQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);<br> | ||
+ | local speed = 0.05;<br><br> | ||
+ | |||
+ | ''-- The cube will rotate 90 degrees from current rotation by speed amount.'' | ||
+ | local moveCube = function()<br> | ||
+ | local fromQuat = cube.LocalRotation; <br> | ||
+ | cube.LocalRotation = fromQuat.'''Lerp'''(toQuat, 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}} | {{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|SQuaternion|RotateTowards|(SQuaternion other, float t);|Rotates this towards other, by no more than t degrees}} |
The SQuaternion struct contains a simple 4D vector of X, Y, Z and W floats designed to represent rotations with orientation.
X axis
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
Y axis
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
Z axis
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
W axis
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
Initialises quaternion from four floats
-- creates a new quaternion with value [0.0, 0.707, 0.0, 0.707]
Creates a quaternion from 3 Euler floats (i.e. 3x 0-360' angles)
Creates a quaternion from a Angle / Axis pair
Creates a quaternion a forward vector; presuming up is (0,1,0)
Creates a quaternion a forward and up vector pair
Note: Add/Scale/Divide are also implemented as operators (e.g. A + B, A += B)
Returns the Euler rotation for this Quaternion
local euler = newQuat.EulerAngles;
Space.Log(euler);
Rotates a vector by a quaternion
local newVector = Vector.Forward;
rotatedVector = newQuat * newVector;
Space.Log(rotatedVector);
Rotates a quaternion by a quaternion
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]
Returns the angle between two quaternions
local otherQuat = Quaternion.New(0.0, 1.0, 0.0, 0.0);
angle = newQuat.Angle(otherQuat);
Space.Log(angle);
Linearly interpolates between this and other quaternion, by factor t and returns the result
-- Attach this script to a cube or any object.
local cube = Space.Host.ExecutingObject;
cube.SubscribeToEvents();
local toQuat = Quaternion.New(0.0, 0.707, 0.0, 0.707);
local speed = 0.05;
-- The cube will rotate 90 degrees from current rotation by speed amount.
local moveCube = function()
local fromQuat = cube.LocalRotation;
cube.LocalRotation = fromQuat.Lerp(toQuat, speed);
end
Spherically interpolates between this and other quaternion, by factor t and returns the result
Rotates this towards other, by no more than t degrees
Returns the dot product of this and another quaternion
Returns the inverse of this quaternion
Equivalent of new SQuaternion(0,0,0,1)
|