Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm programming a basic game on XNA. I started to place an object (eg weapon) attached to the right arm of my player. When I move my character forward behind left or right all right. But when I rotates my equipment is not positioned correctly. I am fully understands it is necessary to recalculate the new position based on the rotation done but I do not see how. Here is my code and pictures

        //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

Figure A: position: x:0;y:0;z:0 angle : 90 Figure B: position: x:2;y:4;z:0 angle : 90 enter image description here Figure A: position: x:1;y:0;z:1 angle : 35 Figure B: position: How calcul this position ? angle : 35

share|improve this question

1 Answer 1

You'll need to position the weapon in the player's local space, and apply the same transformation to it that you apply to the player model.

More specifically, figure out the vector from the player's origin to the weapon's origin, before applying any rotation. In your case it seems this may be (2, 0, -2) or some such. Then rotate that vector by the player's rotation, and then add to it the player's translation.

share|improve this answer
    
thanks. I i'm going test this. And i go back return my result ;-) –  Mehdi Bugnard Dec 29 '12 at 23:01
    
OHH thank you so much! This is so that I was stuck here! ----> More specifically, figure out the vector from the player's origin to the weapon's origin, before applying any rotation. In your case it seems this may be (2, 0, -2) or some such –  Mehdi Bugnard Dec 29 '12 at 23:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.