-
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;
}
}
}
}
-
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.
-
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.
-
ill try both your methods.
never dealt with quaternions before though.