I’m creating a sidescroller, so this means the user can navigate their character by pressing the left or right key. I update the location of my camera in the simpleUpdate function as:
private Vector3f camLocationUpdate = new Vector3f();
[java]@Override
public void simpleUpdate(float tpf) {
// Get the player’s coordinates and use them to position the camera
camLocationUpdate.setX(player.getLocalTranslation().getX());
float newYCamPos = previousYCamPos + (player.getLocalTranslation().getY() - previousYCamPos) * Math.min(0.7f, Math.abs(player.getLocalTranslation().getY() - previousYCamPos));
camLocationUpdate.setY(newYCamPos + 1.2f);
camLocationUpdate.setZ(player.getLocalTranslation().getZ() + 7f);
previousYCamPos = newYCamPos;
cam.setLocation(camLocationUpdate);[/java]
For the y component some easing is added for when the character jumps. Though somehow when the character moves left or right there is some jitter as if the character is lagging. Which is not the case. As you can see in the code above the camera is assigned the same x component as the character.
The game runs at about 50 fps and if I don’t let the camera move with the character, the motion appears fluid. My question is now does anybody have an idea how to stop the jitter ?
simpleUpdate() is called before anything else. If your player is moved by a control or app state then that will happen after this.
In general, putting this kind of logic in simpleUpdate is a bad idea.
…and especially since you are following a specific node, a Control would be a better way to do this and then you could make sure it runs after anything else updating the player position.
The player has a control which is a class which extends BetterCharacterControl. What do you recommend I do with that ? Maybe some more concrete info would help.
Here is the order that things are called every frame:
simpleUpdate()
all appState.update() methods
all Spatials’ controls’ update methods
all appState.render() methods
all visible Geometry Controls’ render methods
Your best bet would be to add a control to the player after the BetterCharacterControl and update the camera in your control’s update method. Otherwise you will have jitter.
Edit: ninja’ed by OP… the best kind of ninja’ed to be.
@Ojtwist said:
Ok it seems fixed. I took your advice @pspeed and made sure it runs after anything else.
I wrote a control for the camera to follow a player when it is moving across X axis a time ago. Take a look and let know if it fix your requiriments. It is old code and I didn’t tests it properly, so it may have issues
@Override
public Control cloneForSpatial(final Spatial newSpatial) {
final SideCameraControl control = (SideCameraControl) super.cloneForSpatial(newSpatial);
control.setMaxDistanceX(this.maxDistanceX);
control.setStepX(this.stepX);
return control;
}
@Override
public void read(final JmeImporter im) throws IOException {
super.read(im);
final InputCapsule ic = im.getCapsule(this);
this.maxDistanceX = ic.readFloat(SideCameraControl.FIELD_NAME_MAX_DISTANCE_X,
SideCameraControl.DEFAULT_MAX_DISTANCE_X);
this.stepX = ic.readFloat(SideCameraControl.FIELD_NAME_STEP_X, SideCameraControl.DEFAULT_STEP_X);
}
Set stepXAxis and maxDistance so fix your needs. You just need to add this control to your player. You can change the code to adjust camera to movements across Y axis if you want.