GameMonkey Script

GameMonkey Script Forums
It is currently Mon Dec 10, 2018 12:05 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Quaternions
PostPosted: Thu May 08, 2008 2:15 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 707
I was browsing the GameDev.Net forum recently (a fairly infrequent activity for me) and I noticed that 'Quaternions' are still regularly discussed and are being widely used. As a game programmer who has accumulated significant 3D experience, I find this disturbing.

To quickly revise, quaternions are most commonly used in game programming to store and manipulate rotations. They may also be used quite readily and reasonably for physics, particularly when dealing with angular momentum or related values, but that is not the focus of today's opinionated rant ;) Rotations or orientations are just one part of a 3D Transformation. They can be represented by 1) Euler angles, for up to 3 axes and any ordering 2) Axis and Angle, representing an angle of rotation about and axis direction vector 3) 3x3 Matrix or sub matrix 4) Quaternion. There may be other representations or variations of these. Each representation has strength, weaknesses and uses.

Now Quaternions seem to have a following in game programming but I believe they are misused frequently, often adding no value to the code or project at all. I also believe there is an elitist mentality whereby something that is considered complex must be used to show off a programmers l33t ski11z.
These are the typical things I hear about quaternions:
1) They are 4 dimensional values that cannot be visualized and are hard to understand.
2) They are fast.
3) They are very useful in game programming and should be used.

Yes they are 4 dimensional, but in the context we are using the things, they really just represent a scaled version of the quite intuitive Axis and Angle representation, and the math demonstrates this. Yes they are fast, but ONLY when working with quaternions by themselves. Once translations or scaling is introduced, working with the separate components is either tedious or impossible. I worked at a company once and the lead engine programmer at the time said 'we use quaternions for our transforms because they are fast'. I looked at the real code in the engine and a game they were making with it and noted that the quaternions were constantly being converted to and from matrices or other formats, completely negating all computational efficiency.

I say, here are the strengths of quaternions:
1) Can represent a rotation via a unit quaternion
2) Small memory representation (4 floats)
3) Fast multiply to combine rotations operation
4) SLerp operation allows smooth and pleasant interpolation result
5) Quaternion TO matrix conversion is fast

The weaknesses are:
1) Can represent rotations with smaller values via compression, Euler angles or single axes if possible. This might be important for disk storage, network transmission or animation tracks.
2) They ONLY represent the Rotation part of a Transform (in this context), otherwise a 4x4 (or equivalent) matrix is needed.
3) The math to rotate a 3D point / vector is only efficient for 1 operation, otherwise a matrix x vector is faster
4) Almost all other operations with quaternions require conversion, specifically converting FROM matrix is slow.

So, the time to use quaternions is really limited to places where you only want to work with rotations and specifically want to SLerp them. That pretty much limits their use to the animation system.
What to use instead? Well modern games really need Translation, Rotation and Non-uniform Scaling, so a 4x4 matrix (which may be stored as 4x3 matrix) is ideal, and works well with Vector hardware like CPU SSE units or GPU vector registers.
Anyone still using Euler angles for general purpose game use should re-examine their reasoning. Euler angles make sense for a single axes, but that would be the same as Axis Angle representation, except that the axis is limited. Euler angles may be used to decompose a matrix rotation component to transmit via network. The final use is in 3D editor interfaces. Very often artists only want to play with 1 or 2 axes and those axes are usually X,Y,Z directions in some relative space. That's pretty much it, otherwise Euler angles are ambiguous, slow and impossible to visualize past 2 axis due to the induced extra rotation and gimbal lock. Think of Euler angles like 360 degrees in a circle, an antiquated and arbitrary way to represent something, but occasionally useful to interface with humans, particularly those trained in the old school. There is almost always a better alternative.

I might add to this rant as I think about it a bit more, but the conclusion I want to present is... 1) Use tools and techniques when and where they add value. 2) Carefully consider how to represent transformations in you game for common use and specialized use 3) Don't follow the herd on game dev forums when a significant percentage of people have no idea what they are talking about. I don't write this to boast of my knowledge, but to share my personal opinion based on my practical experience.


Top
 Profile  
Reply with quote  
 Post subject: Re: Quaternions
PostPosted: Sat May 10, 2008 6:55 am 
Offline

Joined: Fri Nov 24, 2006 9:50 am
Posts: 165
For expressing game logic (not internal engine math) a spherical coodinate system is quite good.. ie. an axis plus two "angles". Matrices are no fun if you are trying to express very subtle game controls or AI logic. (and quaternions are really bad for that, they should be regulated to animation, camera interpolation work and physics)


Top
 Profile  
Reply with quote  
 Post subject: Re: Quaternions
PostPosted: Sat May 10, 2008 11:04 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 707
Yes, I think what you are describing are 'steering' or 'orbit' angles. Eg. For a car there may just be 1, for a an aeroplane 3, and for general objects and cameras, often 2 axes. Those represent rotations about the logical Forward, Right and Up directions. Usually they are applied with extra code to keep restrictions such as Up staying Up in a First Person Shooter game, without introducing Roll. I forgot to mention 3 axis as a method of representing a rotation, however it is identical to the 3x3 matrix, with those basis vectors stored in either columns or rows.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group