gpt4 book ai didi

c# - 在 XNA 中旋转 3D 模型

转载 作者:太空狗 更新时间:2023-10-29 17:49:43 29 4
gpt4 key购买 nike

我是 XNA 的新手,我正在创建一个简单的游戏。抱歉,这可能真的很简单,但我找不到任何帮助。游戏中有一艘我用 Blender 制作的船,我希望能够通过旋转船的 X、Y 和 Z 轴来控制这艘船。这是我的代码:

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
RotationMatrix = Matrix.CreateRotationY(MathHelper.PiOver2) * Matrix.CreateRotationY (rotationY) * Matrix.CreateRotationX(rotationX) * Matrix.CreateRotationZ(rotationZ);

Matrix shipTransformMatrix = RotationMatrix * Matrix.CreateTranslation(ship.Position);

DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
// TODO: Add your drawing code here


base.Draw(gameTime);
}

public void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms)
{
//Draw the model, a model can have multiple meshes, so loop
foreach (ModelMesh mesh in model.Meshes)
{
//This is where the mesh orientation is set
foreach (BasicEffect effect in mesh.Effects)
{

effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
effect.Projection = projectionMatrix;
effect.View = viewMatrix;

}

//Draw the mesh, will use the effects set above.
mesh.Draw();
}
}

这将使飞船旋转,但不会沿着飞船的轴线旋转。如果我旋转 Y 轴(通过更改 rotationY 的值),船将沿其 Y 轴旋转。但是如果我旋转 X 轴或 Z 轴,船会根据世界的 X 轴和 Z 轴而不是它自己的轴旋转。我该怎么做才能让船绕自己的轴旋转?我需要对矩阵做一些不同的事情吗?谢谢

最佳答案

使用 CreateRotationX、CreateRotationY 和 CreateRotationZ 都应用围绕世界或全局轴的旋转。这意味着它会导致您的对象仅围绕世界/全局轴旋转,而不是您对象的局部轴。

使用 CreateFromAxisAngle 可让您输入所需的任何旋转轴,包括飞船自身的局部轴。

然而,您需要改变整体旋转范例,因为围绕任意轴(例如船的向上)旋转可能会同时导致 3 个角度值中的任何一个发生变化。跟踪所有不必要的困难。有一个更简单的方法:

只需以矩阵(或四元数)形式存储旋转而不是 3 个角度。

关于c# - 在 XNA 中旋转 3D 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10973628/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com