XNA使用偏移平移旋转骨骼 [英] XNA Rotate a bone with an offset translation

查看:108
本文介绍了XNA使用偏移平移旋转骨骼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,该模型的骨骼与父骨骼具有平移的偏移量(它不位于父骨骼的尾部).在制作模型的Blender中,我可以旋转骨骼,并看到附加的网格正确旋转.但是,当我尝试在代码中旋转骨骼时,似乎绕着父级的尾部旋转了骨骼.我搞砸了矩阵数学(我最大的敌人).

I have a model that has a bone which has a translated offset from the parent bone (it is not positioned at the tail of the parent bone). In Blender, where the model was made, I can rotate the bone and see the attached mesh rotate correctly. However, when I attempt to rotate the bone in my code, it seems to rotate it about the tail of the parent. I'm messing something up in the Matrix math (my worst enemy).

更新:

protected override void Update(GameTime gameTime)
    {
        bone.Transform =  Matrix.CreateFromYawPitchRoll(0f, 0.5f, 0f);

        base.Update(gameTime);
    }

非常标准,但是如果重要,请绘制:

Pretty standard, but if it matters, Draw:

private void DrawModel(Model model, GraphicsDevice graphics, Matrix viewMatrix, Matrix projectionMatrix)
    {
        Matrix[] boneTransforms = new Matrix[model.Bones.Count];
        model.CopyAbsoluteBoneTransformsTo(boneTransforms);

        Matrix worldMatrix = orientation * Matrix.CreateTranslation(position);

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
                effect.View = viewMatrix;
                effect.Projection = projectionMatrix;

                effect.EnableDefaultLighting();
                effect.PreferPerPixelLighting = true;

                // Set the fog to match the black background color
                effect.FogEnabled = true;
                effect.FogColor = Vector3.Zero;
                effect.FogStart = 1000;
                effect.FogEnd = 3200;
            }
            mesh.Draw();
        }
    }

以下是显示问题的屏幕截图 http://s1231.photobucket.com/albums/ee516/Neovivacity/?action = view& current = boneRotation.png

Here's screen shots to show the problem http://s1231.photobucket.com/albums/ee516/Neovivacity/?action=view&current=boneRotation.png

推荐答案

好的,这可能不是解决此问题的最直接的方法,但是对于有类似问题的任何人,我都有解决方法.

Ok, this may not be the most straightforward way of solving this, but I have a fix for anyone who's also having a similar issue.

在加载模型时,存储原始变换.然后像我一样将骨骼的变换倍增.最后,您获得了原始转换Translation,例如oringalTranform.Translation,并获得了骨头的新转换.要调整偏移量,请bone.Transform * = Matrix.CreateTranslation(originalTranslation-newTranslation).

When loading the Model, store the original transform. Then you multiply the bone's transform like I did. Finally you get the original transforms Translation, oringalTranform.Translation for example, and get bone's new transform. To adjust for the offset, bone.Transform *= Matrix.CreateTranslation(originalTranslation - newTranslation).

这是我的解决方案中的代码段:

Here's a code snippet from my solution:

public void LoadContent(ContentManager content)
    {
        Model = content.Load<Model>(ModelName);
        //Set the Bone pointers and transform matrices
        AileronLBone = Model.Bones["LAileron"];
        AileronRBone = Model.Bones["RAileron"];
        ElevatorBone = Model.Bones["Elevator"];
        RudderBone = Model.Bones["Rudder"];
        FlapsBone = Model.Bones["Flaps"];
        if (AileronLBone != null) AileronLTransform = AileronLBone.Transform;
        if (AileronRBone != null) AileronRTransform = AileronRBone.Transform;
        if (ElevatorBone != null) ElevatorTransform = ElevatorBone.Transform;
        if (RudderBone != null) RudderTransform = RudderBone.Transform;
        if (FlapsBone != null) FlapsTransform = FlapsBone.Transform;
    }

    public void Update(GameTime gameTime)
    {
        SetOffsetRotation(ElevatorBone, ElevatorTransform, ElevatorRotation);
    }

    public void SetOffsetRotation(ModelBone bone, Matrix transform, float rotation)
    {
        Vector3 oringalTrans = transform.Translation;
        bone.Transform *= Matrix.CreateRotationX(rotation);
        Vector3 newTrans = bone.Transform.Translation;
        bone.Transform *= Matrix.CreateTranslation(oringalTrans - newTrans);
    }

这篇关于XNA使用偏移平移旋转骨骼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