I have the following simpleUpdate:
pre type="java"
public void simpleUpdate(float tpf){
// Setup camera speed based on the TPF and the selected speed magnification
float magNormRate = tpfcamMagnifRate/camMagnifCeil;
Vector3f camDir =
cam.getDirection().clone().multLocal(camWalkSpeedmagNormRate);
…
walkDirection.set(0, 0, 0);
…
if(forward) walkDirection.addLocal(camDir.x, 0, camDir.z);
…
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
…
}/pre
The problem is that when the frame rate changes (e,g, when more lights are switched on or off), the camera speed changes disproportionally. I thought that TPF would have a stabilising effect. Also I seem to get smoother movements at lower frame rates. What is even more puzzling is that that if I change “tpf” in magNormRate to something like 0.01 then the camera movement is constant irrespectively of the frame rate. Any ideas what is wrong here? Can you rely on the TPF changing dynamically through the action?
the thing is, the less fps you have the more tpf is accurate.
When you have 3000 fps, tpf is so small that there may be some precision loss due to float precision.
To avoid that you can enable vSync that will set the fps to 60, also you can set the fps you want in the settings.
Indeed, VSync fixes the problem. However, my FPS were between 200 and 500, so I would not expect getting any floating point problems? Hmmm…
The problem has been resolved.
You need to be careful when you mix different kinds of behaviour (as was the case in my program). I had some behaviour which does not apply to characters (e.g. rotations of the world, objects flying through walls, etc.) and others which are applicable to various characters / players. The first type of behaviour needs to rely on the TPF to cushion the variations in the CPU and graphic adapter speeds. The latter however, can use setWalkDirection method, which is already automatically adjusted to the varying TPFs.
This means that in the above example, you’d need two different normalisation variables (here magNormRate) - one with the use of TPF (non-character behaviour) and another which uses a final constant appropriate for the type of movement, which can then be used with the setWalkDirection (character behaviour).