Need help with 3D math for placing the origin of a bullet for a FPS character

Hi! Ok, so I’m coding a first-person shooter. I currently place the origin of any new bullet that’s fired directly in front of the character by using this:

bulletg.setLocalTranslation(cam.getLocation().add(cam.getDirection().mult(2)));

What I’d like to do is place the bullet origin a little off-centered to the right a little, as if being fired by a right-handed person. I have in my head how it’s probably done, but I can’t figure out if it’s a rotation based on the character as the origin, or just how to offset the position of the bullet relative to the character “cam”.

Any help on this is greatly appreciated!

Thank you!

worldVectorOffset = cam.GetRotation().mult(LocalVectorOffset)

@Empire Phoenix said: worldVectorOffset = cam.GetRotation().mult(LocalVectorOffset)

Thank you for your reply! I think I see where you’re going with this, but I still can’t seem to figure out how to apply it to my code.

I think what you might be saying to do is take the direction of the camera and get a position by calculating a rotation value based on that?

Once I’m able to position the starting point of the bullet as if it were from the tip of a gun positioned in the right hand of the player, I’m going to cast the bullet trajectory toward whatever object is collided with in the ray cast.

  1. if you do that, you’ll have “instant” bullet, which is not the case irl and in most games. Bullets are fast, but they aren’t “instant”
  2. If you keep in your idea of doing a ray test, keep in mind that you should use the “ray” from the physics engine. Why ? Because if you use the ray in the scenegraph your bullet will hit things like particles.
  3. A “big” problem when you have a camera and a gun and a bullet fired from the gun is that the bullet will never hit the “center” of the camera if its trajectory is // with the camera. I mean : you have 2 different point (the camera and the tip of the gun) and 2 line parallel. It’s the reason why in some fps people shoot “from their head”.
  4. remember that with you approach people will maybe be able to shoot through wall (as the tip of the gun will maybe be able to go through the wall)
@udoobu said: Thank you for your reply! I think I see where you're going with this, but I still can't seem to figure out how to apply it to my code.

I think what you might be saying to do is take the direction of the camera and get a position by calculating a rotation value based on that?

you can get the rotationf orm the camera, a quaerion

You make the offset to the gun in local space, (aka right in view is right ect)
useing quaternion mult on the vector will convert it to world space.

then you can use camera location + offset to gun converted to worldspace

Thanks a bunch! I’ll let you guys know what I came up with or have questions. :slight_smile: