Camera speed?

hello everyone…sry to ask this dumbo question…



i have noticed one thing that in some application my camera move very fast and in some my camera move very slow…

for eg when i draw some primitive shapes like box and then when i press "W", my camera moves fast forward…

but when i render some kinda complex terrain then my camera move very slow on pressing "S" or "W" means all navigation keys…



just i wanted to know that y its happening…Is there any way to control the speed of my camera or whatever u say according to above scenario…



THANKs (in ADVANCE)…



again sry for dumbo questions…

What you want to achieve is 'frame independent movement'.



Your camera probably moves x units per frame.

Now if you render a simple box and with 500 Frames per Second your Cam moves faster than, if you render a Terrain and you only get 50 FPS.



You need to move the Camera (and all other things in the scene) a fixed amount per time not per frame.

Either limit the framerate to always be 30 or something or even better, multiply the speed with the time per frame.

thanks for the advice…

but could u elaborate a bit with small code snippet…i mean how to retrieve frame rate and manipulate it as u said to limit the frame rate to some better constant…

He didn't say you have to manipulate the framrate but what you have to do is to know how many milliseconds ellapsed since the last camera move update and deduce by how much you have to update the camera position.



It's very easy, if your camera has a speed of 60miles per millisecond (mpms) and the last update was made 10ms ago then the delta-distance is "60mpms x 10ms" = 600miles.

In the following Test are two boxes the small box will move with (more or less) constant speed, while the big one moves dependent on the framerate.

If you press SPACE the framerate will be slowed down.



public class TestFramerateIndependant extends SimpleGame {
   private Box a;
   private float aSpeed = 0.1f;
   
   private Box b;
   private float bSpeed = 5f;
   
   @Override
   protected void simpleInitGame() {
      // Big Box Framerate dependant
      a = new Box("a", new Vector3f(), 1, 1, 1);
      a.setModelBound(new BoundingBox());
      a.updateModelBound();
      
      // small Box Framrate independant
      b = new Box("a", new Vector3f(), 0.3f, 0.3f, 0.3f);
      b.setModelBound(new BoundingBox());
      b.updateModelBound();
      b.getLocalTranslation().z +=5;
      
      rootNode.attachChild(a);
      rootNode.attachChild(b);

      // set key to slow down framerate
      KeyBindingManager.getKeyBindingManager().add("slow", KeyInput.KEY_SPACE);
      
      // set cam position
      cam.setLocation(new Vector3f(0, 15, 10));
      cam.lookAt(b.getLocalTranslation(), Vector3f.UNIT_Y);
   }

   @Override
   protected void simpleUpdate() {
      super.simpleUpdate();
      if (Math.abs(a.getLocalTranslation().x) > 7)
         aSpeed *=-1;
      if (Math.abs(b.getLocalTranslation().x) > 7)
         bSpeed *=-1;
      
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("slow")) {
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
      
      // Framerate dependant
      // always move the object the same amount per frame
      a.getLocalTranslation().x += aSpeed;
      
      // Framerate independant
      // multiply speed with the time since last frame
      b.getLocalTranslation().x += bSpeed * Timer.getTimer().getTimePerFrame();
   }
   
   public static void main(String[] args) {
      TestFramerateIndependant test = new TestFramerateIndependant();
      test.setConfigShowMode(ConfigShowMode.AlwaysShow);
      test.start();
   }
}

thanks for the snippet…



so then…can i control my camera i.e framerate independent by this code ???..

just an example…suppose i put this code for Key "D"…when D is pressed this code will run…

cam.getLocation().x += bSpeed * Timer.getTimer().getTimePerFrame(); for different axis(X,Y,Z)…

well yes pretty much, there are always 1001 ways to do things.



If you use one of jme's input hadling classes like First/ThirdPersonController or ChaseCamera it should already be Frame rate independant.

thanks core …for ur help…

nw i m moving 2wards first/thirdpersoncontroller and chase camera as u said…

i would say one thing…JME—> I M LOVING IT…  :slight_smile:

Make sure to try out all the tests (run TestChosser.java) and look at the source of them in the jmetest package.

Thats a good way to learn all the different jme features.