所以我得到了一个物体,我试图根据偏航,俯仰和滚动方案旋转,相对于物体自己的局部轴,而不是全局空间的轴。根据this的说法,我需要按该顺序执行旋转。我将其解释为:
glRotatef(m_Rotation.y, 0.0, 1.0, 0.0);
glRotatef(m_Rotation.z, 0.0, 0.0, 1.0);
glRotatef(m_Rotation.x, 1.0, 0.0, 0.0);
但是,绕Y轴和Z轴旋转不起作用。绕Y轴旋转始终相对于全局空间,绕z轴旋转的效果绕X轴旋转为0,否则会造成混乱。
为了确认,我也尝试了相反的顺序,但这也不起作用。我想我也试过了所有其他的订单,所以问题一定是别的什么。可能是这样吗?
这是我获得旋转的方法:
///ROTATIONS
sf::Vector3<float> Rotation;
Rotation.x = 0;
Rotation.y = 0;
Rotation.z = 0;
//ROLL
if (m_pApp->GetInput().IsKeyDown(sf::Key::Up) == true)
{
Rotation.x -= TurnSpeed;
}
if (m_pApp->GetInput().IsKeyDown(sf::Key::Down) == true)
{
Rotation.x += TurnSpeed;
}
//YAW
if (m_pApp->GetInput().IsKeyDown(sf::Key::Left) == true)
{
Rotation.y -= TurnSpeed;
}
if (m_pApp->GetInput().IsKeyDown(sf::Key::Right) == true)
{
Rotation.y += TurnSpeed;
}
//PITCH
if (m_pApp->GetInput().IsKeyDown(sf::Key::Q) == true)
{
Rotation.z -= TurnSpeed;
}
if (m_pApp->GetInput().IsKeyDown(sf::Key::E) == true)
{
Rotation.z += TurnSpeed;
}
然后将它们添加到m_Rotation中:
//Rotation
m_Rotation.x += Angle.x;
m_Rotation.y += Angle.y;
m_Rotation.z += Angle.z;
(它们被传递给正在移动的对象内部的一个函数,但没有对它们做任何其他事情)。
有什么想法?还有什么是我应该调用的,以确保所有被旋转的轴都是局部轴?
发布于 2012-03-18 18:56:09
加里克
当您调用glRotate (角度,x,y,z)时,它围绕传递给glRotate的向量旋转。向量从(0,0,0)到(x,y,z)。
如果要围绕对象的局部轴旋转对象,则需要将对象glTranslate到原点,执行旋转,然后将其平移回原来的位置。
下面是一个示例:
//Assume your object has the following properties
sf::Vector3<float> m_rotation;
sf::Vector3<float> m_center;
//Here would be the rotate method
public void DrawRotated(sf::Vector<float> degrees) {
//Store our current matrix
glPushMatrix();
//Everything will happen in the reverse order...
//Step 3: Translate back to where this object came from
glTranslatef(m_center.x, m_center.y, m_center.z);
//Step 2: Rotate this object about it's local axis
glRotatef(degrees.y, 0, 1.0, 0);
glRotatef(degrees.z, 0, 0, 1.0);
glRotatef(degrees.x, 1.0, 0, 0);
//Step 1: Translate this object to the origin
glTranslatef(-1*m_center.x, -1*m_center.y, -1*m_center.z);
//Render this object rotated by degrees
Render();
//Pop this matrix so that everything we render after this
// is not also rotated
glPopMatrix();
}
发布于 2013-01-06 16:49:41
您的问题是,您正在存储您的x,y,z旋转,并累加到它们。然后,当您渲染时,您将在单位矩阵上执行总累积旋转(您将全局执行所有旋转)。从你的渲染循环中注释掉你的标识调用。并确保在初始化函数中设置了标识。然后
rotate as normal
m_Rotation.x = m_Rotation.y = m_Rotation.z = 0.0f;
//clear your rotations you don't want them to accumulate between frames
glpush
translate as normal
(all other stuff)
glpop
//you should be back to just the rotations
//glclear and start next frame as usual
我相信你在接受了原始答案之后就发现了这一点。旋转或平移的顺序不会影响旋转发生在哪个轴上,而是影响执行旋转的点。例如,将一颗行星旋转15度会使其在全球轴上旋转15度。将其从原点平移,然后旋转将导致它在平移的距离处环绕原点(如果在与旋转相同的轴上,则在x轴上平移,然后在y轴上旋转将不会有任何混淆效果)。
https://stackoverflow.com/questions/9702867
复制