# Rotation independent rotation

I came up with some cube rotating, but I could not make it smooth and easy to use because as the cube turned - the keys that handle the turning changed their purpose. Link to demo. The demo rotation is done by increasing euler angles and using Quaternion.Euler() to get a quaternion for Quaternion.Slerp().

The ideal rotating would be like below: the keys stay the same, no matter how the cube is rotated. A/D turn the cube from "left/right". W/S turn the cube "up/down". Q/E turns the cube's current face.

But I have no idea where to start when making the above. How could the above be achieved?

EDIT Rioki's answer solved this question. I was previously using local rotation but changing it to world rotation fixed this. Link to demo.

• Looks like you're very close. Why not describe exactly what's not working? – MichaelHouse Apr 23 '13 at 14:03

This is somewhat simple. When working with quaternions, to alter the rotation you multiply the current orientation with the change you want to apply. Now there are two ways to to do it:

result = orig * change


and

result = change * orig


The nice thing about this is that the one rotates it in the local coordinate system and the other rotates it in the global coordinate system. (As Esa points out, the first rotates around the local coordinate system and the second rotates around the global one.)

I am not sure what you are trying to do, but slerp may be overkill, since it interpolates between two states. The way you describe the problem, is you check what keys are pressed and how much time elapsed since the last tick and then do something like:

if (is_pressed('W'))
orientation = orientation * Quaternion.Euler(dt, 0, 0);
if (is_pressed('S'))
orientation = orientation * Quaternion.Euler(-dt, 0, 0);
if (is_pressed('A'))
orientation = orientation * Quaternion.Euler(0, dt, 0);
if (is_pressed('D'))
orientation = orientation * Quaternion.Euler(0, -dt, 0);

• <ul> -Local rotation => orientation * Quaternion.Euler<ul/> World rotation => Quaternion.Euler * orientation – Esa Apr 24 '13 at 7:13
• Battling with the formatting, but I think people can see what I meant :). – Esa Apr 24 '13 at 7:20