before i ask this question i want you to keep in mind that i am asking it in general, and i also want a general answer :)

i am making a 3d first person shooter and i want to know how i would go about getting a gun infront of the camera, making it look like the player is holding the gun, like in other fps games, i dont want any code or recomendations to any engines or anything like that, just the theory of how to do it, i can figure out the rest.

Recommended Answers

All 22 Replies

>>i am making a 3d first person
what is that? I've heard of a 1st person shooter, but 3d???

Ancient Dragon: I think he's talking about a 3d game of the first person shooter game type.

tomtetlaw: If you have the gun rendering already, simply align the model with the direction of the camera, slightly offset the position of it and you should be away.

Best of luck!

Adam

Ancient Dragon: I think he's talking about a 3d game of the first person shooter game type.

tomtetlaw: If you have the gun rendering already, simply align the model with the direction of the camera, slightly offset the position of it and you should be away.

Best of luck!

Adam

i try that, but i can never get the gun to look right, the camera goes through the gun, the gun rotates to the other side of the camera, but what i am really asking is it just a matter of positioning/rotating it right?

First up, have you tried reducing the near plane distance to say 0.1 instead of 1.0, this should reduce clipping?

The alignment problem you're having needn't be one. Here's what you need to do for drawing the weapon:

1. Load the Identity matrix (back to basic transforms here).
2. Translate the weapon slightly into the scene (In opengl you'd translate in the direction of the negative-z axis, in directx the positive)

When you can see the gun in front of you...

3. Translate the weapon down ever so slightly (I'm guessing you're using y-up so -y, but only a small amount)

Finally...

4. Translate the weapon in the x-axis by a small amount depending on which hand you'd like the gun to be in.

Hope this helps.
Adam

what is near plane distance? and what is identity martix?

Ok, first of all you need to understand what the view frustum is. The view frustum is a volume of 3D space that will contain vertex data that will later be rendered to the screen.

Simply put, the near plane is the closest point that models can be 'seen', the far plane is the furthest point that models can be 'seen' by the camera.

This page should give you a basic idea what this is (check out the image!).

Next, as you're unsure of what a identity matrix actually is, I'm guessing you don't know about matrices.

The best example I can give without going into specifics is that the identity matrix it the transformation that is applied to the vertices that will apply no translation or rotation (i.e. no change).

If you intend to get into 3D graphics programming you're going to need to learn vector and matrix mathematics.

A book I recommend for this is: "3D math primer for graphics and game development" by Fletcher Dunn and Ian Parberry..

I'd also have a look for some 'beginning' books in whatever graphical API you're using.

Also, I'd check out a lot of the free tutorials you'd be able to find through your favourite search engine.

As one last thing, check out the beginnners forums at gamedev.net for games and graphical specific help.

As far as getting the hang of 3D graphics, it can be a lot of fun, but there will be a lot of work to go with it.

Best of luck,
Adam.

commented: Good info in these posts. +16

Also on a side note, alot of fps's hide the actual player model and then have seperate higher detailed models for the arm/gun specially for the first person view. This will obviously solve most clipping problems as well.

Also on a side note, alot of fps's hide the actual player model and then have seperate higher detailed models for the arm/gun specially for the first person view. This will obviously solve most clipping problems as well.

This will not be a solution to clipping issues, the near viewing plane reduction is the fix to the clipping issue. rendering different objects does nothing to what the camera can see

i did everything you said and it is almost fixed but i still have the problem with the gun rotating rong. heres my code so you can see althouhg i doubt it will help since its in DarkGDK but i will comment it.

#include "DarkGDK.h"

void DarkGDK ( void )
{
	dbSyncOn					( );
	dbSyncRate					( 60 );

	SetCurrentDirectory			( "media" );

	dbSetDisplayMode			( 1280, 768, 32    );

	dbSetCameraRange			( 0.1f, 30000.0f ); // here is the viewing frustrum set to 0.1

	dbLoadImage					( "texture.jpg", 1 );
	dbLoadImage					( "detail.jpg",  2 );
	dbSetupTerrain				( );
	dbMakeObjectTerrain			( 1 );
	dbSetTerrainHeightMap		( 1, "map.bmp" );
	dbSetTerrainScale			( 1, 3.0f, 0.6f, 3.0f );
	dbSetTerrainLight			( 1, 1.0f, -0.25f, 0.0f, 1.0f, 1.0f, 0.78f, 0.5f );
	dbSetTerrainTexture			( 1, 1, 2 );
	dbBuildTerrain				( 1 );
	
	dbPositionCamera			( 434, 42, -517 );
	dbLoadObject				( "skybox2.x", 2 );
	dbSetObjectLight			( 2, 0 );
	dbSetObjectTexture			( 2, 3, 2 );
	dbScaleObject				( 2, 5000, 5000, 5000 );

	dbLoadObject				( "MP5.x", 3 ); // load the gun
	dbMakeVector3				( 3 ); // i think this is the identity matrix.

	dbLoadMusic ( "ambiance.wav", 1 );

	register float fCameraAngleX = 0.0f;
	register float fCameraAngleY = 0.0f;

	/////////////////////////////////////////////////////
	//                    main loop                    //
	/////////////////////////////////////////////////////

	while ( LoopGDK ( ) )
	{
		dbControlCameraUsingArrowKeys ( 0, 2.0f, 0.3f );
		if ( dbLeftKey ( ) )
			dbMoveCameraLeft ( 0, 2.0f );
		if ( dbRightKey ( ) )
			dbMoveCameraRight ( 0, 2.0f );

		register float fHeight = dbGetTerrainGroundHeight ( 1, dbCameraPositionX ( ), dbCameraPositionZ ( ) );
		dbPositionCamera ( dbCameraPositionX ( ), fHeight + 10.0f, dbCameraPositionZ ( ) );

		fCameraAngleX = dbWrapValue ( fCameraAngleX + dbMouseMoveY ( ) * 0.4f );
		fCameraAngleY = dbWrapValue ( fCameraAngleY + dbMouseMoveX ( ) * 0.4f );

		dbXRotateCamera ( fCameraAngleX );
		dbYRotateCamera ( fCameraAngleY );
		dbPositionObject ( 3, dbCameraPositionX ( ) + 0.2f, dbCameraPositionY ( ) - 0.2f, dbCameraPositionZ ( ) ); // here is the spot that positions the gun.
		dbXRotateObject  ( 3, dbCameraAngleX ( ) ); // here is the spot that rotates the gun
		dbYRotateObject  ( 3, dbCameraAngleY ( ) ); // here to.

		dbSetObjectCull ( 3, false );

		dbLoopMusic ( 1 );

		dbSync ( );
	}

	return;
}

hope that helps!

I've not come across dark gdk before so I have done a quick search on google on your behalf.

The function dbMakeVector3 appears to do exactly what it says, it creates a 3D non-homogeneous vector.

To load the identity matrix for current items (and I'm guessing here, as I'm more of an OpenGL guy) you're going to have to set your world matrix to the identity.

For example (after a quick google for dark gdk again):

D3DXMatrix current;
D3DXLoadIdentity( &current );
dbSetObjectWorldMatrix( 1, &current );

(Based on link)

I'm making an assumption that that clears the camera transform also, if that's controlled by directx's view matrix you may need to load an identity in for that too?

Have a go with the first part and see how it goes from there.

Adam.

D3DXMatrix was actully D3DXMARTIX but D3DXLoadIdentity() doesnt get recongnised, it tells me it cannot find where the function is defined, but thanks for all your help so far!

Ok, so I see that D3DXMATRIX has the following constructor:

D3DXMATRIX( FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
            FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
            FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
            FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44 );

If you call this constructor with the following arguments:

D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 1.0f );

