Quaternion to yaw

Discussion in 'Game Development (Technical)' started by zoombapup, Sep 10, 2010.

  1. zoombapup

    Moderator Original Member

    Joined:
    Nov 25, 2004
    Messages:
    2,890
    Likes Received:
    0
    Hey all. Math problem help requested! :)

    Given the following:

    Code:
    	
    float Quaternion::getYaw()
    {
    	return asin(-2*(x*z - w*y));
    }
    
    I get in the range -Pi/2 to Pi/2, however I want the value in absolute range from 0..2Pi

    My problem is that I cant just translate and scale the value because the angles returned are not continuous but form a series of curves (like a sin/cos curve if you will). I'm sure there's an easy way to transform a sin/cos curve into a 0..X value, but its escaping me. Anyone know?
     
  2. buto

    Original Member

    Joined:
    Oct 2, 2006
    Messages:
    45
    Likes Received:
    0
    I'm not completly sure what you are trying to achieve, but I understand it like this:
    You want to create a function f:[-1, 1] -> [0, 2PI], so that sin(f(x)) = x. (I hope I don't overcomplicate things here).
    If this is right, then I'm afraid, that this won't work, since the problem is ambigous. 0 could for example be mapped to 0 or to PI. Thus f would not be a well defined function. In order to map to the full range between 0 and 2PI, you would need more parameters.
    I hope I didn't get it completly wrong!

    Kind regards,
    Sebastian
     
  3. buto

    Original Member

    Joined:
    Oct 2, 2006
    Messages:
    45
    Likes Received:
    0
    Probably this is better:
    Since all values of sin(x) for x between 0 and PI are positive, you can't map [-1, 1] to [0,PI] using asin.

    Regards,
    Sebastian
     
  4. zoombapup

    Moderator Original Member

    Joined:
    Nov 25, 2004
    Messages:
    2,890
    Likes Received:
    0
    The yaw method I'm using above returns -90 to +90 (in degrees), which isnt really what I'm after. I'm after a point on the circle 0..360 degrees. Given that its a yaw, that makes sense. Trouble is that I pass in a 0..360 when I create the quaternion, I now want to get it back out again only this is giving me -90..90 which isnt really that useful.

    Its basically the phase of the angle I'm getting, where as I want an absolute angle.
     
  5. ChrisP

    Indie Author

    Joined:
    Feb 5, 2007
    Messages:
    971
    Likes Received:
    0
    Judging by http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion it seems like you're computing the pitch, not the yaw. Try the last equation on that page.

    OTOH, your coordinate frame is quite likely different from the traditional one that page uses, so you might be stuck having to re-derive the conversion, which is a bit beyond me right at this moment. Or just mess around until you hit on a formula that works!
     
  6. xDan

    xDan New Member

    Joined:
    Mar 28, 2007
    Messages:
    107
    Likes Received:
    0
    I know little maths, but perhaps you have an entity with rotation as a quaternion and you want to get its "look angle" in the ground plane?

    If so, make a unit vector, rotate it by the quaternion, then you can use the X,Z components and trigonometry to calculate the yaw angle.

    Using the Irrlicht engine maths library I do something like this:-
    Code:
    // Extract the Y component from a rotation.
    // Rotation starting at {0,0,1}
    f32 extract_y_rotation(const core::quaternion &quat)
    {
        // Rotate a unit vector by the quaternion
        core::vector3df vec(0,0,1);
        vec = quat * vec;
        
        // Then get the yaw angle from that unit vector
        core::vector2df vec2d(vec.X,vec.Z);
        vec2d.normalize();
        return vec2d.getAngle();
    }
     
  7. zoombapup

    Moderator Original Member

    Joined:
    Nov 25, 2004
    Messages:
    2,890
    Likes Received:
    0
    You know for some reason Dan, that didnt occur to me :) hehe.. thanks.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer