情况:我有一个三维模型,我试图围绕一个枢轴点旋转和轨道运行。我们现在说模型是铅笔,它的轴心点是Vector3(0,0,0)。铅笔笔尖应始终接触枢轴点,并应根据用户按钮按下每个轴旋转。铅笔到枢轴点的初始偏移是Vector3(0,10,0)。
我一直在使用四元数以避免万向节锁定,虽然我认为我可能通过错误使用四元数来体验它。
我目前的代码是:
public void UpdateRotation(Vector3 pivotPoint)
{
KeyboardState keyboardState = Keyboard.GetState();
Vector3 tempPos = pivotPoint - position;
if (keyboardState.IsKeyDown(Keys.X))
{
// Rotate around X axis.
Vector3 temp = position - pivotPoint;
temp = Vector3.Transform(temp, Matrix.CreateRotationX(rotateSpeed));
position = pivotPoint + temp;
// Update the direction the object is oriented.
rotation *= Quaternion.CreateFromAxisAngle(
Vector3.Right, rotateSpeed);
}
if (keyboardState.IsKeyDown(Keys.Y))
{
// Update the direction the object is oriented.
rotation *= Quaternion.CreateFromAxisAngle(
Vector3.Up, rotateSpeed);
}
if (keyboardState.IsKeyDown(Keys.Z))
{
// Rotate around Z axis.
Vector3 temp = position - pivotPoint;
temp = Vector3.Transform(temp, Matrix.CreateRotationZ(rotateSpeed));
position = pivotPoint + temp;
// Update the direction the object is oriented.
rotation *= Quaternion.CreateFromAxisAngle(
Vector3.Backward, rotateSpeed);
}
}
,这是在DrawModel()函数中:
effect.World = Matrix.CreateScale(gameobject.scale) *
Matrix.CreateFromQuaternion(gameobject.rotation) *
Matrix.CreateTranslation(gameobject.position);
基本上模型会围绕单轴旋转ok,我甚至可以围绕x或z和y轴旋转,但是如果我尝试围绕所有三个旋转,旋转将变得倾斜并且铅笔笔尖将会离开枢轴点。
任何关于这种情况的光明都会非常感激。我有一种感觉,我没有正确使用矩阵,但我无法找到有效的订单。
提前致谢。