Quaternions again

Previously I raved about quaternions.
Now the biggest complaint about quaternions is that you can’t visualise them… which I kind of half agree with and half don’t. Yes Euler angles are easier to visualise, e.g. having 0,0,0 is clearly pointing north, and 0,180,0 is clearly pointing south, but then again quaternions aren’t hideously bad:

[w x y z]
[1 0 0 0] - This points north
[0 0 1 0] - This points south

So w is 1 when the quaternion is ‘identity’, and the other axes are 1 when there is a ‘full’ rotation around that axis (‘full’ in the sense of ‘as far as you can go in that direction before you start coming back on yourself’… so basically 180 degrees). A twist around Z would go to [0 0 0 1].

Another thing which is great about quaternions though is there is no ‘history’ on them. If you use Euler angles you have to have a rotate order, e.g. decide that you are going to rotate around Y, then X, then Z.
What happens then if you are in an orientation and want to rotate in a different order? Well you can’t easily, you’d have to switch to a Matrix or Quaternion, change the rotate order and recalculate the new angles.

An extra thing you can do with quaternions, which you can’t easily do with the other representations, is LERP or SLERP. This basically means to move from one orientation to another along the shortest path. It’s breathtakingly simple to LERP, you simply linearly interpolate and then normalise.

But then again, sometimes Euler angles seem nicer… sometimes it’s nice to have set ‘axes’ that you rotate around, like say a world-space pivot around the Y axis, keeping the horizon nice and level. Aren’t Euler angles better at that kind of thing? Well actually you can do that with quaternions (and matrices) too. Quaternions allow you to rotate the orientation around any axis, so if you want to artificially limit yourself to one particular axis (like, say, positive Y) you can. But if you keep it as a quaternion, you have the option of rotating around other axes too, and using Lerp and Slerp.

Or course there are many advantages to having an orientation in matrix form as well, especially when you have to transform many points. The strategy I’ve found which works brilliantly is to keep all your orientations as quaternions as long as possible, and then right before you need to transform points or get axes, convert to a matrix. This is because the conversion from quaternion to matrix is quite elegant, but the conversion the other way is messier, and has branches.

If you want to display the orientation to the user, say for debugging purposes, Euler angles are useful. It’s pretty simple to go from quaternion to Euler and back.

Now for the confession: there is one ugly thing about quaternions, which is that for every orientation there is not one but TWO quaternions which describe it. Eg (0 0 1 0) and (0 0 -1 0) are the same. This is a bit like say having +180 and -180 being the same thing. It’s not a big deal, but it does mean you have to be careful that when you LERP between two quaternions you pick the right one. For example, if you did a LERP with angles between 20 and 30 degrees, but accidentally picked the -350 version instead of +10 degrees, you’d go a very long way round. It’s the same with quaternions, if you find a rotation is going to take more than 180 degrees, you need to flip one of them. Don’t let that put you off, quaternions are awesome in every other way.

Hopefully I’ve convinced you to use quaternions, but if not, to finish off, another cool trick : to find the angle between two orientations, first multiply the conjugate (w -x -y -z) of orientation A with orientation B. Find the length of the imaginary part (i.e sqrt(x*x + y*y + z*z)) and then asin it and multiply by two. Voilà – there is your angle! Try doing that with matrices!!

[You can also acos the real part, but asin is more robust for small angles... In fact come to think of it, atan2(len(imag), real) would probably be better yet!]

Posted in games, programming | Leave a comment

Starting at THQ

Everything has been crazy hectic at the moment so I haven’t had time to blog, but here’s a quick summary:

  • Bizarre folks are great and I’m gonna miss working with them
  • It’s going to be weird having folks going all over the world on my Facebook!
  • I’m starting at …*drumroll* THQ Digital Studios Warrington on Monday (very excited and nervous but also looking forward to meeting people since they are meant to be absolute code Ninjas)
  • Lots of good press for Lucid
  • Loads of great videos coming out of the woodwork:

