LyQuaternion - Ly Framework - Gamely

Adds Quaternions to Crayta.

To access it from every script using Ly.Quaternion LyFramework with properly setupped LyRequire is needed.
After installing you need to add it to LyFramework subfolder lib see LyRequire Documentation.
Quaternions are represented as array of 4 elements in xyzw order.

Constructors:
FromXYZAngle(x,y,z,angleInDegrees) separated x,y,z component axis.
FromAxisAngle(Vector , AngleInDegrees)
FromRotation(Rotation)
FromEuler(pitch,yaw,roll)

WARNING in functions with [qOut = new] (Copy, NLerp, SLerp …) not providing qOut results in a new construction/allocation so remember to always specify it when maximum performances are required and you do not intend to construct a new object(I often use Copy with no out as Constructor as it is a fast way to initialize many quaternions to the same initial values).

Methods:
Copy(q, [qOut = new]) providing qOut it copies q to qOut with no new allocations.

SetAxisAngle(q, Vector, AngleInDegrees)
update a quaternion with a new axis and angle in degrees

SetXYZAngle(q, x, y, z, AngleInDegrees)
update a quaternion with a new axis as separated x y z components and angle in degrees

SetRotation(q, Rotation)
update a quaternion from a rotation

SetEuler(q, pitch, yaw, roll)
update a quaternion with separated angles in degrees

Normalize(q, [qOut = q])
Normalize quaternion to qOut or in place if qOut is not provided

Inverse(q , [qOut = q])
Invert quaternion to qOut or in place if qOut is it works like conjugate + normalize

Conjugate = function(q, [qOut = q])
same as inverse but doesn’t provide normalization

Multiply(a,b , [qOut = a] )
Multiplies and updates a if no out is provided (Warning you cannot use b as out)

XformVector(q,Vector, [VerctorOut])
Write the trasformed Vector to VectorOut or update in place vector if VectorOut is not provided.
Hasn’t been tested yet i’ll test it soon and fix possible y, z swaped.

SLerp (a, b, t, [qOut = new])
Spherical interpolation between 2 quaternions update qOut if provided or creates a new quaternion

NLerp (a, b, t, [qOut = new])
Faster than SLerp and guaranteed to generate the same path.Doesn’t guarantee constant velocity.
Can be used in 99% of the cases with no issues.
Normalized Interpolation between 2 quaternions update qOut if provided or creates a new quaternion

ToRotation(q, [RotationOut])
Convert Quaterion to Rotation class , updates RotationOut or create a new Rotation if not provided.

Distance(a, b)
Returns the minimum angle between a and b quaternions 0 to pi

Every functions byt XformVector and ToRotation returns the quaternion to chain them together.

local Q = Ly.Quaternion
Q.SLerp( self.qStart, self.qEnd, t , self.qRes):Normalize():
local rot = Q.FromAxisAngle( Fw, 45):ToRotation()

Future plans:

  • (DONE with video tutorial) a demo that shows how to achieve no gimbal lock for 6 degrees of freedom

Tutorial: