How to use FirstPersonHandler

Hi All!



I search for a good tutorial about FirstPersonHandler.

I want to use it in my game, but i don't know how.

Here is my code so far…


package FlightSimulator;
import com.jme.app.*;
import com.jme.renderer.*;
import com.jme.util.*;
import com.jme.system.*;
import com.jme.math.*;
import com.jme.input.*;
import com.jme.scene.shape.*;
import com.jme.scene.*;
import com.jme.bounding.*;
import com.jme.scene.state.*;
import com.jme.image.*;
import com.jmex.terrain.TerrainBlock;
import com.jmex.terrain.util.*;
import javax.swing.ImageIcon;

import jmetest.flagrushtut.lesson5.FlagRushHandler;

import com.jme.light.*;

public class Main extends BaseGame {
      //display attributes for the window. We will keep these values
      //to allow the user to change them
      //Dit is een veld voor de grootte van de basis
      private int width;
      //Voor de hoogte
      private int height;
      //Voor de diepte
      private int depth;
      //Weet ik niet...
      private int freq;
      //Hierin zit of de game wordt getoond in fullscreen of niet
      private boolean fullscreen;
     
      //Our camera object for viewing the scene
      private Camera cam;
     
      //Dit veld wordt gebruikt om time per frame te berekenen
      protected Timer timer;
     
      //??
      private Node scene;
     
       //TextureState to show the monkey on the sphere.
      private TextureState ts;
      
      //Het terrein
      private TerrainBlock tb;
      
      protected InputHandler input;


   public Main(){
      
   }
   
   /**
   * build the height map and terrain block.
   */
   private void buildTerrain() {
      // Generate a random terrain data
      MidPointHeightMap heightMap = new MidPointHeightMap(64, 1f);
      // Scale the data
      Vector3f terrainScale = new Vector3f(4, 0.0575f, 4);
      // create a terrainblock
      tb = new TerrainBlock("Terrain", heightMap.getSize(), terrainScale,
                  heightMap.getHeightMap(), new Vector3f(0, 0, 0), false);
      
      tb.setModelBound(new BoundingBox());
      tb.updateModelBound();
     
      // generate a terrain texture with 3 textures
      ProceduralTextureGenerator pt = new ProceduralTextureGenerator(heightMap);
      pt.addTexture(new ImageIcon(Main.class.getClassLoader()
                  .getResource("jmetest/data/texture/grassb.png")), -128, 0, 128);
      pt.addTexture(new ImageIcon(Main.class.getClassLoader()
                  .getResource("jmetest/data/texture/dirt.jpg")), 0, 128, 255);
      pt.addTexture(new ImageIcon(Main.class.getClassLoader()
                  .getResource("jmetest/data/texture/highest.jpg")), 128, 255,
                  384);
      pt.createTexture(32);
     
      // assign the texture to the terrain
      TextureState ts = display.getRenderer().createTextureState();
      Texture t1 = TextureManager.loadTexture(pt.getImageIcon().getImage(),
                  Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR, true);
      ts.setTexture(t1, 0);
      tb.setRenderState(ts);
   }
   
   private void buildLighting() {
      /** Set up a basic, default light. */
       DirectionalLight light = new DirectionalLight();
       light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
       light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
       light.setDirection(new Vector3f(1,-1,0));
       light.setEnabled(true);
 
         /** Attach the light to a lightState and the lightState to rootNode. */
       LightState lightState = display.getRenderer().createLightState();
       lightState.setEnabled(true);
       lightState.attach(light);
       scene.setRenderState(lightState);
   }
   
   protected void update(float interpolation) {
      //update the time to get the framerate
      timer.update();
      interpolation = timer.getTimePerFrame();
      //if escape was pressed, we exit
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit")) {
         finished = true;
      }
     
