Oynx

06-27-2001, 08:53 AM

Are there any programmers out there that can help me? I am writing a game that allows you to move around a 3D grid in any direction by turning to face the way you want to go (up, down, left or right) and then pressing a key to go forwards (or backwards) in that direction. I found some code online that does this but there is a problem with it - when you are looking straight up (90 degrees) and then head straight in that direction, you end up travelling at about 80 degree or so. In other words, you can never head straight up or down, only at a slope.

Here is the code... I hope someone can make some sense out of it because I can't. I would be perfect if it wasn't for this little problem.

--------------------------------------------------------------

float piover180 = 0.0174532925f;

if (nChar == VK_UP)

{

m_fXPos -= (float)sin(m_fHeading*piover180) * 0.03f;

m_fYPos -= (float)sin(m_fLookUpDown*piover180) * 0.03f;

m_fZPos -= (float)cos(m_fHeading*piover180) * 0.03f;

InvalidateRect(NULL);

}

if (nChar == VK_DOWN)

{

m_fXPos += (float)sin(m_fHeading*piover180) * 0.03f;

m_fYPos += (float)sin(m_fLookUpDown*piover180) * 0.03f;

m_fZPos += (float)cos(m_fHeading*piover180) * 0.03f;

InvalidateRect(NULL);

}

if (nChar == VK_RIGHT)

{

m_SectYRotate -= 1.0f;

m_fHeading = m_SectYRotate;

InvalidateRect(NULL);

}

if (nChar == VK_LEFT)

{

m_SectYRotate += 1.0f;

m_fHeading = m_SectYRotate;

InvalidateRect(NULL);

}

if (nChar == VK_PRIOR)

{

if (m_fLookUpDown < -90) m_fLookUpDown = -90;

m_fLookUpDown-= 1.0f;

InvalidateRect(NULL);

}

if (nChar == VK_NEXT)

{

if (m_fLookUpDown > 90) m_fLookUpDown = 90;

m_fLookUpDown+= 1.0f;

InvalidateRect(NULL);

}

--------------------------------------------------------------

If anyone can help, I'd be very thankful,

Jamie

Here is the code... I hope someone can make some sense out of it because I can't. I would be perfect if it wasn't for this little problem.

--------------------------------------------------------------

float piover180 = 0.0174532925f;

if (nChar == VK_UP)

{

m_fXPos -= (float)sin(m_fHeading*piover180) * 0.03f;

m_fYPos -= (float)sin(m_fLookUpDown*piover180) * 0.03f;

m_fZPos -= (float)cos(m_fHeading*piover180) * 0.03f;

InvalidateRect(NULL);

}

if (nChar == VK_DOWN)

{

m_fXPos += (float)sin(m_fHeading*piover180) * 0.03f;

m_fYPos += (float)sin(m_fLookUpDown*piover180) * 0.03f;

m_fZPos += (float)cos(m_fHeading*piover180) * 0.03f;

InvalidateRect(NULL);

}

if (nChar == VK_RIGHT)

{

m_SectYRotate -= 1.0f;

m_fHeading = m_SectYRotate;

InvalidateRect(NULL);

}

if (nChar == VK_LEFT)

{

m_SectYRotate += 1.0f;

m_fHeading = m_SectYRotate;

InvalidateRect(NULL);

}

if (nChar == VK_PRIOR)

{

if (m_fLookUpDown < -90) m_fLookUpDown = -90;

m_fLookUpDown-= 1.0f;

InvalidateRect(NULL);

}

if (nChar == VK_NEXT)

{

if (m_fLookUpDown > 90) m_fLookUpDown = 90;

m_fLookUpDown+= 1.0f;

InvalidateRect(NULL);

}

--------------------------------------------------------------

If anyone can help, I'd be very thankful,

Jamie