Honeycomb Engine: Dev Blog 3 ~ Quaternions for Rotations

Quaternions. The mere mention of these mathematical constructs would’ve sent shivers down my spine a couple of years ago. They’re really weird and complex. However, when you’re making a game engine and you have to model the rotations of objects, you’d be crazy not to use them for several reasons.

The easiest way for people to think about an arbitrary rotation in 3D space is to think of three separate rotations around the x-, y- and z-axes respectively. This way of representing a rotation is commonly known as Euler angles. The advantage is that it’s easy for someone to reason about a rotation represented in Euler angles because they’re so simple – I just rotate around x, then y, then z, it’s that easy. However, this brings a couple of problems with it too; namely, it’s inefficient to do three separate rotations for every single rotation you apply, and Euler angles can run into a problem called Gimbal lock.

In a system where three gimbals each provide an axis of movement around a central point, it is possible to lose one degree of freedom by rotating one of the gimbals 90° such that its axis converges on one of the other gimbals. When this happens, the other two gimbals will rotate along the same axis. The word ‘lock’ is a tad misleading, because you can fix the problem simply by rotating the first gimbal back, but it still leads to weird behaviour sometimes. It’s best explained through an animated diagram.

Quaternions fix both of these issues. Firstly, Euler’s rotation theorem (Yes, Euler got around a lot) tells us that we can represent any rotation or sequence of rotations around a fixed point as one single composite rotation around some axis through that point. As it turns out, we can model rotations around an arbitrary axis as a quaternion!

img_20161109_003123

Rotating around an axis rather than three rotations around x, y and z axes.

To understand how this works, we first need to know what a quaternion is. I’m not going to go into too much detail, because I want to keep this post relevant to how we can use them for rotations. At their most basic level, a quaternion, q, is made up of four real numbers, x, y, z and w, where:

q = w + xi + yj + zk

Here, i, j and k represent versors, or unit quaternions. Don’t worry about what they mean too much, because your head might go nuclear trying to wrap your brain around them. What we want to do is represent x, y, z and w in a way that’s useful to us. Luckily, it’s really simple: take our axis to be a three-dimensional vector (a, b, c) and our angle to be θ. Then our quaternion’s real number parts are:

x = a * sin(θ/2)
y = b * sin(θ/2)
z = c * sin(θ/2)
w = cos(θ/2)

Now we have a quaternion that represents an angle-axis rotation, immune from the problem of gimbal lock. To apply the rotation to an object, we multiply the point we want to rotate by the rotation quaternion. To figure out what the quaternion is doing, we can take x, y, z and w and calculate the angle and axis from them – first of all we can find the angle:

θ = 2 * cos⁻¹(w)

Then, we can use this angle to figure out what the rotational axis – a 3D vector – is:

a = x / sin(θ/2)
b = y / sin(θ/2)
c = z / sin(θ/2)

With this in mind, it’s a little easier to reason about what quaternions are doing. Keep in mind that all these rotations are about the origin, so if you want to rotate around an arbitrary point, you’ll need to translate your object by the inverse position vector of the point, do the rotation, then translate by the position vector of the point to undo the first translation.

rotation-by-translation

This image is about 2D translations, but the general idea still applies.

To make a rotation by compositing two different rotations, we can just multiply their respective quaternion rotations together, paying attention to the order:

new_rotation = second_rotation * first_rotation

The other great thing about quaternions is that we can mix between two rotations really easily, with more believable results that using Euler angles. Whereas we’d use a regular lerp operation (linear interpolation) to move from one rotation vector to another if we were using Euler angles, we can use the more sophisticated slerp (spherical linear interpolation) technique for quaternion-based rotations.

To imagine the difference between these two techniques, imagine you were stood on a completely flat world, whether it be in 2D or 3D, and I asked you to walk from point A to point B on that surface in a straight line at a constant speed – that’s you using a lerp. Now imagine doing the same, but on the surface of a perfectly spherical meteorite instead – of course, walking in a straight line would end up with you inside the meteorite, which is impossible, but if you walk across the surface directly to your destination, that’s what a slerp is doing. Super simple stuff!

on-top-of-the-world

The flag looks like the USB logo, but it’s supposed to be an angle-axis diagram.

That rounds off pretty much all I know about quaternions and how much I could be bothered to look up online. If I got anything wrong, please do tell me because it’s definitely possible. Remember, if quaternions start to hurt your head, which happened plenty of times to me, keep in mind the most important thing: Don’t Worry About It™. You don’t need to know them inside out to be able to use them.

Advertisements

New Hub and Help Text – Project Spikes 01/04

Today’s update has a graphical update for the Hub World. As I’m planning on having the first iteration of a story in the game pretty soon, I’ve re-re-re-done the Hub as a spaceship, which will play a small part in the eventual storyline. In the new Hub World, you’ll find everything is a bit more compact, with a room for each world, containing level selection cubes and secret cubes for each level. The small room you start off in (the cockpit of the spaceship) has a few new decorative items, such as lights, flashing blue accenting for the tops of walls and improved models for TVs and laptops, which were present in older versions of the Hub as decoration. There are also new models for chairs which are a lot like the old ones, but look nicer.

spikes-77

I’ve added a couple new textures, for example the window texture you can see above. There’s also a decorative texture for a solar panel, which are also visible on the spaceship, and for the spaceship I used a particle pack by Unity I found on their Asset Store, a helpful resource for anything from small particle effects to completed projects. The pack I used can be found here. I used the fire particles from this for the boosters on the wings of the spaceship. On the subject of particles, I’ve also improved the activation particle effect for when you walk into a powerup. I will add these into the game properly soon, in later levels. It’s hard to get a screenshot of a fast-moving object, but I had a (poor) try:

spikes-78

Another helpful addition to the game is text fields, where useful information will pop up on-screen for a few seconds at points where you might need it. You can turn this help off from the options menu if you wish, but in the future this will be replaced by a voiceover system, so at that point the options menu will only disable the subtitles for that person speaking. I’ve not added many hints but I will continue to place them at points where the player might need them, for example at the start of the World 1 boss battle, where it’s probably not obvious immediately what you should do.

I’ve also now finished World 3 Level 1, with a second section of the level at the top of the lift. This second portion introduces a new type of cube – the Alignment Cube –  which never rotates and will stay aligned to the world’s XYZ axes. This makes it very useful for standing on and riding all the way to the top of the level.

spikes-79

The full level looks pretty nice, and I’ll aim to get some of the older levels upto this graphical level in future updates.

spikes-80

That’s all for this update. Unfortunately I wasn’t able to make the player turn round along with the direction of gravity, or make the world turn round either, because quaternions appear to be the bane of my existence. I came pretty damn close, but it wasn’t smooth at all and felt confusing, so I’ll leave that for a future update. It can be played at the usual place here, and if you have any ideas for features or comments on the game, feel free to leave them below. Thanks for reading and have fun playing, tell your friends/family/dog!