How to implement First Person Shooter camera?

I’m trying to implement First Person Shooter camera.
FPS camera is something that stays around the forehead of the character model (and bone) (track the head’s position and move together), and it rotates as the head rotates.
Or I should say that the head follows the rotation of the FPS camera.
(In this case, the model keeps standing and only rotates its head to see the world around him).

I thought of using Universal Camera and a Bone that is embedded in the head mesh, and synchronize rotations of these two.
But I am not sure which properties of camera and bone should be linked.
And axis (local or world?) matter is also confusing.


I’m using a model (and texture) from this example.
This Multi View method would help you to compare TPS and FPS.

If you could help me I really appreciate it!
Thank you for reading.
miyabi

Just parent the camera to the head armature, or just add like a sway calculation and bounce from the movement.

Use a freecam and just treat it like any other node you need to position.

1 Like

Parenting looks cool, but is it possible to parent a camera to a bone?
I tried it, but it seems not working.
If you have nailed it, would you show me a code?

Thank you for your reply.
miyabi

You can attach a mesh to a bone and then parent the camera to that mesh

To attach a mesh to a bone: https://www.babylonjs-playground.com/#11BH6Z#443 (Line #45)

1 Like

But Ideally if its first person you would attack your mesh to your camera and play animations in response to your inputs.

https://www.babylonjs-playground.com/#11BH6Z#447
You will need to hide certain parts of your mesh to the camera though, like hide the head and move the camera forward from center slightly. You gotta consider what the person will actually “see” from first person.

So the main stuff you gotta worry about is the height of the model add some sort of simple physics impostor or bounding box/capsule setup. Then attach the camera to that. Then you will need to tailor your mesh and animations for FPS.

Any sort of sway can be a simple cos/sin addition to the cameras rotation specific axes scaled by the magnitude of some factor like the speed of the player.

2 Likes

dynamic fov control (simulator for eyes focus )

https://www.babylonjs-playground.com/frame#UGLGTJ#58

1 Like

Thanks for your reply, guys!
I’ve implemented looks-like-what-I-wanna-do FPS camera in this playground
Please check this out.

Wow this looks great!
This dynamic fov control must be very useful when you move around the world, but in my case, models just sit and only move their upper body to see the world around them, so I might not employ your great method to my app.
But thanks for showing me pretty good demo! :grinning:

What I wanna do looks like this playground.
Is there any better way to do this?

Thanks for your attention.

1 Like

It looks great I think you should go with it

Offset the local position of the camera forward so that the view does not feel like it is in the head of the model, or hide the head (don’t know if this is possible).

That is exactly what I am trying to solve.
I think, to properly make the camera stay exactly the same position relative to the head (maybe on the forehead of the model), the camera need to move inside the local space of the head (actually the neck bone).
So I want the camera to use the local space of neck bone as its world space.

You know the complicated thing is this,

  • I want the bone to follow the rotation of the camera

  • and I want the camera to follow the position of the bone (and also move some distance relative to the local space of the bone)

What why? That is not normally how you would do it but ok…

Why do you need the camera attached to the mesh and not vice versa?

Am I missing something?

I thought, it is said that I can control universal camera by mouse, so if the bone follow the motion of the camera, that would be good.

Maybe the best solution is

  • Parent the bone to the camera, and somehow implement some code to move the bone from mouse.

Not necessarily, in fact it might cause jittery jumpy movements, and you are gonna have to constantly update your up vector anyways on the camera.

Re read my post about the height and animations reliant on basic math.

A simple solution given what you would want to lerp the cameras position to the bones absolute position. But then you are gonna have to figure out a way to establish forward.

Thank you for your replys Pryme8!
I like the way how universal camera rotates as I keep clicking on the canvas and move the cursor around.

  • The distances that the cursor’s moved along x (and y) axis from the position of mouse-on event affects the camera’s rotation around y (and x)

Do I need to implement this distance-to-rotation conversion by myself (which I think I can but not the best at)?

So the solution would be

  1. Make the camera a child of the head mesh which is attached to the neck bone.
    So the parent->child relationships becomes [neck bone]->[head mesh]->[fps camera(not universal camera anymore)].

  2. Implement the cursor’s-moved-distance-to-rotation-around-x-and-y-axis conversion by myself, and use that calculated rotation to rotate the neck bone.

But why? The person in first person will never see it unless you are using a reflection probe and do a mirror or something, but even then I belive convention would be to render a different model to the reflection probe then what you are rendering to the first person camera.

1 Like

Remember we are only trying to simulate real life models not emulate them.

There is always a creative solution. The main consideration to have here is what is your final product trying to achieve, a realistic biometrics system or just a representation to fool the brain?