... you will create an identity matrix.

Regards,
Adam

what do i do with that martix?

Set that matrix as your view matrix before translating your gun in world space.

Adam.

for this code:

D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		0.0f, 0.0f, 1.0f, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f ) current;
D3DXLoadIdentity			( &current );
dbSetObjectWorldMatrix		( 1, &current );

i get this error:

c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(9) : error C2146: syntax error : missing ';' before identifier 'current'
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(9) : error C2065: 'current' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(13) : error C2065: 'current' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(13) : error C3861: 'D3DXLoadIdentity': identifier not found
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(14) : error C2065: 'current' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(42) : error C2065: 'current' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(42) : error C2146: syntax error : missing ';' before identifier 'D3DXMATRIX'
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(46) : error C2065: 'current' : undeclared identifier
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(46) : error C3861: 'D3DXLoadIdentity': identifier not found
c:\documents and settings\tom\my documents\visual studio 2008\projects\shooter\shooter\main.cpp(47) : error C2065: 'current' : undeclared identifier

D3DXMATRIX current = D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
                                 0.0f, 1.0f, 0.0f, 0.0f,
                                 0.0f, 0.0f, 1.0f, 0.0f,
                                 0.0f, 0.0f, 0.0f, 1.0f );

that still doesnt fix the problem??

D3DXMATRIX current = D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,
                                 0.0f, 1.0f, 0.0f, 0.0f,
                                 0.0f, 0.0f, 1.0f, 0.0f,
                                 0.0f, 0.0f, 0.0f, 1.0f );
	D3DXLoadIdentity			( &current );
	dbSetObjectWorldMatrix		( 1, &current );

and this: D3DXLoadIdentity ( &current ), acording to the VC++ 2008 compiler, doesnt exist??

Ok, first up, this is probably my mistake, again I'm mainly an OpenGL kind of guy.

The constructor for D3DXMATRIX creates an identity matrix!

D3DXLoadIdentity must have been a function someone has added as a utility function, forget this function call completely, you've already set the identity matrix for current, the D3DXLoadIdentity function call is no longer required.

D3DXMATRIX current = D3DXMATRIX( 1.0f, 0.0f, 0.0f, 0.0f,   
                                 0.0f, 1.0f, 0.0f, 0.0f,   
                                 0.0f, 0.0f, 1.0f, 0.0f,
                                 0.0f, 0.0f, 0.0f, 1.0f );   
  
dbSetObjectWorldMatrix ( 1, &current );

Adam.

thats what i did but it makes no difference, no offence

That statement is of no help to me, what errors are you still getting?

it doesnt give me any error, it just doesnt fix the rotation problem :P

it doesnt give me any error, it just doesnt fix the rotation problem :P

Did you set your view matrix to the identity also ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.