Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Quaternions

Discussion in 'Editor & General Support' started by Omar Rojo, May 4, 2007.

  1. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Is there a good resource where i can learn about quaternions ? Its a topic I know nothing about ! NOTHING ! and seems to be everywhere now that im learning advanced 3D programming. 8)

    A comparison with 3-component Vectors would be appreciated too.

    Thanks in advance.

    Omar Rojo
     
  2. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
  3. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Thank you very much
     
  4. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
  5. highpockets

    highpockets

    Joined:
    Jan 17, 2013
    Posts:
    55
    I know this is an ancient post, but while I was learning about Quaternions, it popped up a couple times in while searching. So I wanted to say my piece here.

    Most say to stay away from creating quaternions from scratch unless you know what you are doing. And I’ve read stuff like, “don’t mess with the secret sauce, just enjoy the delicious hamburger that is made with it”, basically saying don’t make the quaternions, just learn how to manipulate them with existing functions, but I think it’s important to learn about the secret sauce. When you do dabble for a bit in the secret sauce, you’ll start making the sauce and hamburger and you’ll feel great about it and realize it wasn’t so complicated to figure it out.

    So I think I’m half decent at mathematics, but most of the quaternion explanations made me go cross eyed and if that’s all you end up seeing, you’ll find it easy to agree with the crowd when they say Quaternions aren’t really human friendly. I think the real problem is that people over complicate things especially when they try to relay information to beginners.

    So here is my attempt at simplifying the spooky Quaternion. Usually when we want to rotate an object, it’s because we want to change the direction it is facing or we just want to make some fancy effect and direction isn’t really a concern. For the purpose of this example, let’s say we want to change the direction an object is facing.

    The object is a cardboard box, named Mr Box, with a smiley face on one side. Whatever way the smiley face is facing is our forward direction. Let’s say Mr. Box is in unity and he’s facing down the z axis which is conveniently known as the world’s forward axis (Vector3.forward or (0,0,1)).

    Now Mr. Box wants to turn left by 90 degrees or 0.5π radians. In this case, we know that Mr. Box can simply rotate about the y axis 90 degrees counter clockwise to face his desired target direction and it would be simply to make this modification using Euler angles, but we don’t want to do that because we might make more rotations and on other axis later and we could end up with the old gimbal lock problem that everyone hates.

    So, we know our forward facing direction is (0,0,1), we know the target direction is 90 degrees to the left (-1,0,0). We already know the axis that we want to rotate about is the y axis, but let’s allow Vector3.Cross(currentDirection, targetDirection) work it’s magic (always pass the start direction as the first param and the target direction as the second param). This returns the cross product which will always get the rotation axis (the xyz of quaternion) for you unless the 2 direction vectors are pointing in exact opposite directions, thus there is no plane to calculate a cross product.

    So the axis is a direction vector that you put in the “secret sauce” quaternion as the xyz parameters new Quaternion(axis.x, axis.y, axis.z, w)... That’s a pretty big portion of the secret sauce!! Now to calculate the angle. It always goes like this:

    Mathf.Sqrt(Vector3.Dot(targetDirection, targetDirection) * Vector3.Dot(currentDirection, currentDirection)) + Vector3.Dot(targetDirection, currentDirection);

    that’s the w ingredient of the secret sauce, it’s just a scalar/float that we will just call angle!! Now we create our quaternion:

    new Quaternion(axis.x, axis.y, axis.z, angle).normalized;

    We always normalize because unity wants a unit length Vector4. Now we apply our quaternion to Mr. Box:

    targetRotation = ourNewQuaternion * box.transform.rotation;
    box.transform.rotation = targetRotation;

    We multiply ourNewQuaternion by Mr. boxes rotation always in the same order, we cannot put box.transform.rotation * ourNewQuaternion because order always matters when multiplying quaternions.

    Hopefully this helps beginners wrap there head around Quaternions. Just think of it as the axis is a direction vector that represents xyz and the angle is the w, the reason why it doesn’t look like that when you see it usually is because it is normalized in unity and since it is only 1 unit length, it makes it kind of hard to read.

    Nice looking burger hey!

    Cheers,
     
unityunity