Share this post

Link to post

Share on other sites

At the moment you're rotating the gun and then moving it. You want to move the gun outwards by the radius of "you", rotate it, then move it to wherever you want in world space. This is because all rotations are done about the origin.So, if "you" are 10 units in X and Z, you'd translate the gun by 10 units in Z (D3DXMatrixTranslation(0.0f,0.0f,10.0f);), then rotate it by whatever angle you want (D3DXMatrixRotationY(fAngle);), then move it to your position (D3DXMatrixTranslation(fX, fY, fZ);)

Share this post

Link to post

Share on other sites

Because the gun just needs to be 10 units away from the origin. Actually, if the gun is facing right, you need to translates it by (10,0,0).

If you imagine a gun sitting in space with a needle through it vertically (the Y axis), you can rotate about that (which is what you were doing before) and the gun rotates around the center.If you move the centre of rotation to 10 units behind the gun, and rotate about that, then the gun seems to swing around the space. This kind of thing is hard to explain [smile]If you stretch your arm out in front of you and then you turn 90 degrees right, your hand pivots about your body. But if you have your hand by your side (ideally your hand would be in the center of your body, but obviously that's not possible :P) and you turn 90 degrees, your hand doesn't move at all, it just turns. This is what you're doing when your move the gun. When you translate it to (0,0,10), its the same as stretching your arm out in front of you. Then you rotate, so your hand (the gun) moves appropriately around you. Then, finally, you move the gun to wherever you want it to be in space.

I'd advise reading up on some geometry and possibly matrix math, because it's a fairly basic concept that you need to be able to grasp fully, and my explanation is rubbish :P

Share this post

Link to post

Share on other sites

Make sure you begin your transformations with an identity matrix. It looks like you are accounting for all transformations from the origin correctly - moving the gun out from the origin along the Z axis so that its base is touching the origin, then rotating it by the players rotation, then translating it to its position. Butif you begin with a matWorldRot that already has transformations on it (which it probably does if you already transformed an object and did not re-set an identity matrix in there.

Also, make sure your gun mesh is actually cenered around the the origin to begin with. Often a model will be created so that its not exactly centered on the origin in the model tool, and all hell will break loose if you expect it to rotate around the center.