140 likes | 319 Views
Maths and Technologies for Games Quaternions. CO3303 Week 1. Representing Rotations. We have used two methods to store a rotation :. A matrix: Can just use 3x3 matrix for rotation Used these in DirectX. Three Euler/Fixed angles: X, Y & Z, (or yaw, pitch & roll)
E N D
Maths and Technologies for GamesQuaternions CO3303 Week 1
Representing Rotations • We have used two methods to store a rotation: • A matrix: • Can just use 3x3 matrix for rotation • Used these in DirectX • ThreeEuler/Fixed angles: • X, Y & Z, (or yaw, pitch & roll) • Can be combined in any order • Used in TL-Engine • [See Van Verth for more detail]
Converting between Formats • We can create a matrix from fixed angles • Create a rotation matrix for each angle: • E.g. Z rotation matrix of α: • Multiply the matrices together: M = RX*RY*RZ • Different results for different multiplication orders • Can also convert matrix back to fixed angles • See Van Verth or the matrix class in the labs
Euler Angles – Pros/Cons • Concise storage – just three floats • Euler/Fixed angles seem intuitive • But which order to multiply when forming a matrix? • Different orders in same app can cause problems • Are the rotations world or local rotations? • Depends on order again • Gimbal lock can occur when one angle is 90° • The other angles interfere with each other, result is ambiguous • E.g. causing strange spinning effects when looking straight up or down (you will have seen this problem in some games)
Matrices – Pros/Cons • Unambiguous definition of rotation • As compared to Euler angles • 4x4 matrix can also hold position and scale • And totally different things: projection matrix • Used by graphics hardware • Must convert all forms to matrices eventually • But uses 16 floats • Much processing/storage even for simple operations • Can be stored in two ways, by row or by column • APIs (DirectX) and maths texts use different forms
Axis-Angle Rotations • Look for other ways to specify rotations… • Can specify any rotation as a rotation axis r (a vector) and an angle θ (a scalar): • Can convert to and from matrices (see Van Verth or matrix class) • Useful way to think about rotation • But operations are generally complex with this form • Including interpolation (see later)
Quaternions • Quaternions can be used to represent rotation • A general quaternion takes the form: • We will use these forms interchangeably • We will mostly consider normalised quaternions: • However, several quaternion formulae result in un-normalised quaternions • Often need to renormalise quaternions before reuse = examinable
Quaternions • A quaternion is a form of axis-angle rotation • When in a normalised form • If we rewrite a quaternion in this form: • Then r and θ form an axis-angle rotation as described before: • r is a unit vector here
Quaternions – Pros/Cons • Why use this more complex variation on angle-axis form? • Because quaternions can easily be: • Combined together • Used to transform points/vectors • Quaternions can be interpolated easily • A feature vital for animation, which is more difficult with matrices • Quaternions only use 4 floats for storage • However, they lack hardware support • So we need to convert them to and from matrices
Quaternion Formulae 1 • A quaternion can be converted to a matrix: • A little expensive, can be simplified in code • Transposed from Van Verth book for row-based system (DirectX / math text difference) • Reverse conversion is also possible (see Van Verth) • Quaternions don’t convert easily to Euler angles
Quaternion Formulae 2 • Quaternions can be added and scaled: • Scaling is used for normalisation • Two quaternions can be multiplied to create a single quaternion performing both rotations: • Same effect as multiplying matrices, order important • Coded efficiently this is faster than matrix multiplication • N.B. Swapped multiplication order from text for row-based system
Quaternion Formulae 3 • Inverse of a quaternion (the rotation in the opposite direction) is simple: • Assumes quaternion is normalised • Much faster than matrix equivalent, which can be complex • We can also represent vectors as quaternions • Just set w = 0:
Quaternion Formulae 4 • Rotate a vertex or vector p by a quaternion q=(w,v): • Slower than matrix equivalent • 1st formula reversed for row-based system (2nd remains same) • N.B. The formula in earlier Van Verth edition is incorrect • Derivation of most formulae in Van Verth • As well as further detail
Quaternions: Initial Summary • Quaternions can perform similar operations to matrices • With comparable performance • But need to convert to / from matrices • And can’t store position / scaling • No compelling reason to use them yet • But next week we will see how quaternions compare to matrices for interpolation • And how this is used this for animation