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.
  2. Dismiss Notice

FPS 1.16 Quaternion stuff

Discussion in 'Scripting' started by jbecana, Mar 27, 2012.

  1. jbecana

    jbecana

    Joined:
    Feb 14, 2012
    Posts:
    22
    I'm following this nice videos but I'm stuck with the rotation stuff. In this part, the gun must follow the camera while it rotates. I've got it working, but I don't know what I'm doing by multiplying these quaternions:

    transform.position = cameraObject.transform.position + ( Quaternion.Euler (0, targetYRotation, 0) * Vector3 (holdXpos * hipToAimRatio, holdYpos* hipToAimRatio, holdZpos * holdFactor * hipToAimRatio) + Quaternion.Euler (targetXRotation, targetYRotation, 0) * Vector3 (0, 0, currentRecoilZPos) );

    targetXRotation = Mathf.SmoothDamp (targetXRotation, cameraObject.GetComponent (scpMouseLook).cameraXRotation, targetXRotationV, followingSpeed);
    targetYRotation = Mathf.SmoothDamp (targetYRotation, cameraObject.GetComponent (scpMouseLook).cameraYRotation, targetYRotationV, followingSpeed);

    // Follow camera
    transform.rotation = Quaternion.Euler ( targetXRotation, targetYRotation, 0 );

    In case you're wondering, holdFactor is just a float to correct my gun in Z when I look up or down so I don't see it floating in space. I really like to know what's the meaning of these piece of code (black line). ¿What is the purpose of this? I mean, ¿what do you get by multiplying a quaternion and a vector3? ¿And why does it stop working if I move the targetXRotation and targetYRotation assignment (red lines) before the transform.position assignment? I had these lines before in my code, but then it stopped working ( or I realized then) after adding the recoil thing from this chapter, so I have had to put them in the exact place shown in the tutorial to get it right. I've a rough idea about what quaternions are for, but no idea about how to operate them with a minimum sense...
    Thanks in advance.
     
  2. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
  3. jbecana

    jbecana

    Joined:
    Feb 14, 2012
    Posts:
    22
    Thanks for your answer magnetix. I totally agree with you and I hope it's not necessary to know quaternions deeply to use them. I'm just interested in the practical use of them in any case. I'd appreciate if someone could extent the Quaternion info in the documentation, and I don't mean the mathematical concepts, complex numbers etc... just simple pieces of code with the most common operations or uses and their explanation. Being practical, I assume a quaternion is like a coordinate system, so transform rotation means the local coordinate system for my object. First thing came to my mind was if a quaternion is composed of four values, and a vector3 is three values ¿how do they multiply? I guess there is an internal conversion and multiplying a vector3 position by a Quaternion rotation is a way to move that position in a local direction defined by Quaternion coordinate system. So more or less in that line of code the first Quaternion product moves the gun related to a coordinate system aligned with the camera Y rotation (targetYRotation), and the second moves the gun in z axis relative to the camera plane (targetXRotation,targetYRotation), but this just speculation.

    When I have time I'll look for this in internet, maybe I find some simple 3D specific cases in internet...
    Thanks.
     
  4. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
    Well, I don't really have time to write out a tutorial on quaternions but Google will provide lots of info on them. I just Googles "quaternions tutorial" and there's plenty of info. For multiplying a vector and quaternion, it's easy to give a vector a fourth component of 1, which makes it a homogenous vector. This is all stuff I covered in my degree course, but again there's plenty of info out there. You can think of a quaternion as an axis-angle representation of a rotation, the first three components being the axis of rotation.

    In that first line, you are finding an overall position by adding two vectors. These vectors are rotated individually by the quaternions based on current rotation values. So you are maintaining the vectors (such as a holdPos offset) and applying the rotation to align to certain objects. I try not to think too much about individual coordinate systems, but think in terms of layering up transformations. If you think about someone holding a gun, the gun would have the hand transformation applied to it to match the hand position and orientation and the barrel of the gun might then have a rotation in the Z axis to rotate relative to the gun (and the hand). I'm not sure if that helps or is just confusing!
     
  5. jbecana

    jbecana

    Joined:
    Feb 14, 2012
    Posts:
    22
    Thank you magnetix, your explanation has been very helpful. I did a quick search in Google but the first results provided more of a theoretical explanation, but I think I understand why we use quaternions and what their operations do. Being practical, if it's OK to see the product of a rotation (quaternion) by a vector (position) as a rotation of the position based on the quaternion rotation ( as if that position were a child and the quaternion represented the parent orientation ) then it's fine for me. That's why I think of coordinate systems when I try to understand how it works, because any object rotation is stored as a quaternion, so this product can result in the same effect as if both objects were parented, not really sure, but that's what I got. I understand 99% of the code in FPS1 tutorial at this point, but I'm not so familiar with the maths involved in 3D stuff. It's a same because I've being using 3D software for the last 10 years, but I didn't have to look inside this till now...
    Thanks again for your time.