BlitzMax

BlitzMax

  • Downloads
  • Docs
  • API
  • Resources
  • About

›Quaternion

BlitzMax API

  • Introduction

BRL

    Audio

    • Introduction
    • TChannel
    • TSound

    AudioSample

    • Introduction
    • TAudioSample
    • TAudioSampleLoader

    Bank

    • Introduction
    • TBank

    BankStream

    • Introduction
    • TBankStream

    Base64

    • Introduction
    • TBase64

    Blitz

    • Introduction
    • TArrayBoundsException
    • TBlitzException
    • TNullFunctionException
    • TNullMethodException
    • TNullObjectException
    • TOutOfDataException
    • TRuntimeException
  • BRL.BMPLoader
  • Clipboard

    • Introduction
    • TClipboard
    • TClipboardOpts
    • TWin32ClipboardOpts
    • TX11ClipboardOpts
  • BRL.D3D7Max2D
  • BRL.D3D9Max2D
  • BRL.DirectSoundAudio
  • BRL.EndianStream
  • Event

    • Introduction
    • TEvent
  • BRL.EventQueue
  • BRL.FileSystem
  • BRL.FreeAudioAudio
  • BRL.GLGraphics
  • BRL.GLMax2D
  • BRL.GNet
  • BRL.Graphics
  • BRL.Hook
  • IO

    • Introduction
    • MaxIO
  • BRL.JPGLoader
  • BRL.KeyCodes
  • LinkedList

    • Introduction
    • TList
    • TListEnum
    • TLink

    Map

    • Introduction
    • TMap
    • TIntMap
    • TIntKey
    • TPtrMap
    • TPtrKey
    • TStringMap
  • BRL.Math
  • Matrix

    • Introduction
    • SMat2D
    • SMat2F
    • SMat2I
    • SMat3D
    • SMat3F
    • SMat3I
    • SMat4D
    • SMat4F
    • SMat4I

    Max2D

    • Introduction
    • TImage

    MaxLua

    • Introduction
    • TLuaClass
    • TLuaObject
  • BRL.OGGLoader
  • BRL.OpenALAudio
  • ObjectList

    • Introduction
    • TObjectList
    • TObjectListEnumerator
    • TObjectListReverseEnumerator

    Pixmap

    • Introduction
    • TPixmap
    • TPixmapLoader
  • BRL.PNGLoader
  • BRL.PolledInput
  • Quaternion

    • Introduction
    • SQuatD
    • SQuatF
    • SQuatI
  • BRL.RamStream
  • Random

    • Introduction
    • TRandom

    Reflection

    • Introduction
    • TTypeId
    • TMember
    • TConstant
    • TField
    • TGlobal
    • TFunction
    • TMethod
  • BRL.Retro
  • BRL.Socket
  • BRL.SocketStream
  • BRL.StandardIO
  • Stream

    • Introduction
    • TCStream
    • TIO
    • TStream
    • TStreamException
    • TStreamFactory
    • TStreamReadException
    • TStreamWrapper
    • TStreamWriteException

    StringBuilder

    • Introduction
    • TStringBuilder
    • TSplitBuffer
  • BRL.System
  • BRL.SystemDefault
  • BRL.TextStream
  • BRL.TGALoader
  • ThreadPool

    • Introduction
    • TRunnable
    • TThreadPoolExecutor

    Threads

    • Introduction
    • TThread
    • TThreadData
    • TMutex
    • TSemaphore
    • TCondVar

    Timer

    • Introduction
    • TChrono
  • BRL.TimerDefault
  • Vector

    • Introduction
    • SVec2D
    • SVec2F
    • SVec2I
    • SVec3D
    • SVec3F
    • SVec3I
    • SVec4D
    • SVec4F
    • SVec4I

    Volumes

    • Introduction
    • TVolume
  • BRL.WAVLoader

