# Rotating an object around me

This topic is 5059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a gun that I want to rotate as I rotate, but I always want it pointed outward; the way I have it now it rotates around its center without respect to my position.

##### 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 on other sites
Why don't I need to translate the gun by X as well?

##### 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]

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 on other sites
D3DXVECTOR3 v = gun.vPos;float offset = gun.length/2;D3DXMatrixTranslation(&x, 0.0f, 0.0f, offset);matWorldRot *= x;D3DXMatrixRotationY(&x, angles.y);matWorldRot *= x;D3DXMatrixTranslation(&x, v.x, v.y, v.z);matWorldRot *= x;

I tried this, and as soon as I move the gun flies off the screen.

##### Share on other sites
Assuming the gun.vPos is the same as the players position.....

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.

-Shaun

##### Share on other sites
Well unfortunately it's not working. It puts the gun behind me and the more I change my position, the farther away from me the gun goes.

##### Share on other sites
Please, which are the initial positions for "you" and for the "gun"? Is it a first person application?

##### Share on other sites
Yes it is first-person; the gun's position is always mine - Vec(-2.2f, 1.2f, 0.5f).

##### Share on other sites
Quote:
 Original post by SfpianoYes it is first-person; the gun's position is always mine - Vec(-2.2f, 1.2f, 0.5f).

If the gun follows your rotation, the gun's position can not be always (your position) - Vec.
Initially, are you (the camera's eye) at the origin and look at to the Vec(-2.2f, 1.2f, 0.5f) direction?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 11
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015646
×