Quaternions

I’ve just coded a 2, 3 and 4 dimension Vector class, a 2, 3 and 4 dimension Matrix class and a Quaternion class (also a couple of container classes). 2400 lines of code in about a day. I accidentally missed one of my lectures today as I was too busy coding.

Quaternions are cool! Anything in 4 dimensions is cool. A quaternion is a rotation in 3 dimensions that, among other things, prevents ‘Gimbal Lock’. It’s also easy to do smooth rotation through interpolation of quaternions.

Quaternions come from an obsure branch of mathematics from the 19th century. They have pretty much been superseded by vector mathematics, but have their uses in computer graphics.

Here endith the lesson for today.

4 Responses to “Quaternions”


  1. 1 Razor

    2400 lines in one day?! I only just clocked 4k, and I’ve been working away for a while now, maybe 2 months…

    Those classes are certainly important though. I havn’t bothered with a matrix class, but I’ve written vector and quaternion ones. If I can give you any advice, it’s this: Polish your vector class till it positively shines. It’s pretty much the work horse of any 3d game, you’ll be using one every few lines, so make sure it’s fast, easy to use and reliable. And const correct, and it’s name isn’t too long, etc. etc.

    I’m pretty proud of my vector class actualy. Templated both on type and number of dimensions, with specialisations for 2, 3 and 4 dimensional to allow vector.x usage (using some union trickery). I’ve used it for colours too. I even had an (incomplete) sse optimised specialisation, but it’s uses were limited.

  2. 2 Tom

    Unfortunately this is in the land of Java and it’s for uni. This means a couple of things: I’m not using this for games, I can’t really use templates because I a) don’t know how they work in Java (easily remedied) and b) don’t think I should use the lastest features of Java anyway.

    What I’m using it for is rendering (using only plotpixel) a bezier patch. It’s for COMP404 - Advanced Graphics and Multimedia.

    Sounds like you have a cool vector class there. I’ll definitely take your advice when I get around to programming my engine.

    It’s quite a few lines of code, but it’s mostly easy stuff like add, subtract, multiply etc.

  3. 3 Razor

    Ah, I see now. Iirc java doesn’t have operator overloading, that must be a bit of a pain. So you’re writing some sort of software renderer? Sounds difficult, but fun. Does your renferer have to be per-pixel, or will you generate a polygonal representation first, or what?

  4. 4 Tom

    It’s per-pixel, so I will have to write the line drawing algorithms myself. We are allowed to use any datastructures we want so I will probably be generating line segments or something. For extra marks we can do hidden surface removal, I think I might have a crack at z-buffering.

    Yep, no operator overloading, so there is a lot of v.mult(m).add(u), fun!

    It’s been fun-ish, but I wish I was doing this stuff for a game instead of a boring uni assingment. And I’m not really learning that much as I’ve mucked around with OpenGL and vectors before. Oh well.

Leave a Reply

You must login to post a comment.