Jump to content
• Advertisement

# Rotating an object around me

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement
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

##### Share on other sites
Why don't I need to translate the gun by X as well?

#### Share this 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

##### 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 this post

##### 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 this post

##### 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 this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Now

• 9
• 13
• 40
• 15
• 11
• Advertisement
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!