Text

    CSV

    • Introduction
    • TCsvParser
    • TCsvHeader
    • TCsvRow
    • TCsvOptions
    • SCsvColumn

    Format

    • Introduction
    • TFormatter

    Ini

    • Introduction
    • TIni
    • TIniProperty
    • TIniSection

    JConv

    • Introduction
    • TJConv
    • TJConvBuilder
    • TJConvSerializer

    Json

    • Introduction
    • TJSON
    • TJSONObject
    • TJSONString
    • TJSONInteger
    • TJSONReal
    • TJSONBool
    • TJSONArray
    • TJSONNull
    • TJSONError
    • TJSONNumber

    Regex

    • Introduction
    • TRegEx
    • TRegExMatch
    • TRegExOptions
    • TRegExException

    TOML

    • Introduction
    • ETomlNodeType
    • TToml
    • ITomlNode
    • STomlDate
    • STomlDateTime
    • STomlTime
    • TTomlArray
    • TTomlInteger
    • TTomlString
    • TTomlTable
    • TTomlBoolean

    Xml

    • Introduction
    • TxmlDoc
    • TxmlNode
    • TxmlAttribute

MaxGUI

  • MaxGUI.MaxGUI
  • MaxGUI.Drivers
  • MaxGUI.Localization
  • MaxGUI.ProxyGadgets
  • MaxGUI.CocoaMaxGUI
  • MaxGUI.Win32MaxGUIEx

PUB

  • Pub.FreeJoy
  • Pub.FreeProcess
  • Pub.Glad
  • Pub.Glew
  • Joystick

    • Introduction
    • TJoystickDriver
  • Pub.Lua
  • Pub.OpenAL
  • Pub.OpenGLES
  • Pub.xmmintrin
  • Pub.ZLib

Crypto

    Digest

    • Introduction
    • TMessageDigest
    • TDigestRegister
    • TNoSuchAlgorithmException

    Cipher

    • Introduction
    • TCipher
    • TCipherMode
    • Crypto.AESCipher
    • Crypto.AnubisCipher
    • Crypto.BlowfishCipher
    • Crypto.CamelliaCipher
    • Crypto.Cast5Cipher
    • Crypto.DESCipher
    • Crypto.IdeaCipher
    • Crypto.KasumiCipher
    • Crypto.KhazadCipher
    • Crypto.KSeedCipher
    • Crypto.NoekeonCipher
    • Crypto.SerpentCipher
    • Crypto.SkipjackCipher
    • Crypto.TwofishCipher
    • Crypto.XTeaCipher

    CBCMode

    • Introduction
    • TCBCCipherMode

    CFBMode

    • Introduction
    • TCFBCipherMode

    CTRMode

    • Introduction
    • TCTRCipherMode

    ECBMode

    • Introduction
    • TECBCipherMode

    F8Mode

    • Introduction
    • TF8CipherMode

    LRWMode

    • Introduction
    • TLRWCipherMode

    OFBMode

    • Introduction
    • TOFBCipherMode

    XTSMode

    • Introduction
    • TXTSCipherMode

    Blake2BDigest

    • Introduction
    • TBlake2B_512
    • TBlake2B_384
    • TBlake2B_256
    • TBlake2B_160

    CRC32

    • Introduction
    • TCRC32

    MD5Digest

    • Introduction
    • TMD5

    Ripemd320Digest

    • Introduction
    • TRipemd320

    Ripemd356Digest

    • Introduction
    • TRipemd256

    Ripemd160Digest

    • Introduction
    • TRipemd160

    Ripemd128Digest

    • Introduction
    • TRipemd128

    SHA1Digest

    • Introduction
    • TSHA1

    SHA256Digest

    • Introduction
    • TSHA256

    SHA512Digest

    • Introduction
    • TSHA512

    SHA3Digest

    • Introduction
    • TSHA3_512
    • TSHA3_384
    • TSHA3_256
    • TSHA3_224

    TigerDigest

    • Introduction
    • TTiger

    WhirlpoolDigest

    • Introduction
    • TWhirlpool

    Crypto

    • Introduction
    • TCryptoRandom
    • TCryptoSecretBox
    • TCryptoHash
    • TCryptoSign
    • TCryptoKeyExchange
    • TCryptoKey
    • TCryptoHashKey
    • TCryptoSecretBoxKey
    • TCryptoSignature
    • TCryptoExchangeKeyPair
    • TCryptoSessionKeyPair
    • TCryptoNPacket
    • TCryptoKK1Packet
    • TCryptoKK2Packet
    • TCryptoXX1Packet
    • TCryptoXX2Packet
    • TCryptoXX3Packet
    • TCryptoExchangeState
    • TCryptoPWHashMasterKey
    • TCryptoPWHashStoredKey
    • TCryptoPasswordHash

