XNA旋转骨偏移翻译(XNA Rotate a bone with an offset transl

2019-10-31 13:41发布

我有一个具有已翻译的从父骨(它不位于母体骨的尾部)偏移的骨模型。 在搅拌机,其中模型制成,我可以旋转骨见附件网正常旋转。 然而,当我试图在我的代码旋转骨骼,似乎将其旋转大约父的尾巴。 我在矩阵数学(我最大的敌人)搞乱的东西了。

更新:

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

        base.Update(gameTime);
    }

相当标准,但如果它的事项,抽奖:

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

Answer 1:

好吧,这可能不是解决这一问题最简单的方法,但我的人谁也有类似问题的修复程序。

当装载模式,存储原始变换。 然后你乘骨骼的变换,像我一样。 最后你得到的原始转换翻译,oringalTranform.Translation例如,并获得骨的新转变。 调整为偏移量,bone.Transform * = Matrix.CreateTranslation(originalTranslation - newTranslation)。

下面是我的解决方案的代码片段:

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 Rotate a bone with an offset translation