Problem with FixedLogicRateGame

first … hello



i found out about jME some time ago and was playing around with terrain stuff and things ‘in’ SimpleGame … now i tried to move to FixedLogicGame but came around a strange error (at least it is strange for me … maybe its just i overlooked something )



ok … this is the beginning of my main class:


public class Main2 extends FixedLogicrateGame{
 
  public static void main( String args[] ){
    Main2 app = new Main2();
    app.setLogicTicksPerSecond( 5 );
    app.start();
  }
 ....



now if i try to start it i get a NullPointerException while calling app.setLogicTicksPerSecond():

java.lang.NullPointerException

   at com.jme.app.FixedLogicrateGame.setLogicTicksPerSecond(Unknown Source)



i tried several approaches how to fix it but i came to no solution :(
so i hope someone around here can help me

(sry if this topic was around here sometime ago ... but i haven't found something via search)

thanks Florian aka Goliat

Ive tried calling setLogicTicksPerSecond AFTER the display has been created in the initSystem() method. That worked for me last time I checked (which was a while ago).



DP

hmm… let me check that …

but if i remember right the javadoc says the function shouldn’t be called after .start()



edit: doesn’t work for me either :frowning:

well, ive looked at the docs. And the docs say:



This should not be called prior
* to the application being <code>start()</code> -ed.


which means it shouldn't be called BEFORE (prior is before) start() has been called.

Il look at the code now...

DP


public class TestLogicrateGame extends FixedLogicrateGame {
   
   private Camera cam;
   private InputHandler input;
   
   private Node rootNode;
   
   protected void update(float interpolation) {
   }
   
   protected void render(float percentWithinTick) {
      display.getRenderer().clearBuffers();
      display.getRenderer().draw(rootNode);
   }
   
   protected void initSystem() {
      try {
         display = DisplaySystem.getDisplaySystem(properties.getRenderer());
         display.createWindow(properties.getWidth(), properties.getHeight(), properties.getDepth(), properties.getFreq(), properties.getFullscreen());
         
         cam = display.getRenderer().getCamera(properties.getWidth(), properties.getHeight());
         
         setLogicTicksPerSecond(5);
      }catch (JmeException jmee) {
         jmee.printStackTrace();
         System.exit(1);
      }
      
      Vector3f loc = new Vector3f(0, 0, 0);
      Vector3f left = new Vector3f(-1, 0, 0);
      Vector3f up = new Vector3f(0, 1, 0);
      Vector3f dir = new Vector3f(0, 0, -1);
      
      cam.setFrustum(1f, 1000f, -0.55f, 0.55f, 0.4125f, -0.4125f);
      cam.setFrame(loc, left, up, dir);
      
      display.getRenderer().setCamera(cam);
      
      input = new FirstPersonHandler(this, cam, properties.getRenderer());
      
   }
   
   protected void initGame() {
   }
   
   protected void reinit() {
   }
   
   protected void cleanup() {
   }

   public static void main(String[] args) {
      TestLogicrateGame app = new TestLogicrateGame();
      app.start();
   }
}



works like a charm for me.

DP

To be perfectly honest with you. I have never used FixedLogicrateGame because:



it doesn’t allow me to update my input in the update(…) method.



it doesn’t allow me to update my rootNode from the update(…) method



it doesn’t allow me to update my AI routines in the update(…) method



My all time favourite is VariableTimeStep game. Which is basically BaseGame with a timer. So the float value in the update() method is infact the time it took for this method to be called again (interpolation time).



Again, alot of controllers need the time, e.g. KeyNodeForwardAction in FirstPersonHandler(…) need to know about the time. So you would need to rewrite some things that deal with time again in order to remove the time element.



I love the idea tho. It would be just like old times with the posX += 5; instead of posX += 5 * time; :slight_smile:



DP

it works … even if i put the setLogicTicksPerSecond() call where i had it (direct under .start() in the main method) … but i don’t see any difference expect you added some code to the methods … (which i have done too in an extending class)



btw … am i wrong or does MidPointHeightMap only work with N^2 sizes but TerrainPage just uses N^2+1 ?

I haven’t touched terrain for…4…maybe 5 months now. But I believe that was the case back then.



I mean you could always obtain the data from the MidHeightPoint thingamabob, System.copyArray onto another bigger array, and set the last value to 0 or something that suits. But thats a hack IMO.



Perhaps MidHeightPoint isn’t supposed to work with Pages? I duno…



Im not the best person to be speaking to about this matter tho. Mojo would be your man as he made the terrain system.



DP

"DarkProphet" wrote:
So you would need to rewrite some things that deal with time again in order to remove the time element.
Not at all! As the logic rate is guaranteed, you can be certain of the amount of time elapsed between subsequent calls to update(float). You can then simply pass this constant time elapsed value to the controllers and use your own code based on a fixed timestep.

awsome, but how would I be able to obtain that time since the interpolation is always -1?



it is just (1/TicksPerSecond)?



DP

uh … let me just quick thank you before i retreat to my low mathematical problems and leave the bigger stuff to you guys :wink:

tell us your maths probs, id love to hear em.



DP

"DarkProphet" wrote:
Is it just (1/TicksPerSecond)?
Precisely: 1 / (30 ticks/sec) = (1/30) sec/tick.

na … just have no idea how to use matrixes (correct english plural?) … but am using quaternions now … much easier :wink:



but i just tried to use your code from above … which runs fine as long as i dont alter it …



now i tried to load a terrain (with my loader):


   protected void initGame() {
    try {
      rootNode.attachChild(TerrainManager.loadMap("test", display));
    }
    catch (JDOMException ex) {
    }
    catch (IOException ex) {
    }
   }



and it crashes again (with the same stacktrace as before)
the loadMap() method works fine ... at least while using SimpleGame[/code]

its matrices :slight_smile:



Where are you placing the terrain loading? As long as nothing is before initing the display in the try/catch block, then your fine.



@eric. Perhaps you could set the interpolation of the update method to 1/TicksPerSecond instead of me having to have a variable set for the number of ticks and doing it manually.



Oh yeah, could you perhaps explain the interpolation value of the rendering method please? I dont seem to understand it very well… :?



DP

as you can see from the code above i placed it in the initGame() method … which should be called directly after the initSystem() method and therefore after .start() and the display init

ah yes, sorry.



But if thats the full code for the initGame, where are you initialising the rootNode?



where are you saying :



private Node rootNode;

protected void initGame() {
  rootNode = new Node("Root Node");
  // do whatever here, e.g. loading the terrain
}



?

The FixedLogicrateGame and all other games are much different from SimpleGame because they dont do things for you.

Please have a look at the code for SimpleGame to understand where rootNode, lightState and similar variables are coming from.

DP

as i said i took your code from above …

if nobody minds ill just post the whole thingy again:



package krieger;

import com.jme.app.*;
import com.jme.renderer.*;
import com.jme.input.*;
import com.jme.scene.*;
import com.jme.system.*;
import com.jme.math.*;
import krieger.terrain.*;
import org.jdom.*;
import java.io.*;

public class Main2 extends FixedLogicrateGame {

   private Camera cam;
   private InputHandler input;

   private Node rootNode;

   protected void update(float interpolation) {
   }

   protected void render(float percentWithinTick) {
      display.getRenderer().clearBuffers();
      display.getRenderer().draw(rootNode);
   }

   protected void initSystem() {
      try {
         display = DisplaySystem.getDisplaySystem(properties.getRenderer());
         display.createWindow(properties.getWidth(), properties.getHeight(), properties.getDepth(), properties.getFreq(), properties.getFullscreen());

         cam = display.getRenderer().getCamera(properties.getWidth(), properties.getHeight());

      // this command crashes jME
      setLogicTicksPerSecond(5);
      }catch (JmeException jmee) {
         jmee.printStackTrace();
         System.exit(1);
      }

      Vector3f loc = new Vector3f(0, 0, 0);
      Vector3f left = new Vector3f(-1, 0, 0);
      Vector3f up = new Vector3f(0, 1, 0);
      Vector3f dir = new Vector3f(0, 0, -1);

      cam.setFrustum(1f, 1000f, -0.55f, 0.55f, 0.4125f, -0.4125f);
      cam.setFrame(loc, left, up, dir);

      display.getRenderer().setCamera(cam);

      input = new FirstPersonHandler(this, cam, properties.getRenderer());

   }

   protected void initGame() {
      // my added code, without this FixedLogicRateGame runs fine, but this works in SimpleGame
    try {
      rootNode.attachChild(TerrainManager.loadMap("test", display));
    }
    catch (JDOMException ex) {
    }
    catch (IOException ex) {
    }
   }

   protected void reinit() {
   }

   protected void cleanup() {
   }

   public static void main(String[] args) {
      Main2 app = new Main2();
      app.setDialogBehaviour( ALWAYS_SHOW_PROPS_DIALOG );
      app.start();


   }
}

as I said before, you should do:



rootNode = new Node(“root scene Node”);



in your init game before you attach anything to it.



because simpleGame already does that for you. in FixedLogicrate game and all other games, that isn’t done for you.



DP



so try this code:



package krieger;

import com.jme.app.*;
import com.jme.renderer.*;
import com.jme.input.*;
import com.jme.scene.*;
import com.jme.system.*;
import com.jme.math.*;
import krieger.terrain.*;
import org.jdom.*;
import java.io.*;

public class Main2 extends FixedLogicrateGame {

   private Camera cam;
   private InputHandler input;

   private Node rootNode;

   protected void update(float interpolation) {
   }

   protected void render(float percentWithinTick) {
      display.getRenderer().clearBuffers();
      display.getRenderer().draw(rootNode);
   }

   protected void initSystem() {
      try {
         display = DisplaySystem.getDisplaySystem(properties.getRenderer());
         display.createWindow(properties.getWidth(), properties.getHeight(), properties.getDepth(), properties.getFreq(), properties.getFullscreen());

         cam = display.getRenderer().getCamera(properties.getWidth(), properties.getHeight());

      // this command crashes jME
      setLogicTicksPerSecond(5);
      }catch (JmeException jmee) {
         jmee.printStackTrace();
         System.exit(1);
      }

      Vector3f loc = new Vector3f(0, 0, 0);
      Vector3f left = new Vector3f(-1, 0, 0);
      Vector3f up = new Vector3f(0, 1, 0);
      Vector3f dir = new Vector3f(0, 0, -1);

      cam.setFrustum(1f, 1000f, -0.55f, 0.55f, 0.4125f, -0.4125f);
      cam.setFrame(loc, left, up, dir);

      display.getRenderer().setCamera(cam);

      input = new FirstPersonHandler(this, cam, properties.getRenderer());

   }

   protected void initGame() {
     
      // crutial part below
      rootNode = new Node("Root Scene Node");


      // my added code, without this FixedLogicRateGame runs fine, but this works in SimpleGame
    try {
      rootNode.attachChild(TerrainManager.loadMap("test", display));
    }
    catch (JDOMException ex) {
    }
    catch (IOException ex) {
    }
   }

   protected void reinit() {
   }

   protected void cleanup() {
   }

   public static void main(String[] args) {
      Main2 app = new Main2();
      app.setDialogBehaviour( ALWAYS_SHOW_PROPS_DIALOG );
      app.start();


   }
}

hmm… sorry for bothering you … guess you have better things todo then to help a newb like me :wink:



after adding the node (and some other stuff, a timer (which wasn’t yet the best idea)) it works without crashing … but the result is more or less not what it is suppost to be … i’ll just post a screenshot maybe you (or someone else) can tell me what i did wrong …







are there any states missing? (expect the lightstate) …

the white thing you see there is suppost to be my terrain … just somehow disturbed :([/img]