FPS Weapon

Started by
12 comments, last by superpig 18 years, 3 months ago
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 !
"Do, or do not. There is no try." - Yoda
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!
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.
"Do, or do not. There is no try." - Yoda
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
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..
"Do, or do not. There is no try." - Yoda
and you are sure you have enabled z buffer reads/writes?
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)
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 positionplayerPosition.yaw	+= playerPosition.yawVel;playerPosition.pitch	+= playerPosition.pitchVel;// Normalize and addD3DXVECTOR3 cameraVel;D3DXVec3TransformNormal( &cameraVel, &playerPosition.vel, &matOrientation );playerPosition.pos += cameraVel;// Use quaternions to rotate the camera and avoid gimble lockD3DXQUATERNION 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 positionD3DXVec3TransformNormal( &meshModify, &meshModify, &matOrientation );	// NormalizeD3DXVECTOR3 meshPosition = playerPosition.pos + meshModify;			// Add to player's position// Build translation matrixD3DXMatrixTranslation( &matMeshWorld, meshPosition.x, meshPosition.y, meshPosition.z );// Build rotation matrixD3DXMATRIX rotation;D3DXMatrixRotationYawPitchRoll( &rotation, playerPosition.yaw, playerPosition.pitch, playerPosition.roll );// Combine rotation and transformationmatMeshWorld = rotation * matMeshWorld;


You now have the final world-space position in meshPosition, and the final world-space matrix in matMeshWorld.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
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.
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.
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

This topic is closed to new replies.

Advertisement