SDL

    SDL

    • Introduction
    • TSDLStream

    SDLSystem

    • Introduction
    • TSDLMultiGesture

    SDLVideo

    • Introduction
    • TSDLDisplay
    • TSDLDisplayMode
    • TSDLGLContext
    • TSDLWindow

    SDLRender

    • Introduction
    • TSDLRenderer
    • TSDLTexture

    SDLTimer

    • Introduction
    • TSDLTimer

    SDLHaptic

    • Introduction
    • TSDLHaptic
    • TSDLHapticCondition
    • TSDLHapticConstant
    • TSDLHapticCustom
    • TSDLHapticDirection
    • TSDLHapticEffect
    • TSDLHapticLeftRight
    • TSDLHapticPeriodic
    • TSDLHapticRamp

    SDLSensor

    • Introduction
    • TSDLSensor
  • SDL.SDLFreeAudio
  • SDL.GLSDLMax2D
  • SDL.GL2SDLMax2D
  • SDL.D3D9SDLMax2D
  • SDLVirtualJoystick

    • Introduction
    • TVirtualJoystick

Steam

    SteamSDK

    • Introduction
    • TSteamClient
    • TSteamUtils
    • TSteamUserStats
    • TSteamFriends
    • TSteamUGC
    • ISteamFriendsListener
    • ISteamUGCListener
    • ISteamUserStatsListener
    • ISteamUtilsListener
    • EItemPreviewType
    • EPersonaChange
    • EPersonaState
    • ERemoteStoragePublishedFileVisibility
    • EUGCMatchingUGCType
    • EUGCQuery
    • EUserRestriction
    • EUserUGCList
    • EWorkshopFileType
Edit

BRL.Quaternion

Quaternions are a mathematical concept that can be used to represent and manipulate 3D rotations in a way that avoids some of the issues encountered with other methods, such as Euler angles and rotation matrices. A quaternion is a four-dimensional complex number that can be written as q = w + xi + yj + zk, where w, x, y, and z are real numbers, and i, j, and k are imaginary units with the property i^2 = j^2 = k^2 = ijk = -1. In the context of 3D rotations, w represents the scalar (real) part, while x, y, and z together form the vector (imaginary) part.

Quaternions have several advantages over other rotation representations:

  1. Compactness : Quaternions only require four numbers to represent a rotation, while rotation matrices require nine numbers.

  2. Numerical stability : Quaternions can be easily normalized, which helps maintain numerical stability during calculations.

  3. Interpolation : Quaternions can be smoothly interpolated using a method called Spherical Linear Interpolation (SLERP), which provides a more intuitive and accurate interpolation between rotations than other methods.

  4. Avoiding Gimbal Lock : Quaternions do not suffer from gimbal lock, a problem that occurs with Euler angles when two of the three axes of rotation become aligned, resulting in a loss of one degree of freedom. Gimbal lock is named after the mechanical gimbals used in early navigation systems, where the alignment of two gimbals would cause the system to lose its ability to rotate freely in all three dimensions. In the context of 3D rotations, gimbal lock can cause unexpected and undesirable behavior, such as sudden jumps in rotation or the inability to perform certain rotations. Quaternions, on the other hand, maintain their full range of motion without encountering gimbal lock, providing a more robust and reliable method for representing and manipulating 3D rotations.

  5. Efficiency : Quaternion operations, such as multiplication and inversion, are generally faster than their matrix counterparts.

