Sign in to follow this  
BFK_GoNER

Camera + weapon mesh attachment

Recommended Posts

Good day to all, I'm having trouble rendering a weapon in my FPS project. I'm using this tutorial as a back-bone for my game: http://www.ultimategameprogramming.com/demoDownload.php?category=DirectX&page=3 (Timed Camera). Now.. I have a Camera class, which stores the player's x/y/z position, look-at coordinates and the up-vector. I managed to position the weapon in 3D space dynamically according to the camera position, but I have no idea how to ROTATE the weapon mesh! Would anyone be so kind to help me out?.. p.s. Is there any nice tutorial on using the yaw/pitch/roll model for 3D camera view?

Share this post


Link to post
Share on other sites

D3DXMATRIX World;
D3DXMatrixInverse(&World, NULL, &CameraView);

D3DXVECTOR3 WorldPos;
D3DXVECTOR3 WeaponOffset = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
D3DXVec3TransformCoord(&WorldPos, &WeaponOffset, &World);
D3DXVECTOR3 Targ = WorldPos + (LookDir * 1000.0f);

D3DXMatrixLookAtLH(&World, &WorldPos, &Targ, &(D3DXVECTOR3(0.0f, 1.0f, 0.0f)));
D3DXMatrixInverse(&World, NULL, &World);



That will give you the world transform for the weapon stored in "World". The "WeaponOffset" is the position of the weapon in relation to the camera.

Share this post


Link to post
Share on other sites
Ok, thanks! But where exactly am I supposed to paste this code to? In the SetCamera() I have:


void SetCamera () {

GetInput();

D3DXMATRIXA16 View;

D3DXVECTOR3 Eye(Camera.xPos, Camera.yPos, Camera.zPos);
D3DXVECTOR3 LookAt(Camera.xView, Camera.yView, Camera.zView);
D3DXVECTOR3 Up(Camera.xUp, Camera.yUp, Camera.zUp);

D3DXMatrixLookAtLH(&View, &Eye, &LookAt, &Up);

D3D_Device->SetTransform(D3DTS_VIEW, &View);

return; }

________

And yeah, I'm loading the meshes using the following code:


for(DWORD i = 0; i < numMaterials2; i++) // loop through each subset
{
D3D_Device->SetMaterial(&material2[i]); // set the material for the subset
meshWeapon->DrawSubset(i); // draw the subset
}



So which part of the code are you referring to?

Share this post


Link to post
Share on other sites
Simply set the view matrix to identity before drawing the HUD (and the weapon). So you dont have to worry about the camera movement.
Or use the inverse of the view matrix as the world matrix (wich also gives identity...)

Share this post


Link to post
Share on other sites
Excellent! :D

This is the result code:


//Set the View matrix:

D3DXMATRIXA16 View;

D3DXMatrixIdentity(&View); // Setting the view matrix to identity

D3D_Device->SetTransform(D3DTS_VIEW, &View);

// Drawing the weapon:

for(DWORD i = 0; i < numMaterials2; i++) // loop through each subset
{
D3D_Device->SetMaterial(&material2[i]); // set the material for the subset
meshWeapon->DrawSubset(i); // draw the subset
}

Thanks a lot for your help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this