Jump to content
  • Advertisement
Sign in to follow this  
GLForce

FPS Weapon

This topic is 4641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This could be a general subject, but im searching for a precise answer in relation with DirectX. I'm currently working on a game engine and now im on the first person wepon part. You know the arm with a gun who always follow you everywhere ? So, that was hard to make it follow correctly the camera, but i finally did it. Now that this work, my problem is that the weapon get trough things. I thought that wasn't a problem since with collision the gun should never get trough walls, but to have a good size at the screen the weapon has to be big, and so the collision box could be strange. So, is there a way to say to directx to always draw the gun over the rest (but not over gui of course), or another solution ? thanks !

Share this post


Link to post
Share on other sites
Advertisement
It is quite easy actually.

1. Draw the scene like you used to do.
2. Turn of the zbuffer in the DirectX device by setting the zbufferenabled value to false. This makes sure that the weapon doesn't use the zbuffer so it is always rendered "on top". Don't forget to enable the zbuffer again after this step.
3. Render the overlay.

Hope this works for you!

Share this post


Link to post
Share on other sites
I tought about that. The problem is that the gun is a mesh, composed of subsets wich render sepraratly. So by desactivating z buffering i screw all. Polygons going oevr each other and all the rest you can imagine.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i haven't used direct x before, but its available in opengl, so you'll have to look for the equivalent function. what you need to do is clear the z buffer.

basic steps
Draw entire scene excluding gun and hud.
clear z buffer
draw gun
draw hud

Share this post


Link to post
Share on other sites
I think my explanation was not enough clear. The problem with z-buffering is in the gun itself. Because, for example, the back part of the canon draw over the handle, ect..

Share this post


Link to post
Share on other sites
If you are using matrices and vectors the correct way, it should be easy to calculate the final world-space position of the gun. At that point, you can perform collision detection on it.

Since you are coding an FPS application, I assume that you are using a quaternion-based camera. Basically, this allows you to move in the direction that you are pointing.

Camera-Relevant Code:

// Update player position
playerPosition.yaw += playerPosition.yawVel;
playerPosition.pitch += playerPosition.pitchVel;

// Normalize and add
D3DXVECTOR3 cameraVel;
D3DXVec3TransformNormal( &cameraVel, &playerPosition.vel, &matOrientation );
playerPosition.pos += cameraVel;

// Use quaternions to rotate the camera and avoid gimble lock
D3DXQUATERNION quat;
D3DXQuaternionRotationYawPitchRoll( &quat, playerPosition.yaw, playerPosition.pitch, playerPosition.roll );
D3DXMatrixAffineTransformation( &matOrientation, 1.25f, NULL, &quat, &playerPosition.pos );
D3DXMatrixInverse( &matView, NULL, &matOrientation );


The following code will offset the player's position to obtain the gun's position:

/* BUILD THE GUN MATRIX */
D3DXVECTOR3 meshModify = D3DXVECTOR3( 3.0f, -1.5f, 10.5f ); // Offset from camera position
D3DXVec3TransformNormal( &meshModify, &meshModify, &matOrientation ); // Normalize
D3DXVECTOR3 meshPosition = playerPosition.pos + meshModify; // Add to player's position

// Build translation matrix
D3DXMatrixTranslation( &matMeshWorld, meshPosition.x, meshPosition.y, meshPosition.z );

// Build rotation matrix
D3DXMATRIX rotation;
D3DXMatrixRotationYawPitchRoll( &rotation, playerPosition.yaw, playerPosition.pitch, playerPosition.roll );

// Combine rotation and transformation
matMeshWorld = rotation * matMeshWorld;


You now have the final world-space position in meshPosition, and the final world-space matrix in matMeshWorld.

Share this post


Link to post
Share on other sites
Draw the scene
CLEAR the z-buffer but don't turn ZBUFFER off! and dont turn zwite-enabe off!
To draw the weapon succesfully, translate it farer (not so close) from the camera! (sorry, my english is...)
I mean your problem is:
near clip-plane here:|
|
WEAPON MESH WEAPON MESH WEAPON MESH
|
|
THIS SECTION <-- before the NearCplane. will to be drawn.

So, translate it, into the current position.
(It's able, that you have to set som things in the MatrixPerspFOV before the Weapon drawing.)

I hope it was helpful.

Share this post


Link to post
Share on other sites
I've been told before by a few graphics guys that they don't want to do the clear zbuffer then draw gun because it stalls the pipeline. Is this true? and if so does it incur a significant performance penalty? Also that it apparently doesn't work when using some types of shadowing.

Share this post


Link to post
Share on other sites
Why don't you look at how Quake 3 does it (Quake 3 was open sourced after all). It may not use DX but who cares? The concepts are still there. If you still insist on seeing DX code then check out DXQuake3. :)


-SirKnight

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!