2
0

intro.bbdoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. Quaternions are a mathematical concept that can be used to represent and manipulate 3D rotations in a
  2. way that avoids some of the issues encountered with other methods, such as Euler angles and rotation
  3. matrices. A quaternion is a four-dimensional complex number that can be written as `q = w + xi + yj + zk`,
  4. where `w`, `x`, `y`, and `z` are real numbers, and `i`, `j`, and `k` are imaginary units with the property
  5. `i^2 = j^2 = k^2 = ijk = -1`. In the context of 3D rotations, w represents the scalar (real) part,
  6. while `x`, `y`, and `z` together form the vector (imaginary) part.
  7. Quaternions have several advantages over other rotation representations:
  8. 1. **Compactness** : Quaternions only require four numbers to represent a rotation, while rotation matrices
  9. require nine numbers.
  10. 2. **Numerical stability** : Quaternions can be easily normalized, which helps maintain numerical stability
  11. during calculations.
  12. 3. **Interpolation** : Quaternions can be smoothly interpolated using a method called Spherical Linear
  13. Interpolation (SLERP), which provides a more intuitive and accurate interpolation between rotations
  14. than other methods.
  15. 4. **Avoiding Gimbal Lock** : Quaternions do not suffer from gimbal lock, a problem that occurs with
  16. Euler angles when two of the three axes of rotation become aligned, resulting in a loss of one degree
  17. of freedom. Gimbal lock is named after the mechanical gimbals used in early navigation systems,
  18. where the alignment of two gimbals would cause the system to lose its ability to rotate freely in
  19. all three dimensions. In the context of 3D rotations, gimbal lock can cause unexpected and undesirable
  20. behavior, such as sudden jumps in rotation or the inability to perform certain rotations. Quaternions,
  21. on the other hand, maintain their full range of motion without encountering gimbal lock, providing a
  22. more robust and reliable method for representing and manipulating 3D rotations.
  23. 5. **Efficiency** : Quaternion operations, such as multiplication and inversion, are generally faster
  24. than their matrix counterparts.
  25. Despite these advantages, quaternions can be less intuitive to work with than Euler angles or rotation
  26. matrices, and some operations, like extracting a specific axis of rotation, can be more complicated.
  27. However, the benefits of using quaternions often outweigh these drawbacks, making them a popular choice
  28. for many 3D applications, including computer graphics and game development.
  29. In the following sections, we'll explore how to use the `SQuatD`, `SQuatF`, and `SQuatI` structs provided
  30. by the `math.quaternion` module in BlitzMax to work with quaternions, and how they can be applied to various
  31. rotation tasks.
  32. ### Euler Angles
  33. Euler angles are a set of three angles that describe the orientation of an object in
  34. three-dimensional space. They represent a sequence of three rotations applied around specific
  35. axes in a specified order. The angles are named after the Swiss mathematician Leonhard Euler,
  36. who extensively studied the topic.
  37. The three angles are often referred to as pitch, yaw, and roll:
  38. 1. **Pitch (θ)** : The rotation around the X-axis. It represents the up and down tilting motion,like nodding your head.
  39. 2. **Yaw (ψ)** : The rotation around the Y-axis. It represents the side-to-side turning motion, like shaking your head.
  40. 3. **Roll (ϕ)** : The rotation around the Z-axis. It represents the side-to-side tilting motion, like tilting your head.
  41. Euler angles can be applied in different orders, such as XYZ, ZYX, or YXZ. The order in which the angles
  42. are applied matters because rotation is not commutative. For example, rotating around the X-axis and
  43. then the Y-axis will produce a different result than rotating around the Y-axis and then the X-axis.
  44. While Euler angles are intuitive and easy to understand, they can be problematic in certain situations,
  45. such as when they lead to gimbal lock. Quaternions are often used as an alternative to Euler angles
  46. because they avoid gimbal lock and provide smoother interpolation between rotations.
  47. ## Basic Operations with Quaternions
  48. ### Creating Quaternions
  49. To create a quaternion using the SQuatD struct, you can use the constructor:
  50. ```blitzmax
  51. Local quat:SQuatD = New SQuatD(x, y, z, w)
  52. ```
  53. The `x`, `y`, `z`, and `w` parameters represent the components of the quaternion. You can also create a
  54. quaternion from Euler angles or a rotation matrix using the provided functions:
  55. ```blitzmax
  56. Local euler:SVec3D = New SVec3D(pitch, yaw, roll)
  57. Local quatFromEuler:SQuatD = SQuatD.CreateFromEuler(euler)
  58. Local quatFromMatrix:SQuatD = SQuatD.CreateFromRotation(mat)
  59. ```
  60. ### Multiplying Quaternions
  61. To combine two rotations represented by quaternions, you can multiply them together:
  62. ```blitzmax
  63. Local combinedQuat:SQuatD = quat1 * quat2
  64. ```
  65. ### Inverting Quaternions
  66. Inverting a quaternion refers to finding the quaternion that represents the opposite (or inverse) rotation.
  67. When you apply the inverse rotation to an object that has already been rotated by the original
  68. quaternion, the object returns to its initial orientation.
  69. A quaternion `q` is represented as `(x, y, z, w)`. Its inverse, denoted as `q_inv`,
  70. is calculated by taking the conjugate of `q` and then normalizing the result. The conjugate of a
  71. quaternion is obtained by negating the vector part (`x`, `y`, `z`) while keeping the scalar part
  72. (`w`) the same. The normalization is necessary to ensure that the inverse quaternion has a
  73. magnitude of 1, just like the original quaternion.
  74. Mathematically, the inverse quaternion `q_inv` is computed as follows:
  75. ```
  76. q_inv = (x, y, z, w)^* / ||(x, y, z, w)||^2
  77. ```
  78. where `^*` denotes the conjugate and `|| ||` denotes the magnitude (or norm) of the quaternion.
  79. In practice, you can use the `Invert()` method to find the inverse of a quaternion. Once you
  80. have the inverse quaternion, you can apply it to an object to undo the rotation applied by
  81. the original quaternion. This can be useful in situations where you need to reverse or cancel
  82. out a previous rotation.
  83. ```blitzmax
  84. Local invertedQuat:SQuatD = quat.Invert()
  85. ```
  86. ### Interpolating Between Quaternions
  87. To smoothly interpolate between two rotations, you can use either linear interpolation
  88. (Interpolate) or spherical linear interpolation (SphericalInterpolate). The main difference
  89. between these two methods lies in how the interpolation is performed:
  90. 1. **Linear Interpolation (Interpolate)** : Linear interpolation is the simplest method, where
  91. each component of the quaternion is linearly interpolated between the start and end values
  92. based on the interpolation factor. This method is faster to compute but can result in
  93. non-uniform motion and changes in speed during the interpolation. Linear interpolation does
  94. not guarantee that the resulting interpolated quaternions have a unit length, so it's necessary
  95. to normalize the result afterwards.
  96. 2. **Spherical Linear Interpolation (SphericalInterpolate)** : Spherical linear interpolation,
  97. also known as *slerp*, takes into account the spherical geometry of quaternions. This method
  98. interpolates along the shortest path on the quaternion's hypersphere, resulting in uniform
  99. motion and constant speed during the interpolation. Slerp is more computationally expensive
  100. than linear interpolation, but it provides smoother and more natural transitions between rotations.
  101. Since slerp preserves the unit length of quaternions, there's no need to normalize the result.
  102. In summary, linear interpolation is faster but can produce non-uniform motion and requires
  103. normalization, while spherical linear interpolation provides smoother, more natural transitions
  104. at the cost of increased computational complexity.
  105. ```blitzmax
  106. Local interpolatedQuat:SQuatD = quat1.Interpolate(quat2, t)
  107. Local slerpedQuat:SQuatD = quat1.SphericalInterpolate(quat2, t)
  108. ```
  109. ## Quaternion Applications
  110. Quaternions can be used to perform various operations on 3D rotations, such as converting between
  111. different rotation representations, rotating points or vectors, and generating rotation matrices.
  112. ### Converting Between Representations
  113. To convert a quaternion to Euler angles:
  114. ```blitzmax
  115. Local eulerAngles:SVec3D = quat.ToEuler()
  116. ```
  117. To convert a quaternion to a rotation matrix:
  118. ```blitzmax
  119. Local mat3:SMat3D = SQuatD.ToMat3(quat)
  120. Local mat4:SMat4D = SQuatD.ToMat4(quat)
  121. ```
  122. ### Rotating Points or Vectors
  123. To rotate a point or vector using a quaternion, you can first convert the quaternion to a
  124. rotation matrix and then apply the matrix to the point or vector. This approach is advantageous
  125. for several reasons:
  126. 1. **Compatibility** : Converting a quaternion to a rotation matrix ensures compatibility with other
  127. systems or libraries that expect or require the use of matrices for transformations. This way, you
  128. can seamlessly integrate quaternion-based rotations with existing matrix-based systems.
  129. 2. **Efficiency** : When you need to rotate multiple points or vectors, converting the quaternion to
  130. a rotation matrix first and then applying the matrix to each point or vector can be more efficient.
  131. This is because the conversion from quaternion to matrix is done once, and the resulting matrix can
  132. be reused for all the subsequent rotations.
  133. 3. **Clarity** : For some users, working with matrices might be more intuitive or familiar than working
  134. with quaternions. By converting the quaternion to a rotation matrix, you can leverage the familiarity
  135. and ease of understanding associated with matrix-based transformations.
  136. ```blitzmax
  137. Local rotationMatrix:SMat4D = SQuatD.ToMat4(quat)
  138. Local rotatedPoint:SVec3D = rotationMatrix * point
  139. ```
  140. ### Generating Rotation Matrices
  141. Quaternions enable the generation of rotation matrices, which are applicable in various transformations..
  142. Here are some examples:
  143. #### Applying Quaternion to a Matrix
  144. ```blitzmax
  145. Local mat3:SMat3D = SQuatD.ToMat3(quat)
  146. Local mat4:SMat4D = SQuatD.ToMat4(quat)
  147. ```
  148. #### Creating Translation and Rotation Matrix
  149. ```blitzmax
  150. Local translation:SVec3D = New SVec3D(x, y, z)
  151. Local rotTransMatrix:SMat4D = SQuatD.RotTrans(quat, translation)
  152. ```
  153. #### Creating Translation, Rotation, and Scaling Matrix
  154. ```blitzmax
  155. Local translation:SVec3D = New SVec3D(x, y, z)
  156. Local scaling:SVec3D = New SVec3D(scaleX, scaleY, scaleZ)
  157. Local origin:SVec3D = New SVec3D(originX, originY, originZ)
  158. Local rotTransOriginMatrix:SMat4D = SQuatD.RotTransOrigin(quat, scaling, origin)
  159. ```