I'm trying to make a first person shooter and I'm having trouble limiting the movement of the camera and/or player
only on the xz plane. Being more specific, what I've done so far is create a player Node and attach a CameraNode
to it. Then I binded a NodeHandler with the player node. So the handler moves the player node and camera node
as expected. When the player looks up (either with the mouse or the keyboard) and then moves forward then the
player starts "taking off" and basically flies around the level. So I want to be able to have free mouse and keyboard look
without affecting the player's movement. I've read some of the posts relating to that problem but I can't figure out a solution.
I've also tried to use the jme-physics package but my models spin around for no good reason…
Are there any suggestions?
I'm a newbie myself but I think It's basically done like this:
playerNode.setLocalTranslation(playerNode.getLocalTranslation().add( cam.getDirection().xspeed, 0f , cam.getDirection().zspeed));
in update.
Are you suggesting to do that in the main update loop of the Game class and after the NodeHandler has been updated or
override the NodeHandler and use custom actions i.e. replace the update method of KeyNodeForwardAction with your implementation?
I've actually managed to keep the player just above by always setting manually the y coordinate to the current terrain's height
at the player's current location in the Game's update method such as the following.
float baseY = terrainNode.getHeight(playerNode.getLocation()) + 2;
if (playerNode.getLocation().y != baseY) {
playerNode.getLocation().y = baseY;
}
Although i think this is not a good solution, since I want the player to be able to jump as well...
I did it like this:
My main class extends SimpleGame and disables the FirstPersonHandler. I have made a class that implements MouseInputListener and gets the cam from the main class (which owns an instance of this class). The class has an update() method that gets called every simpleUpdate().
I control the direction of the camera in onMove(), being a MouseInputListener.
Then in update I have something not entirely unlike the line in my last post.
It's probably not the smartest way but it works.