XNA 中的四元数旋转

hey*_*red 4 c# xna rotation

我做的是对的吗?

显然不是因为否则我不会在这里发布问题,但我正在尝试围绕另一个模型对一个模型进行四元数旋转。

假设我有一个具有向量 3 位置和浮动旋转角度的盒模型。我还有一个指向盒子模型的截头锥体模型,其位置可以说距盒子模型 50 个单位。平截头体还有一个向量 3 位置和一个四元数旋转。

在场景 1 中,长方体和平截头体“未旋转”。这一切都很好。在场景 2 中,我仅旋转盒子,并且希望平截头体随之旋转(有点像追逐相机),平截头体始终直接指向盒子,并且与盒子的距离与未旋转的距离相同。显然,如果我只是通过对盒子和平截头体使用 Matrix.CreateRotationY() 来旋转模型和平截头体,则平截头体会稍微向侧面偏移。

所以我认为平截头体围绕盒子的四元数旋转是最好的?为此,我尝试了以下方法,但没有成功。它在屏幕上绘制我的模型,但它也会在屏幕上绘制看起来像一个巨大盒子的东西,无论我将相机移动多远,盒子总是挡住路

出于测试目的,我有 3 个盒子及其 3 个相关的截锥体 在我的 Game1 类中,我使用位置和旋转来初始化盒子 [0]

boxObject[0].Position = new Vector3(10, 10, 10);
boxObject[1].Position = new Vector3(10, 10, 10);
boxObject[2].Position = new Vector3(10, 10, 10);

boxObject[0].Rotation = 0.0f;
boxObject[1].Rotation = 45.0f;
boxObject[2].Rotation = -45.0f;
Run Code Online (Sandbox Code Playgroud)

因此,所有 3 个盒子都绘制在相同位置但角度不同。然后为了做截锥体,我开始他们的位置:

float f = 50.0f;
frustumObject[0].Position = new Vector3(boxObject[0].Position.X,
            boxObject[0].Position.Y, boxObject[0].Position.Z + f);
frustumObject[1].Position = new Vector3(boxObject[1].Position.X,
            boxObject[1].Position.Y, boxObject[1].Position.Z + f);
frustumObject[2].Position = new Vector3(boxObject[2].Position.X,
            boxObject[2].Position.Y, boxObject[2].Position.Z + f);
Run Code Online (Sandbox Code Playgroud)

然后尝试围绕其关联的盒子模型旋转:

frustumObject[0].ModelRotation = new Quaternion(boxObject[0].Position.X,
            boxObject[0].Position.Y, boxObject[0].Position.Z + f, 0);
frustumObject[0].ModelRotation = new Quaternion(boxObject[0].Position.X,
            boxObject[0].Position.Y, boxObject[0].Position.Z + f, 45);
frustumObject[0].ModelRotation = new Quaternion(boxObject[0].Position.X,
            boxObject[0].Position.Y, boxObject[0].Position.Z + f, -45);
Run Code Online (Sandbox Code Playgroud)

最后,为了绘制模型,我在 GameModel 类中 Draw() 它们,该类还具有:

public Model CameraModel { get; set; }
public Vector3 Position { get; set; }
public float Rotation { get; set; }
public Quaternion ModelRotation { get; set; }

public void Draw(Matrix view, Matrix projection)
    {
        transforms = new Matrix[CameraModel.Bones.Count];
        CameraModel.CopyAbsoluteBoneTransformsTo(transforms);

        // Draw the model
        foreach (ModelMesh myMesh in CameraModel.Meshes)
        {
            foreach (BasicEffect myEffect in myMesh.Effects)
            {
                // IS THIS CORRECT?????
                myEffect.World = transforms[myMesh.ParentBone.Index] *
                    Matrix.CreateRotationY(Rotation) * Matrix.CreateFromQuaternion(ModelRotation) * Matrix.CreateTranslation(Position);

                myEffect.View = view;
                myEffect.Projection = projection;

                myEffect.EnableDefaultLighting();
                myEffect.SpecularColor = new Vector3(0.25f);
                myEffect.SpecularPower = 16;
            }

            myMesh.Draw();
        }
    }
Run Code Online (Sandbox Code Playgroud)

谁能发现我哪里出错了?是因为我在 Draw() 中进行了两种类型的旋转吗?

myEffect.World = 变换[myMesh.ParentBone.Index] * Matrix.CreateRotationY(Rotation) * Matrix.CreateFromQuaternion(ModelRotation) * Matrix.CreateTranslation(Position);

小智 5

乍一看,最好使用静态创建方法(例如Quaternion.CreateFromAxisAngle(Vector3. UnitY, rotation). 四元数的 X、Y、Z 和 W 值与位置没有任何关系。方便的静态方法可以解决棘手的数学问题。

在您的情况下,似乎您希望在盒子旋转时保持平截头体指向盒子的同一侧,因此围绕盒子旋转平截头体。这需要对绘制方法中完成的转换采取稍微不同的方法。

为了围绕另一个对象旋转一个对象,首先需要平移该对象,以便所需旋转的中心位于原点。然后旋转对象并将其平移与第一步相同的量。

因此,在您的情况下,类似的事情应该可以做到(遵循未经测试的示例代码);

// Construct the objects

boxObject.Position = new Vector3(10, 10, 10);
boxObject.Rotation = 45.0f;

frustumObject.Position = new Vector3(0, 0, 50f); // Note: this will be relative to the box (makes the math a bit simpler)
frustumObject.TargetPosition = boxObject.Position;
frustumObject.ModelRotation = Quaternion.CreateFromAxisAngle(Vector3. UnitY, boxObject.Rotation); // Note: this rotation angle may need to be in radians.


// Box Draw()
// Draw the box at its position, rotated about its centre.
myEffect.World = transforms[myMesh.ParentBone.Index] * Matrix.CreateTranslation(Position) * Matrix.CreateRotationY(Rotation);

// Frustum Draw()
// Draw the frustum facing the box and rotated about the boxes centre.
myEffect.World = transforms[myMesh.ParentBone.Index] * Matrix.CreateTranslation(Position) * Matrix.CreateFromQuaternion(ModelRotation) * Matrix.CreateTranslation(TargetPosition);
Run Code Online (Sandbox Code Playgroud)

假设:

  • 盒子绕自己的中心旋转
  • 平截头体保持面向盒子并绕盒子中心旋转

希望这可以帮助。