Despite these advantages, quaternions can be less intuitive to work with than Euler angles or rotation matrices, and some operations, like extracting a specific axis of rotation, can be more complicated. However, the benefits of using quaternions often outweigh these drawbacks, making them a popular choice for many 3D applications, including computer graphics and game development.

In the following sections, we'll explore how to use the SQuatD, SQuatF, and SQuatI structs provided by the brl.quaternion module in BlitzMax to work with quaternions, and how they can be applied to various rotation tasks.

Euler Angles

Euler angles are a set of three angles that describe the orientation of an object in three-dimensional space. They represent a sequence of three rotations applied around specific axes in a specified order. The angles are named after the Swiss mathematician Leonhard Euler, who extensively studied the topic.

The three angles are often referred to as pitch, yaw, and roll:

  1. Pitch (θ) : The rotation around the X-axis. It represents the up and down tilting motion,like nodding your head.
  2. Yaw (ψ) : The rotation around the Y-axis. It represents the side-to-side turning motion, like shaking your head.
  3. Roll (ϕ) : The rotation around the Z-axis. It represents the side-to-side tilting motion, like tilting your head.

Euler angles can be applied in different orders, such as XYZ, ZYX, or YXZ. The order in which the angles are applied matters because rotation is not commutative. For example, rotating around the X-axis and then the Y-axis will produce a different result than rotating around the Y-axis and then the X-axis.

While Euler angles are intuitive and easy to understand, they can be problematic in certain situations, such as when they lead to gimbal lock. Quaternions are often used as an alternative to Euler angles because they avoid gimbal lock and provide smoother interpolation between rotations.

Basic Operations with Quaternions

Creating Quaternions

To create a quaternion using the SQuatD class, you can use the constructor:

Local quat:SQuatD = New SQuatD(x, y, z, w)

The x, y, z, and w parameters represent the components of the quaternion. You can also create a quaternion from Euler angles or a rotation matrix using the provided functions:

Local euler:SVec3D = New SVec3D(pitch, yaw, roll)
Local quatFromEuler:SQuatD = SQuatD.CreateFromEuler(euler)
Local quatFromMatrix:SQuatD = SQuatD.CreateFromRotation(mat)

Multiplying Quaternions

To combine two rotations represented by quaternions, you can multiply them together:

Local combinedQuat:SQuatD = quat1 * quat2

Inverting Quaternions

Inverting a quaternion refers to finding the quaternion that represents the opposite (or inverse) rotation. When you apply the inverse rotation to an object that has already been rotated by the original quaternion, the object returns to its initial orientation.

A quaternion q is represented as (x, y, z, w). Its inverse, denoted as q_inv, is calculated by taking the conjugate of q and then normalizing the result. The conjugate of a quaternion is obtained by negating the vector part (x, y, z) while keeping the scalar part (w) the same. The normalization is necessary to ensure that the inverse quaternion has a magnitude of 1, just like the original quaternion.

Mathematically, the inverse quaternion q_inv is computed as follows:

q_inv = (x, y, z, w)^* / ||(x, y, z, w)||^2

where ^* denotes the conjugate and || || denotes the magnitude (or norm) of the quaternion.

In practice, you can use the Invert() method to find the inverse of a quaternion. Once you have the inverse quaternion, you can apply it to an object to undo the rotation applied by the original quaternion. This can be useful in situations where you need to reverse or cancel out a previous rotation.

Local invertedQuat:SQuatD = quat.Invert()

Interpolating Between Quaternions

To smoothly interpolate between two rotations, you can use either linear interpolation (Interpolate) or spherical linear interpolation (SphericalInterpolate). The main difference between these two methods lies in how the interpolation is performed:

  1. Linear Interpolation (Interpolate) : Linear interpolation is the simplest method, where each component of the quaternion is linearly interpolated between the start and end values based on the interpolation factor. This method is faster to compute but can result in non-uniform motion and changes in speed during the interpolation. Linear interpolation does not guarantee that the resulting interpolated quaternions have a unit length, so it's necessary to normalize the result afterwards.

  2. Spherical Linear Interpolation (SphericalInterpolate) : Spherical linear interpolation, also known as slerp, takes into account the spherical geometry of quaternions. This method interpolates along the shortest path on the quaternion's hypersphere, resulting in uniform motion and constant speed during the interpolation. Slerp is more computationally expensive than linear interpolation, but it provides smoother and more natural transitions between rotations. Since slerp preserves the unit length of quaternions, there's no need to normalize the result.

