DirectX: Rotating my mesh with first person camera

Hey guys, I really need some expertise here. I'm developing a game engine in DirectX and all is going smoothly -- however, what may seem like a very simple problem I just cannot for the life of me find out. My engine imports .X mesh's and is able to place them anywhere on the map, rotate them, scale them, etc, with a CMeshInstance and CWorldTransform class. My engine renders BSP tree maps with collision detection/gravity/physics and the like, and I can place objects anywhere, and when the player runs over them he/she can collect them/use them. In this instance, the player picks up an AK47. Now, obviously I'd like to display that AK47.
Screenshot: https://drive.google.com/open?id=189hiJJKe8UXu0Q-a2gxilmtQG-0uzGE9

Problem is, I cannot for the life of me figure out how to keep the rifle aimed relative to the player's direction (vCamDir). If I turn to the right, i'll stare right into the gun or beyond it. I don't really have a camera class for the BSP scene, I simply use input from the mouse to determine Pitch/Yaw/Roll with

D3DXMatrixRotationYawPitchRoll(&matRot,
0,
D3DXToRadian(g_fSpinY),
-D3DXToRadian(g_fSpinX)); /*But this won't work with the mesh, but creates a perfect first person camera for the BSP scene*/

My Meshes are loaded as follows:

m_pMeshes[2].SetXPosition(vGunPos.x);
m_pMeshes[2].SetYPosition(vGunPos.y);
m_pMeshes[2].SetZPosition(vGunPos.z);
//....
m_pMeshes[2].Render(); //Render AK47

I can use m_pMeshes[2].RotateX(), RotateY(), RotateRel(), etc, but I simply cannot find a relationship between my mouse spin variables (which are determined by dx and dy vars from obtaining the mouse coords).

How can I keep the mesh/rifle rotating with the player? I have a D3DXVERTEX3 for the players direction, a vertex for position, etc, but I just don't know what math I should use to achieve this. And I am *not* using sprites with alpha blending to *pretend* there's a rifle in the bottom screen :P I want to use a real mesh.

The rifle's mesh rotation is handled by the following function:
m_pMeshes[2].RotateAbs(vGunRotate.x, vGunRotate.y, vGunRotate.z);

I mean I even wrote a function to mimic how I'm viewing my BSP scene just to get *some* kind of handle on the rifle's rotation/axis(obviously this is not used in conjunction with any other rotation function):
m_pMeshes[2].YawPitchRoll(
0, // Yaw around the y-axis, in radians
-D3DXToRadian(g_fSpinY*10), // Pitch around the x-axis, in radians
D3DXToRadian(g_fSpinX*10)); // Roll around the z-axis, in radians)

But to no avail. I move the mouse and the rifle spins out of control having a seizure.
Here is a screenshot of when I move the player via the mouse just a tad: https://drive.google.com/open?id=1o-YJ4YTeO5KqCAxkYtZ_T2hil4UynVdm
(I changed the FVF format for the texture coordinates so you could see better in this particular screenshot, irrelevant to the problem)

ANY help would be appreciated. And I thought this would be easy. I need you seasoned pro's on this one. Thanks!
Last edited on
To me, the screenshots hint that you're using the wrong coordinate basis for the rotation.
Topic archived. No new replies allowed.