我这样做对吗?
显然不是因为否则我不会在这里发布问题,但我正在尝试围绕另一个模型对一个模型进行四元数旋转。
假设我有一个具有向量 3 位置和浮动旋转角度的盒模型。
我还有一个指向盒子模型的截头锥体模型,其位置可以说距盒子模型 50 个单位。平截头体还有一个向量 3 位置和一个四元数旋转。
在场景 1 中,长方体和平截头体“未旋转”。这一切都很好。
在场景 2 中,我仅旋转盒子,并且希望平截头体随之旋转(有点像追逐相机),平截头体始终直接指向盒子,并且距盒子的距离与未旋转距离相同。显然,如果我只是通过对盒子和平截头体使用 Matrix.CreateRotationY() 来旋转模型和平截头体,则平截头体会稍微向侧面偏移。
所以我认为平截头体围绕盒子的四元数旋转是最好的?
为此,我尝试了以下方法,但没有成功。它在屏幕上绘制我的模型,但它也会在屏幕上绘制看起来像一个巨大盒子的东西,无论我将相机移动多远,盒子总是挡住路
为了测试的目的,我有 3 个盒子和它们的 3 个相关的平截头体
在我的 Game1 类中,我用位置和旋转初始化了 box[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;
因此,所有 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);
然后尝试围绕其关联的盒子模型旋转:
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);
最后,为了绘制模型,我在 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();
}
}
谁能发现我哪里出错了?是因为我在 Draw() 中进行了两种类型的旋转吗?
myEffect.World = 变换[myMesh.ParentBone.Index] *
Matrix.CreateRotationY(旋转) * Matrix.CreateFromQuaternion(ModelRotation) * Matrix.CreateTranslation(Position);