In summary, linear interpolation is faster but can produce non-uniform motion and requires normalization, while spherical linear interpolation provides smoother, more natural transitions at the cost of increased computational complexity.

Local interpolatedQuat:SQuatD = quat1.Interpolate(quat2, t)
Local slerpedQuat:SQuatD = quat1.SphericalInterpolate(quat2, t)

Quaternion Applications

Quaternions can be used to perform various operations on 3D rotations, such as converting between different rotation representations, rotating points or vectors, and generating rotation matrices.

Converting Between Representations

To convert a quaternion to Euler angles:

Local eulerAngles:SVec3D = quat.ToEuler()

To convert a quaternion to a rotation matrix:

Local mat3:SMat3D = SQuatD.ToMat3(quat)
Local mat4:SMat4D = SQuatD.ToMat4(quat)

Rotating Points or Vectors

To rotate a point or vector using a quaternion, you can first convert the quaternion to a rotation matrix and then apply the matrix to the point or vector. This approach is advantageous for several reasons:

  1. Compatibility : Converting a quaternion to a rotation matrix ensures compatibility with other systems or libraries that expect or require the use of matrices for transformations. This way, you can seamlessly integrate quaternion-based rotations with existing matrix-based systems.

  2. Efficiency : When you need to rotate multiple points or vectors, converting the quaternion to a rotation matrix first and then applying the matrix to each point or vector can be more efficient. This is because the conversion from quaternion to matrix is done once, and the resulting matrix can be reused for all the subsequent rotations.

  3. Clarity : For some users, working with matrices might be more intuitive or familiar than working with quaternions. By converting the quaternion to a rotation matrix, you can leverage the familiarity and ease of understanding associated with matrix-based transformations.

Local rotationMatrix:SMat4D = SQuatD.ToMat4(quat)
Local rotatedPoint:SVec3D = rotationMatrix * point

Generating Rotation Matrices

Quaternions can be used to generate rotation matrices that can be used in various transformations. Here are some examples:

Applying Quaternion to a Matrix

Local mat3:SMat3D = SQuatD.ToMat3(quat)
Local mat4:SMat4D = SQuatD.ToMat4(quat)

Creating Translation and Rotation Matrix

Local translation:SVec3D = New SVec3D(x, y, z)
Local rotTransMatrix:SMat4D = SQuatD.RotTrans(quat, translation)

Creating Translation, Rotation, and Scaling Matrix

Local translation:SVec3D = New SVec3D(x, y, z)
Local scaling:SVec3D = New SVec3D(scaleX, scaleY, scaleZ)
Local origin:SVec3D = New SVec3D(originX, originY, originZ)
Local rotTransOriginMatrix:SMat4D = SQuatD.RotTransOrigin(quat, scaling, origin)

Structs

StructDescription
SQuatDA Quaternion.
SQuatFA Float backed Quaternion.
SQuatIAn Int backed Quaternion.

Enums

EnumDescription
ERotationOrderThe order in which to apply rotations.
← BRL.PolledInputSQuatD →
  • Basic Operations with Quaternions
    • Creating Quaternions
    • Multiplying Quaternions
    • Inverting Quaternions
    • Interpolating Between Quaternions
  • Quaternion Applications
    • Converting Between Representations
    • Rotating Points or Vectors
    • Generating Rotation Matrices
  • Structs
  • Enums
BlitzMax
Docs
Getting StartedDownloadsAbout
Community
ResourcesSyntaxBomb Forums
More
GitHubStarChat on Discord
Copyright © 2023 Bruce A Henderson