Thread: rotation question

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    rotation question

    i have 2 angles, lets say 350 degrees and 40 degrees.

    if i want to go from 350 to 40, the current code i use goes CCW, which is 310 degrees of rotation instead of 50.

    how can i calculate which direction to rotate in? (shortest direction)

    ive been working at this for 2 days now, maybe im over thinking it, i know it has to be simple.

    heres the code i use:

    Code:
    static uint nLastTime = timeGetTime();
    static float fRotationSpeed = 0.0005f;
    
    if (m_bTrackCamera && m_bRotateToAngle)
    {
    	if (timeGetTime() - nLastTime >= 10) // rotate every 10 ms
    	{
    		nLastTime = timeGetTime();
    
    		float fAngleFrom = D3DXToDegree(m_fRotZ);
    		float fAngleTo = D3DXToDegree(m_fRotateToAngle);
    
    		// find shorter rotation path ?
    		if (fAngleTo > fAngleFrom) // CW
    		{
    			m_fRotZ += fRotationSpeed + (0.001f * (fAngleTo - fAngleFrom));
    
    			if (m_fRotZ > D3DX_PI * 2.0f)
    				m_fRotZ -= D3DX_PI * 2.0f;
    
    			if (m_fRotZ >= m_fRotateToAngle)
    			{
    				m_fRotZ = m_fRotateToAngle;
    				m_bRotateToAngle = false;
    			}
    		}
    		else // CCW
    		{
    			m_fRotZ -= fRotationSpeed + (0.001f * (fAngleFrom - fAngleTo));
    
    			if (m_fRotZ < 0.0f)
    				m_fRotZ += D3DX_PI * 2.0f;
    
    			if (m_fRotZ <= m_fRotateToAngle)
    			{
    				m_fRotZ = m_fRotateToAngle;
    				m_bRotateToAngle = false;
    			}
    		}
    	}
    }
    Last edited by X PaYnE X; 12-22-2005 at 10:51 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Don't use euler angles. Try using quaternion representation of rotations instead of euler. Interpolating from one angle to the next is much easier with quaternions.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I have tried it many ways in my RTS but the only thing I have gotten it to work (and yes its a hack) is to simulate that you rotate a 1 degree in a for-loop, and then you count how many times that for-loop iterates and if it iterates more than 180 degrees you know you have to go the other way for the quickest way. Its hackish and not at all the best but it works.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    ill try both your methods.

    never dealt with quaternions before though.

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. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. saving contents of a function
    By speve in forum C Programming
    Replies: 5
    Last Post: 01-05-2002, 08:38 PM