Camera movement is not the same speed at different frame rates

   @Override
    public void update(float tpf) {

    if((cam.getWidth()*0.05)>inputManager.getCursorPosition().getX()&&(cam.getHeight()*0.05)>inputManager.getCursorPosition().getY()){
        MoveCam(LowerLeft);//左下
        MoveCamTime = 0;
    }else if((cam.getWidth()-(cam.getWidth()*0.05))<inputManager.getCursorPosition().getX()&&(cam.getHeight()*0.05)>inputManager.getCursorPosition().getY()){
        MoveCam(LowerRight);//右下
        MoveCamTime = 0;
    }else if((cam.getWidth()*0.05)>inputManager.getCursorPosition().getX()&&(cam.getHeight()-(cam.getHeight()*0.05))<inputManager.getCursorPosition().getY()){
        MoveCam(UpperLeft);//左上
        MoveCamTime = 0;
    }else if((cam.getWidth()-(cam.getWidth()*0.05))<inputManager.getCursorPosition().getX()&&(cam.getHeight()-(cam.getHeight()*0.05))<inputManager.getCursorPosition().getY()){
        MoveCam(UpperRight);//右上
        MoveCamTime = 0;
    }else if((cam.getWidth()*0.05)>inputManager.getCursorPosition().getX()){
        MoveCam(Left);//左
        MoveCamTime = 0;
    }else if((cam.getWidth()-(cam.getWidth()*0.05))<inputManager.getCursorPosition().getX()){
        MoveCam(Right);//右
        MoveCamTime = 0;
    }else if((cam.getHeight()*0.05)>inputManager.getCursorPosition().getY()){
        MoveCam(Backward);//后
        MoveCamTime = 0;
    }else if((cam.getHeight()-(cam.getHeight()*0.05))<inputManager.getCursorPosition().getY()){
        MoveCam(Up);//前
        MoveCamTime = 0;
    }
}

   private void MoveCam(String direction){
   switch(direction){

   case("Left"):
       cam.setLocation(camVector3f.addLocal(new Vector3f(1,0,0)));
    break;
   case("Right"):
       cam.setLocation(camVector3f.addLocal(new Vector3f(-1,0,0)));
    break;
   case("Up"):
        cam.setLocation(camVector3f.addLocal(new Vector3f(0,0,1)));
    break;
   case("Backward"):
        cam.setLocation(camVector3f.addLocal(new Vector3f(0,0,-1)));
    break;
   case("UpperLeft"):
        cam.setLocation(camVector3f.addLocal(new Vector3f(1,0,0)));
        cam.setLocation(camVector3f.addLocal(new Vector3f(0,0,1)));
    break;
   case("UpperRight"):
        cam.setLocation(camVector3f.addLocal(new Vector3f(-1,0,0)));
        cam.setLocation(camVector3f.addLocal(new Vector3f(0,0,1)));
    break;
   case("LowerLeft"):
        cam.setLocation(camVector3f.addLocal(new Vector3f(1,0,0)));
        cam.setLocation(camVector3f.addLocal(new Vector3f(0,0,-1)));
    break;
   case("LowerRight"):
        cam.setLocation(camVector3f.addLocal(new Vector3f(-1,0,0)));
        cam.setLocation(camVector3f.addLocal(new Vector3f(0,0,-1)));
    break;
   }
   
   }

This is the part of the code where I control the movement of the camera
I found that the camera moves at different speeds at different frame rates which puzzles me!

I’m hoping someone can help me see what the problem is.
Let me know if I need to provide more content.

Acknowledgements in advance

this is expected, pass tpf from public void update(float tpf) to MoveCam and multiply the Vector3f representing the direction by it eg.

       cam.setLocation(camVector3f.addLocal(new Vector3f(1,0,0).multLocal(tpf) ));

Tpf is the time since the last frame and will make your movement framerate independent

4 Likes

Thanks for the quick reply and the tip I’m going to try it later

1 Like