struct Eagle::Quat

Overview

A unit quaternion: the way Eagle stores 3D rotations.

You rarely touch the four components. Build rotations from something readable, combine them by multiplication, and apply them to vectors:

Node3D#rotation is a Quat, and Node3D has helpers like rotate_y and look_at that cover most needs.

spin = Quat.from_axis_angle(Vec3::UP, Mathf.deg2rad(90))
spin * Vec3::FORWARD # => roughly (-1, 0, 0): forward turned left

from = Quat.look_rotation(v3(0, 0, -1))
to = Quat.look_rotation(v3(1, 0, 0))
halfway = from.slerp(to, 0.5)

node = Node3D.new
node.rotation = halfway

Defined in:

eagle/math/quat.cr

Constant Summary

IDENTITY = Quat.new

No rotation.

Constructors

Instance Method Summary

Constructor Detail

def self.from_axis_angle(axis : Vec3, rad : Number) : Quat #

A rotation of rad radians around axis. The axis doesn't need to be normalized.


def self.from_euler(pitch : Number, yaw : Number, roll : Number) : Quat #

Builds a rotation from Euler angles in radians. Yaw turns around Y, pitch around X and roll around Z, applied as yaw, then pitch, then roll. This matches how an FPS camera turns.


def self.from_euler(v : Vec3) : Quat #

.from_euler with (pitch, yaw, roll) packed into a Vec3.


def self.identity : Quat #

Returns IDENTITY.


def self.look_rotation(forward : Vec3, up : Vec3 = Vec3::UP) : Quat #

A rotation whose forward (-Z) points along forward, keeping up as close to up as possible.


def self.new(x : Number, y : Number, z : Number, w : Number) #

Creates a quaternion from raw components. Prefer the named constructors.


def self.new #

Creates the identity rotation.


Instance Method Detail

def *(o : Quat) : Quat #

Combines rotations: a * b applies b first, then a.


def *(v : Vec3) : Vec3 #

Rotates the vector v.


def /(s : Number) : Quat #

Divides every component.


def ==(o : Quat) : Bool #

Exact equality. Use #approx? for computed values.


def approx?(o : Quat, eps = 1e-4) : Bool #

True when the components are within eps.


def conjugate : Quat #

The conjugate. For unit quaternions this equals the inverse rotation.


def dot(o : Quat) : Float32 #

Dot product. Values near ±1 mean the rotations are almost the same.


def forward : Vec3 #

This rotation's forward direction: where -Z ends up.


def inverse : Quat #

The rotation that undoes this one.


def length : Float32 #

Length. It is 1 for a proper rotation.


def length_squared : Float32 #

Squared length. It is 1 for a proper rotation.


def normalized : Quat #

Rescales to unit length. Call it after many multiplications to stop drift.


def right : Vec3 #

This rotation's right direction: where +X ends up.


def slerp(o : Quat, t : Number) : Quat #

Spherical interpolation, which blends orientations at constant angular speed. Takes the short way round.


def to_euler : Vec3 #

Euler angles (pitch, yaw, roll) in radians, the approximate inverse of .from_euler.


def to_mat4 : Mat4 #

The rotation as a 4x4 matrix.


def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


def up : Vec3 #

This rotation's up direction: where +Y ends up.


def w : Float32 #

Scalar part.


def w=(w : Float32) #

Scalar part.


def x : Float32 #

Vector part x. Prefer the named constructors to setting components.


def x=(x : Float32) #

Vector part x. Prefer the named constructors to setting components.


def y : Float32 #

Vector part y.


def y=(y : Float32) #

Vector part y.


def z : Float32 #

Vector part z.


def z=(z : Float32) #

Vector part z.