Expressing a quaternion with zero rotation

This is a discussion on Expressing a quaternion with zero rotation within the C++ Programming forums, part of the General Programming Boards category; Hi! How do you do this? Up to now all formulas I've seen will cause the x y and z ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    87

    Expressing a quaternion with zero rotation

    Hi!

    How do you do this? Up to now all formulas I've seen will cause the x y and z parts to collapse by sin(theta/2) if theta is 0. So how do you express say an orientation straight down the x-axis with no rotation about that axis? Thanks

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A zero rotation quaternion would be the unit quaternion.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    Thanks mate. Out of curiousity what would its values be for an orientation down the x-axis? I did some research on unit quats but got more confused

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    What do you mean an orientation down the x - axis?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    I think the question is, how can you produce a quaternion which points down the x-axis but expresses a rotation of 0? The answer is there's no such thing -- a rotation of 0 about the x-axis is a rotation of 0 about any axis. It's like asking which way the zero vector is pointing. It doesn't point anywhere at all.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,337
    O_o

    Left-handed devils or right-handed devils?

    *shrug*

    Either way, I'd say a rotation of 90 degrees should suit you.

    Soma

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,337
    how can you produce a quaternion which points down the x-axis but expresses a rotation of 0?
    Wow. If that's his question, he wasn't kidding about being confused.

    Soma

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    You'd better believe it!!!! I got it figured in the end, thanks guys

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Some code from my quaternion camera. Most of it taken from various tutorials on the internet and then plugged in to my existing system. Don't ask me how quaternions work b/c the math behind them is complex. I just know properties of them and various operations that can be performed on them. So this is one of those things in my book that 'just works'.

    Code:
    void X3DCamera::Pitch(float angle)
    {
        D3DXQUATERNION quat = m_qRot;
        D3DXQuaternionRotationAxis(&quat,TransformVector(&m_qRot, 
    			   &D3DXVECTOR3(1.0f, 0.0f, 0.0f)), 
    			   angle);
        
        m_qRot *= quat;
        D3DXQuaternionNormalize(&m_qRot,&m_qRot);
        m_inTransition = true;
    }
    
    void X3DCamera::Yaw(float angle)
    {
        m_yaw += angle;
        D3DXMATRIX matRot;
        D3DXQUATERNION quat;
        D3DXMatrixRotationY(&matRot,m_yaw);
        D3DXQuaternionRotationMatrix(&quat,&matRot);
    
        D3DXQuaternionRotationAxis(&quat,TransformVector(&quat, 
    	    		   &D3DXVECTOR3(0.0f, 1.0f, 0.0f)), 
                                                      angle);
        
        m_qRot *= quat;
        D3DXQuaternionNormalize(&m_qRot,&m_qRot);
        m_inTransition = true;
    }
    
    void X3DCamera::Roll(float angle)
    {
        D3DXQUATERNION quat = m_qRot;
        D3DXQuaternionRotationAxis(&quat,TransformVector(&m_qRot, 
    			   &D3DXVECTOR3(0.0f, 0.0f, 1.0f)), 
    			   angle);  
        m_qRot *= quat;
        D3DXQuaternionNormalize(&m_qRot , &m_qRot);
        m_inTransition = true;
    }
    
    D3DXVECTOR3* X3DCamera::TransformVector(D3DXQUATERNION *pOrientation, D3DXVECTOR3 *pAxis)
    {
        D3DVECTOR vNewAxis;
        D3DXMATRIX matRotation;
    
        // Build a matrix from the quaternion.
        D3DXMatrixRotationQuaternion(&matRotation, pOrientation); 
    
        // Transform the queried axis vector by the matrix.
        vNewAxis.x = pAxis->x * matRotation._11 + pAxis->y * matRotation._21 + pAxis->z *  
        matRotation._31 + matRotation._41; 
    
        vNewAxis.y = pAxis->x * matRotation._12 + pAxis->y * matRotation._22 + pAxis->z * 
        matRotation._32 + matRotation._42;
    
        vNewAxis.z = pAxis->x * matRotation._13 + pAxis->y * matRotation._23 + pAxis->z * 
        matRotation._33 + matRotation._43;
    
        memcpy(pAxis, &vNewAxis, sizeof(vNewAxis)); // Copy axis.
        return(pAxis);
    }
    
    void X3DCamera::GetViewMatrix(D3DXMATRIX *outMatrix)
    {
        D3DXMATRIX matRot;
        D3DXQuaternionNormalize(&m_qRot,&m_qRot);
        D3DXMatrixRotationQuaternion(&matRot,&D3DXQUATERNION(-m_qRot.x,-m_qRot.y,-m_qRot.z,m_qRot.w));
        
        D3DXVECTOR3 vecLook(matRot._13,matRot._23,matRot._33);
        D3DXVECTOR3 vecOrbitPos = m_vecTargetPos + vecLook * -m_fCamDist;
        D3DXMATRIX matOrbit;
        D3DXMatrixTranslation(&matOrbit,-vecOrbitPos.x,-vecOrbitPos.y,-vecOrbitPos.z);
        
        D3DXMATRIX matTrans;
        D3DXMatrixTranslation(&matTrans,-Pos.x,-Pos.y,-Pos.z);
        
        m_matView = matOrbit * matRot * matTrans;
        *outMatrix = m_matView;
    }
    Last edited by VirtualAce; 08-14-2010 at 11:44 AM.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    That's really nice! Can i copy that please?

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,337
    O_o

    I don't imagine that he would've posted the example if he didn't intend for you to make use of it.

    Soma

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I don't imagine that he would've posted the example if he didn't intend for you to make use of it.
    Hehe. One would think that such a conclusion was obvious but perhaps not.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    Or it could even be described as being polite......

    And i don't see why something you sweated over to make should just be acquired by someone without asking your permission first
    Last edited by shrink_tubing; 08-18-2010 at 04:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  3. Quaternion Rotation
    By psychopath in forum Game Programming
    Replies: 16
    Last Post: 07-23-2006, 03:28 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Camera problem: rolling
    By darksaidin in forum Game Programming
    Replies: 37
    Last Post: 08-15-2003, 08:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21