Audio team research trip:

Years ago respected developer Stephen Cakebread did a Halo/PGR Mashup:

Drift School:

Eamon does a video which makes everyone cry buckets:


…and while drinking with the increasingly famous Mr Steven Tovey, found that SCEE’s bar do 6 beers for 6 pounds and have free popcorn, which if nothing else really made me feel like a student again!

Posted in Uncategorized | Leave a comment

Why 3D does work

The 140 character limit on Twitter does leave me often without enough space to write what I mean, so here’s a blog post.

I read this blog saying that 3D “doesn’t work”: http://blogs.suntimes.com/ebert/2011/01/post_4.html

The gist is that because our eyes focus individually on objects with lenses, even if the stereo separation (by 3d glasses) tells us an object is nearer, our eyes are not evolved to resolve the conflict. I think there is a point to be made here, which is that even though 3d glasses trick our stereo separation cue, they don’t really focus the image where it should be. But I don’t think this means 3D “doesn’t work” it just gives us a headache (which is definitely true from my experience!). The amount of headache we get depends on a few things I reckon, but it probably due to how much our senses disagree, and the amount of re-focusing our eyes have to do.

There was this post defending it: http://cineform.blogspot.com/2011/01/another-overstatement-that-3d-wont-work.html saying that after 15 feet our eye lenses don’t really care, which I agree with.

Then Naty Hoffman posted this: http://twitter.com/renderwonk/statuses/29815065192435713 that TVs are typically closer than 15 feet, so the original point holds.

Addressing the original point, yes there is a discrepancy between stereo separation and individual focus, but if you think about it, our eyes must be able to compensate for this difference, otherwise people with short-sightedness wouldn’t be able to judge depth without their glasses on. Let me conduct a quick experiment: *takes off glasses* – yes I can still perceive depth, of course!

So ’3D will never work’ is clearly untrue. But it could be uncomfortable to watch – I often find that when I am not wearing my glasses my eyes get strained because they are trying to focus on objects which they are unable to focus on. The question is, how uncomfortable will it be?

My gut reaction (without doing any maths) is that it’s a function of the real distance and the fake 3D distance. So for example an object pretending to be at 30 metres but actually at 40 will be more comfortable than an object at 20 metres but actually at 40. But how does this change with a closer screen? Will an object pretending to be at 3 metres but actually at 4 metres have the same comfort as an object pretending to be at 30 metres but actually at 40? My gut reaction is yes, I think it’s the ratio of the fake to real distances which is crucial: the closer it is to 1.0, the more comfortable the effect. So even though a TV is closer, it’s also smaller, the ‘fake’ distances are closer to the screen, and everything scales down, meaning comfort level is similar.

I’ve heard reports that the 3DS is much more comfortable to watch than a cinema or TV, and this would logically hold water. Even though the 3DS is held nearer (lets say 0.40 metres) the objects onscreen aren’t going to poke out much, say 5 cm max (so 0.35 metres). This ratio would be lower than the cinema or TV so possibly more comfortable.

Posted in Uncategorized | Leave a comment

The God Confusion

I’m currently reading The God Delusion by Richard Dawkins, which is a fantastic book, and I’ve been trying to imagine what would I make of it if I was devoutly religious… I think my first reaction would be “My God, this Dawkins chap is also devoutly religious… against religion!”, despite the fact that he very logically points out that this both isn’t the case and doesn’t make sense anyway.

It taking ages to read because I keep getting distracted by amazing videos on YouTube by Dawkins debating and lecturing, and in one case a great speech once made by Douglas Adams. But watching those videos has made me realise that since Dawkins wrote his first book in the 70s, he’s probably just really tired of having to debate the same thing over and over and so thought “Do you know what, if I just go ahead and write a book saying ‘there probably is no god, and here’s the logical reasoning why’ at least I won’t have to repeat myself all the time!

Posted in Uncategorized | Leave a comment