Rotating an object around me

Started by
9 comments, last by Sfpiano 19 years, 1 month ago
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.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
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);)
Why don't I need to translate the gun by X as well?
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
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
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.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
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
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.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Please, which are the initial positions for "you" and for the "gun"? Is it a first person application?
Yes it is first-person; the gun's position is always mine - Vec(-2.2f, 1.2f, 0.5f).
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Quote:Original post by Sfpiano
Yes 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?

This topic is closed to new replies.

Advertisement