# How to calculate the position of an attached model after rotation?

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();
}

}
``````

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

-

## 1 Answer

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.

-
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