2012-12-28 93 views
1

我正在编写xna的基本游戏。我开始在玩家的右臂上放置一个物体(例如武器)。当我将角色向左或向右移动时。但是当我旋转我的设备没有正确定位。 我完全理解,有必要重新计算基于旋转完成的新位置,但我不知道如何。 这里是我的代码和图片 一千谢谢如何计算旋转[XNA]后附加模型的位置?

 //Function that will draw the current item selection in the player's hand    
private void draw_itemActionInUse(Model modelInUse) 
      { 

       int handIndex = skinningData.BoneIndices["Hand_Right"]; 

       Matrix[] worldTransforms = animationPlayer.GetWorldTransforms(); 

       Matrix rotationMatrixCalcul = Matrix.CreateRotationY(player.Rotation.Y); 
//Here I calculate the new position of the item, but it does not work 
       Vector3 newPosition= Vector3.Transform(new Vector3(player.Position.X, player.Position.Y + 4, player.Position.Z), rotationMatrixCalcul); 
       foreach (ModelMesh mesh in modelInUse.Meshes) 
       { 
        foreach (BasicEffect effect in mesh.Effects) 
        { 

         effect.World = 
    worldTransforms[handIndex] 
    * 
    Matrix.CreateScale(2) 
    * 
    Matrix.CreateRotationY(player.Rotation.Y) 
    * 
    Matrix.CreateTranslation(newPosition); 

         effect.View = View_; 
         effect.Projection = Projection_; 

         effect.EnableDefaultLighting(); 
        } 

        mesh.Draw(); 
       } 

      } 

Position initial

图A: 位置:X:0,Y:0; Z:0 角度:90 图B: 位置:x:2; y:4; z:0 角度:90 enter image description here 图A: 位置:X:1; Y:0; Z:1 角:35 图B: 位置:如何演算该位置? 角:35

+0

你见过这个吗? http://stackoverflow.com/questions/13890820/rotating-objects-attached-to-other-objects/13893855#13893855 – neeKo

+0

谢谢!你可以写这个答案吗? –

回答

1

基于this stackoverflow answer,对于附加的对象的变换是:

Matrix positionRotationMatrix = Matrix.CreateTranslation(-parentPosition) 
           * Matrix.CreateFromQuaternion(parentRotation) 
           * Matrix.CreateTranslation(parentPosition); 
Vector3 translation = Vector3.Transform(parentPosition + relativePosition, 
           positionRotationMatrix); 

Matrix worldMatrix = Matrix.CreateScale(scale) 
       * Matrix.CreateFromQuaternion(rotation) 
       * Matrix.CreateFromQuaternion(parentRotation) 
       * Matrix.CreateTranslation(translation); 

变量名是自解释的。

+0

你是一个大老板! –

+0

最后一个问题! “ParentPosition”等于玩家的位置或对象? –

+1

@MehdiBugnard对象所在实体的位置 - 可能是您的案例中的玩家。 – neeKo