      cam.update();

   }
 
   protected void render(float interpolation) {
      //Clear the screen
      display.getRenderer().clearBuffers();
      
      display.getRenderer().draw(scene);

   }
 
   protected void initSystem() {
      
      
      //store the properties information
      width = properties.getWidth();
      height = properties.getHeight();
      depth = properties.getDepth();
      freq = properties.getFreq();
      fullscreen = properties.getFullscreen();
      
      try{
         display = DisplaySystem.getDisplaySystem(properties.getRenderer());
         display.createWindow(width, height, depth, freq, fullscreen);         
         cam = display.getRenderer().createCamera(width, height);
      }
      catch(JmeException e){
         e.printStackTrace();
         System.exit(1);
      }
         
      //set the background to black
      display.getRenderer().setBackgroundColor(ColorRGBA.black);
      
      //initialize the camera
      cam.setFrustumPerspective(45.0f, (float)width / (float)height, 1, 1000);
      Vector3f loc = new Vector3f(250.0f, 100.0f, 250.0f);
      Vector3f left = new Vector3f(-0.5f, 0.0f, 0.5f);
      Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
      Vector3f dir = new Vector3f(-0.5f, 0.0f, -0.5f);
      // Move our camera to a correct place and orientation.
      cam.setFrame(loc, left, up, dir);
      /** Signal that we've changed our camera's location/frustum. */
      cam.update();
      display.getRenderer().setCamera(cam);
      
      /** Get a high resolution timer for FPS updates. */
      timer = Timer.getTimer();
      
      //Zorgen dat de gebruiker de game kan afsluiten door op de ESCAPE knop te duwen
      KeyBindingManager.getKeyBindingManager().set("exit",KeyInput.KEY_ESCAPE);
   }
 
   protected void initGame() {
      scene = new Node("Scene graph node");
      buildTerrain();
      scene.attachChild(tb);
      
      //Licht maken
      buildLighting();
      
      
      FirstPersonHandler fpHandler = new FirstPersonHandler(cam, 50, 1 );
        input = fpHandler;
        fpHandler.getKeyboardLookHandler().setEnabled( true );
        fpHandler.getMouseLookHandler().setEnabled( true );
      
      // update the scene graph for rendering
      scene.updateGeometricState(0.0f, true);
      scene.updateRenderState();

        
//      //Create our Sphere
//      Sphere s = new Sphere("Sphere", 30, 30, 25);
//      s.setLocalTranslation(new Vector3f(0, 0, -40));
//      s.setModelBound(new BoundingBox());
//      s.updateModelBound();
//   
//      ts = display.getRenderer().createTextureState();
//      ts.setEnabled(true);
//      ts.setTexture(TextureManager.loadTexture(Main.class.getClassLoader()
//               .getResource("jmetest/data/images/Monkey.jpg"),
//               Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR));
//      s.setRenderState(ts);
//   
//      scene.attachChild(s);
   
//      //update the scene graph for rendering
//      scene.updateGeometricState(0.0f, true);
//      scene.updateRenderState();

   }
 
   protected void reinit() {
      display.recreateWindow(width, height, depth, freq, fullscreen);
   }
 
   protected void cleanup() {
      ts.deleteAll();
   }

   public static void main(String[] ars){
      Main app = new Main();
      
      app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG, Main.class.getClassLoader()
                .getResource("jmetest/data/images/FlagRush.png"));
       app.start();
   }
}



I've got a terrain, but i can't move my camera with WASD or mouse...
This is probable i noob question, because i'm just started with jME.
Can sombody help me?

PS: Sorry for my bad English, but my motherlanguage is Dutch and this is the first year we teach English at school...

jmetest.input.action.TestFirstPersonController gives you an example…

I think you're missing the input.update(interpolation); line in your update method…



I could be wrong, since I usually use SimpleGame instead of BaseGame. Good luck.

jiminy said:

jmetest.input.action.TestFirstPersonController gives you an example..
I think you're missing the input.update(interpolation); line in your update method..

I could be wrong, since I usually use SimpleGame instead of BaseGame. Good luck.


Thank you verry much!!!!!
That was wat i need...