所以我有一个物体,我试图根据Yaw,Pitch and Roll方案旋转,相对于物体自身的局部轴而不是全局空间的轴。根据{{3}},我需要按顺序执行旋转。我已经将其解释为:
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轴的旋转始终相对于全局空间,围绕X轴的旋转绕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;
(它们被传递给被移动的东西内部的函数,但没有其他任何东西用它们完成)。
思考?还有什么我应该调用以确保所有绕轴旋转的轴都是局部轴吗?
答案 0 :(得分:1)
加瑞克,
当你调用glRotate(angle,x,y,z)时,它会围绕你传递给glRotate的向量旋转。矢量从(0,0,0)变为(x,y,z)。
如果要围绕对象的局部轴旋转对象,则需要将对象gl转换为原点,执行旋转,然后将其转换回原点。
以下是一个例子:
//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();
}
答案 1 :(得分:0)
您的问题是您正在存储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轴上旋转将不会产生任何混淆效应)。 / p>