Skip to main content
GameDev.net gamedev.net
🔒 Locked

Camera + weapon mesh attachment

Started by BFK_GoNER Mar 7, 2007 at 5:00 PM 4 replies 850+ views
Original Post
BFK_GoNER
BFK_GoNER
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?
smittix
smittix
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.
BFK_GoNER
BFK_GoNER
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); // set the material for the subset
meshWeapon->DrawSubset(i); // draw the subset
}



So which part of the code are you referring to?
BFK_GoNER
BFK_GoNER
Anybody?.. :(
Gagyi
Gagyi
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...)
-----------------------------------"After you finish the first 90% of a project, you have to finish the other 90%." - Michael Abrashstickman.hu <=my game (please tell me your opinion about it)
BFK_GoNER
BFK_GoNER
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); // set the material for the subset
meshWeapon->DrawSubset(i); // draw the subset
}

Thanks a lot for your help!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.