FPS, gun in front of camera look

Started by
7 comments, last by wforl 16 years, 3 months ago
been trying to achieve this all night, and just can seem to to get it right, what method is usually used for this If i create my world, and multiply everything by my View*Proj matrix, this sets the world up correctly in relation to the camera. My idea was then to render the gun, but with out multiplying by the VIEW*proj matrix. This renders the gun in the correct place, but is at the cameras coordinates, i then want to move it move forwards slightly, but i just cant figure out how to do this. My other idea was to grab the cameras position coordinates, and create a matrix at that position, and multiply my gun by that matrix, which will work, but be oriented wrong, and its rotation matrix wont be right. hmm, bit lost here guys, and as you can guess, im fairly new to this
Advertisement
hi!

use the inverse of the view matrix. then it's easy to translate the gun anyway you want.

Below code is written from head so it may not work
D3DXMATRIX viewInv, deltaTrans;D3DXMatrixInverse(&viewInv, 0, &cam->getView());//place gun 3 units forward along z-axisD3DXMatrixTranslation(&deltaTrans, 0, 0, 3.0f);worldMatrix = deltaTrans * viewInv;


[Edited by - swecoder on January 24, 2008 1:10:52 PM]
_____________________Lecturer at Blekinge Institute of Technology
hmm, i think its almost there, but its still not working correctly, and i believe its due to the rotations, ie the gun rotates the wrong way when the player rotates a certain direction.

The below code uses the basic lookat function for the time being, once i get this right, i'll then try and implement it to a free movable camera

D3DXVECTOR3 position( cosf(angle) * 3.0f, height, sinf(angle) * 3.0f );		D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);		D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);		D3DXMATRIX V, T;		D3DXMatrixLookAtLH(&V, &position, &target, &up);		Device->SetTransform(D3DTS_VIEW, &V);		//		// Draw the scene:		//		Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);		Device->BeginScene();		Device->SetMaterial(&d3d::WHITE_MTRL);		Device->SetTexture(0, Tex);		D3DXMatrixTranslation(&T, 0, 0, 0);		Device->SetTransform(D3DTS_WORLD, &T);		Box->draw(0, 0, 0); //world objects		D3DXMATRIX viewInv, deltaTrans;		D3DXMatrixInverse(&viewInv, 0, &V);		//place gun 3 units forward along z-axis		D3DXMatrixTranslation(&deltaTrans, 0, 0, 3.0f);		viewInv *= deltaTrans;		Device->SetTransform(D3DTS_WORLD, &viewInv);		Box->draw(0, 0, 0);	//the gun		Device->EndScene();		Device->Present(0, 0, 0, 0);
hmm, does anyone know of maybe a tutorial?
What you'll want to do is keep your gun's position and orientation relative to the camera (AKA in view space). So if the gun's position is <0,0,5>, it will be drawn 5 units in front of the camera. Like you've already figured out, this means you don't want to multiply the gun vertices by a world or view matrix, you should instead just transform it by your projection matrix.

So to answer your question...if you want the gun to be positioned farther forward you should just increase its z-component.
sorry, i made a mistake in my original code. please try the new updated version.
_____________________Lecturer at Blekinge Institute of Technology
Sorry guys but im struggling with this, i managed to implement MJP's notes, but couldn't get the rotation to function correctly.

And swecoder, i implemented your code, and gun seemed to move miles away, do you have a solution file that i can maybe have a look at? ie somthing i can view and compile, to see where im going wrong

thanks for your help guys
Ok, if you just use the identity matrix, your gun will always be where the camera is: but the camera will kind of be inside the gun.

So translate the gun to the right, and forward by a little bit.

Then apply your projection matrix.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

argh, so confused .... :(

if anyone has any code i can look at, please post a link, im trying to implement your ideas, i really am, but im just confusing myself over and over.

This topic is closed to new replies.

Advertisement