Hi all.
I am looking for some sample code for a smooth FPS control which I can use in my library.
The current one I have is based of the examples, however what is the preferred way to attach weapons to the player node which will point in direction of aim.
If anyone has some examples or code I can use as reference, it would be much appreciated.
Well I do not specifically want to emulate a game, I want to create a customizable control which I can use to create any type of first person game. For now, something similar to Firewatch.
But with the added behavior of attaching items to the player node, like arms, hands, weapons, etc.
I already have a control which works very nice, however when I move there are some sort of glitchy movement.
I have a suspicion it is the way the camera node gets updated to the position of the physics body.
I donât think I have a single piece of code I can share, I donât use controls for this and my architecture is probably very different of what you have. But I can comment some decisions that I took to make my fps movement that may inspire you. My reference game is Quake.
In my game the jme loop and game logic are in separate threads synced by message passing.
User input is read in the main thread and passed to the simulation thread, then a future frame (the next one at best) will be updated with the correct camera position. So the view is one step behind the simulation, however:
Mouse input updates the camera rotation instantly without waiting the simulation to be up to date. This makes it snappy and doesnât feel sluggish, unlike movement which is conditioned by inertia and it doesnât need to be as responsive.
I copied the player movement math from the original quake source. Hereâs a good explanation.
The player physics object doesnât have gravity, it just floats, then I calculate the acceleration and friction myself. Otherwise it was bouncy and jerky, specially when moving fast.
Also not related to movement but the weapon model is rendered on a different view port, this way I can have a three meter long rocket launcher and donât having it clipped through walls.
Some games use a screen overlay and the objects representing the playerâs hands+gun are not even in the regular scene. Because itâs rendered over the top of the regular scene, it will never clip with walls⌠but it will also require its own lighting to be adjusted to where the player is standing.
Some games attach the arm/gun to the playerâs chin. If they are suitably polished, they will detect when the gun is clipped with the scene and move them out of the way to avoid ugly clipped.
Some games use a combination of those two approaches. Where the object is attached to the playerâs chin but still rendered as a completely separate layer.
Some games actually represent the playerâs whole body. They can look down and see their hands/legs/etc⌠in that case itâs possible to just attach the weapons like any other object attachment. Just put them in their hands. However, this means that they will usually NOT be visible in the bottom of the screen like the typical chin-attached arm+gun. (The typical FPS style âgun always in viewâ like itâs attached to their chin is not realistic at all⌠itâs just a visual trick to help give the player diegetic status and some aiming capability.)
Really none of these will share code so trying to make something that âworks for all FPSesâ is a BIG job.
And yes, you will probably struggle with when the camera position updates with respect to when the weapon updates⌠one of the side effects of treating scene objects like game objects is that itâs very hard to control what the âcurrent positionâ is because it will change at different parts of the frame. (Where as architectures where the games objects are separate from the JME scene objects means itâs possible for the âgame objectsâ not to change during scene rendering.)
Wow, so many tricks and solutions to this problem.
I never knew half of these, currently I am going for the see the full body of the player when looking down except the torso and head has been removed.
Something like this.