2012-01-09 10 views
0

私は、骨が親骨の末尾に配置されていない親骨からオフセットされた骨を持つモデルを持っています。モデルが作成されたBlenderでは、私は骨を回転させて、付属のメッシュが正しく回転するのを見ることができます。しかし、コード内でボーンを回転しようとすると、親の末尾を中心に回転させるように見えます。私は行列の数学(私の最悪の敵)で何かを台無しにしています。XNAオフセット変換を使用してボーンを回転

更新:

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

+0

おそらく、あなたはworldMatrixが最初に来るように、あなたの乗算の順序を変更する必要がありますか?私は確信していませんが、あなたは試すことができます – annonymously

答えて

1

問題を表示するためにスクリーンショットのは[OK]を、これがないかもしれませんこれを解決する最も簡単な方法ですが、同様の問題も抱えている人のための修正があります。

モデルを読み込むときは、元の変換を保存します。それから、私のように骨の変形を倍増させます。最後に、元のトランスレーションの翻訳、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); 
    } 
関連する問題