Viewing weapon – FPS

Hi! I’m writing a FPS game and I have a problem. I want to add weapon, but… I don’t know, how. I tried to add translating code into my update() function, but I could put my weapon onto the center of the screen only:

Code:
Vector3f camLoc = cam.getLocation().clone(); camLoc.x += 5f * cam.getDirection().x; camLoc.y += 5f * cam.getDirection().y; camLoc.z += 5f * cam.getDirection().z; weapon.setLocalTranslation(camLoc); weapon.setLocalRotation(cam.getRotation());
I want to move my weapon to the position, where the weapons in the FPS almost always are - in the lower right corner. How to do it?

EDIT:
Sorry! Never mind! I found a solution: http://hub.jmonkeyengine.org/groups/general-2/forum/topic/first-person-look-with-weapon/
1 Like

Hi! How did you end up rotating your gun with the camera rotation? Thank you!

@udoobu said: Hi! How did you end up rotating your gun with the camera rotation? Thank you!
Hey, I am currently developing an fps game, more so an engine right now. The way I positioned the gun was quite simple. I made a camera node which moved with the character controller. I had a vector3f location the was the "offset" for the gun. If you think about it, the gun is rotated exactly perpendicular to your body so the angle is even with the camera. All you do is attach the gun to the camnode and move it to the offset vector in the update. Good luck with your game!
@Jmonkier said: Hey, I am currently developing an fps game, more so an engine right now. The way I positioned the gun was quite simple. I made a camera node which moved with the character controller. I had a vector3f location the was the "offset" for the gun. If you think about it, the gun is rotated exactly perpendicular to your body so the angle is even with the camera. All you do is attach the gun to the camnode and move it to the offset vector in the update. Good luck with your game!

Hey! Thanks for the reply! I did end up figuring out my own method, which is similar, if not the same, as your advice. I’m new to all the vector math so I was really just trying to understand it without just taking some code from somewhere.

I basically envisioned a point in space where I wanted my gun and worked on the formula for getting it there. So, yeah I followed some of the JME math articles in the docs… I essentially added a certain angle to the direction of the camera, then brought it toward the cam and down on the Y-axis a bit and it worked great!

Have a good one!