Positioning weapon just in front of Camera Problem...Unsolved and extended

Started by
14 comments, last by spencerholmes 18 years, 2 months ago
Hello. I am writing a game and i want the weapon to stick out of the camera like in classic FPS games. I have setup my camera using them following code. gluLookAt(mPos.x, mPos.y, mPos.z, mView.x, mView.y, mView.z, mUp.x, mUp.y, mUp.z); I am having problems assigning my weapon to the same position as the above because i am using a 3ds model. I can only position my 3ds model using the following code. glPushMatrix() glTranslatef(x, y, z); 3dsLoader.Render(); glPopMatrix(); As you can probably see now the glTranslatef function only takes in 3 parameters and is not setup as a vector like i have done in the gluLookAt() function. I want to know if anyone else has encountered this problem and in some way overcome it. I really don't know what to do and i am open to any guidance! // what i wanted to do was give the weapon the same make-up as the camera. I would then just update the pos.x, pos.y and pos.z accordingly to the camera so both camera and weapon would move at the same speed. I have everything else sorted out just this problem. S [Edited by - spencerholmes on February 13, 2006 8:06:09 PM]
Advertisement
Here is on way, not necessairly the best, but it works nicely.

If you reset your view matrix to identity, and set your world matrix to properly orient the gun you can render the gun without worrying about moving it to world space. Actually what your doing is putting the camera and weapon model in the same space.

You might also want to disable Z testing when drawing the gun, or you will have a situation where the gun might stick into walls. This does require an extra Z buffer clear every frame though.
Sounds good, Thankyou!

S
AFAIK that won't work. Disabling Z testing will screw up the rendering of your weapon model. Clearing the z buffer just for the gun should work though.
z-disabling will only misbehave if you use several models in the same object (i.e the handle is separate from the barrel etc), as long as it is one object it works just fine. :)



Also, make sure you reset the "camera" state before drawing the weapon...

glPushMatrix();
gluLookAt(...);
DrawWorld();
glPopMatrix();

glDisable(GL_DEPTH_TEST);
RenderPlayerGun();
glEnable(GL_DEPTH_TEST);


This way the gun is never rotated and will always be infront of the camera!

Hope it helps!
"Game Maker For Life, probably never professional thou." =)
You sure about that? The gun could draw farther polys last and overwrite nearer polys with z testing off, producing odd artifacts.
I want the weapon to move forward/rotate when the camera does. Is this the right approach?

What i was thinking was having something like this:

glPushMatrix();
glTranslatef(this->_pCamera->getX(), this->_pCamera->getY(), this->_pCamera->getZ());
Weapon.Render();
glPopMatrix();

then the weapon would always be in the same position as the camera. I would then increment/decrement the weapon accordingly to that of the camera and thus would make it look ok and run smooth.

What are your feelings about this?

S
Not sure if this will help, but I posted 1 or 2 topics about this recently:

http://www.gamedev.net/community/forums/topic.asp?topic_id=372273

Basically, I render my gun AFTER rendering all the other crap. And I reset the camera to basically the identity matrix so it doesn't matter if the camera is looking at the sky or ground, the FPS weapon will always be in the same place.
I understand what you are saying but as i said before the weapon is a 3ds file and the only way i can position it is using glTranslatef(x, y, z); i took a look at your link and i noticed you used 9 parameters when i can only use. If i were able to use 9 i think i would be able to sort it out.

I think i am going to post the code!

S
Quote:Original post by spencerholmes
I understand what you are saying but as i said before the weapon is a 3ds file and the only way i can position it is using glTranslatef(x, y, z); i took a look at your link and i noticed you used 9 parameters when i can only use. If i were able to use 9 i think i would be able to sort it out.

I think i am going to post the code!

S




Just because the TUTORIAL you saw used glTranslatef(), it doesn't mean you can't do other things.
Go with the resetting the matrix approach everyone's already suggested, its a good one.

This topic is closed to new replies.

Advertisement