struct Quat

Quaternions are efficient and robust mathematical objects for representing rotations! Understanding the details of how a quaternion works is not generally necessary for using them effectively, so don’t worry too much if they seem weird to you. They’re weird to me too.

If you’re interested in learning the details though, 3Blue1Brown and Ben Eater have an excellent interactive lesson about them!

Instance Fields and Properties

   
Quat Inverse The reverse rotation! If this quat goes from A to B, the inverse will go from B to A.
Quat Normalized A normalized quaternion has the same orientation, and a length of 1.
Quaternion q The internal, wrapped System.Numerics type. This can be nice to have around so you can pass its fields as ‘ref’, which you can’t do with properties. You won’t often need this, as implicit conversions to System.Numerics types are also provided.
Vec4 Vec4 Sometimes you want to do weird stuff with your Quaternions. I won’t judge. This just turns the Quat into a Vec4, makes some types of math easier!
float w W component.
float x X component. Sometimes known as I.
float y Y component. Sometimes known as J.
float z Z component. Sometimes known as K.

Instance Methods

   
Quat You may want to use static creation methods, like Quat.LookAt, or Quat.Identity instead of this one! Unless you know what you’re doing.
Invert Makes this Quat the reverse rotation! If this quat goes from A to B, the inverse will go from B to A.
Normalize A normalized quaternion has the same orientation, and a length of 1.
Relative Rotates a quaternion making it relative to another rotation while preserving it’s “Length”!
Rotate This rotates a point around the origin by the Quat.
ToString Mostly for debug purposes, this is a decent way to log or inspect the quaternion in debug mode. Looks like “[x, y, z, w]”

Static Fields and Properties

   
Quat Identity This is the ‘multiply by one!’ of the quaternion rotation world. It’s basically a default, no rotation quaternion.

Static Methods

   
Delta Creates a quaternion that goes from one rotation to another.
Difference This gives a relative rotation between the first and second quaternion rotations. Remember that order is important here!
FromAngles Creates a Roll/Pitch/Yaw rotation (applied in that order) from the provided angles in degrees!
LookAt Creates a rotation that describes looking from a point, to another point! This is a great function for camera style rotation, or other facing behavior when you know where an object is, and where you want it to look at. This rotation works best when applied to objects that face Vec3.Forward in their resting/model space pose.
LookDir Creates a rotation that describes looking towards a direction. This is great for quickly describing facing behavior! This rotation works best when applied to objects that face Vec3.Forward in their resting/model space pose.
Slerp Spherical Linear intERPolation. Interpolates between two quaternions! Both Quats should be normalized/unit quaternions, or you may get unexpected results.

Operators

   
Implicit Conversions Allows implicit conversion from System.Numerics.Quaternion to StereoKit.Quat.
* This is the combination of rotations a and b. Note that order matters here.
- Gets a Quat representing the rotation from a to b.

Examples

Quat.LookAt and LookDir are probably one of the easiest ways to work with quaternions in StereoKit! They’re handy functions to have a good understanding of. Here’s an example of how you might use them.

// Draw a box that always rotates to face the user
Vec3 boxPos = new Vec3(1,0,1);
Quat boxRot = Quat.LookAt(boxPos, Input.Head.position);
Mesh.Cube.Draw(Material.Default, Matrix.TR(boxPos, boxRot));

// Make a Window that faces a user that enters the app looking
// Forward.
Pose winPose = new Pose(0,0,-0.5f, Quat.LookDir(0,0,1));
UI.WindowBegin("Posed Window", ref winPose);
UI.WindowEnd();




Found an issue with these docs, or have some additional questions? Create an Issue on Github!