Understanding my camera?!

ok, so I have set up a camera in BaseGame it's called cam;


      /** Set up how our camera sees. */
      cam.setFrustumPerspective(45.0f, (float) display.getWidth()
            / (float) display.getHeight(), 1, 1000);
      Vector3f loc = new Vector3f(0.0f, 0.0f, 25.0f);
      Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
      Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
      Vector3f dir = new Vector3f(0.0f, 0f, -1.0f);
      /** 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();
      /** Assign the camera to this renderer. */
      display.getRenderer().setCamera(cam);



then I have created a ChaseCamGame class called by;

      ChaseCamGame chaser = new ChaseCamGame(playerNode, cam, 0.0f, 3.0f, -10.0f);
      playerNode.attachChild(chaser.getCamNode());



with these bits in it.

   public ChaseCamGame(final Spatial target, Camera cam, final float x, final float y, final float z) {
       this.xDistance = x;
        this.yDistance = y;
        this.zDistance = z;
        this.target = target;
      
      camNode = new CameraNode("Camera Node", cam);



    public final CameraNode getCamNode() {
        return camNode;
    }



Before I added the ChaseCamGame class the camera worked perfectly so I know that I am doing that much right.

But to me the rest doesn't look right, I think that 'chaser' is not actually being linked to cam and camNode correctly (or at least in the way I want), however my inexperience in java is letting me down. Can someone please give me a hint?  :)

Neilos

I dont understand what you want to do with you ChaseCamGame class? And why do you attach the camera node to your playerNode? There is the ChaseCamera class in jme to do what you want I suppose. Also have a look at the jmetest/input/TestThirdPersonController.java example.

I attached the camNode to playerNode so I could have the camNode follow the player (ie the camera is in exactly the same place, with relation to the player, all the time) I thought it would be easier than having it attached to the root node and working out where it should be. Would you not have done it this way?



I dislike the ThirdPersonHandler immensely. I suppose I could look at using the ChaseCamera class but I don't know if it does what I want, I'll have to check it thoroughly. But basically I wanted to make ChaseCamGame from scratch for the sheer education of it and also to be sure to get it to do exactly what I wanted.



I'm just struggling to figure out how it all works, my experience of java is limited (steadily growing).



I eventually want to call an update for chaser that handles vertical rotation, not horizontal, of the camera. If my camNode is stuck to my player then I think I don't need to worry about moving it, I might need to call an update to set it behind the player each frame (I dunno but that will be trivial if I do).



I think that it is fine. I have a static camera atm behind the player I was just wondering if what I was doing looked pointless. I'm not very good at understanding how the constructor works and I was just looking for confirmation that it all looks nice :slight_smile:



Neilos

Ok, so I got the camera behaving but now I tried to set the position and the place the camera is looking, from the update I made in ChaseCamGame;


    public final void update(final float time) {
       this.timer = time;
       camNode.setLocalTranslation(xDistance, yDistance, zDistance);
       camLook = new Vector3f(0, -1, 0);
       camNode.lookAt(camLook, new Vector3f(0, 1, 0));
    }



called in the update in BaseGame using;

      chaser.update(tpf);



I know I don't use the variable timer yet but this shouldn't matter.

But I get a null pointer exception at

      chaser.update(tpf);

this line.

What am I doing wrong? It's bound to be something simple  :lol:

Thanks

Neilos

Yeah, I should have understood in the first place why you attach the CameraNode, sorry, I was thinking about ChaseCamera here, sorry.



A NullPointer is in fact the most simple and easiest to track error. In that line the only object that can be null is chaser. So you did not make an object for your chaser variable before you call the chaser.update() :slight_smile:

:slight_smile:



Then my knowledge is failing me, I though tthat the object was created here;


      ChaseCamGame chaser = new ChaseCamGame(playerNode, cam, 0, 5, -20);



?

Edit: hang on, I think that BEFORE is the operative word here let me check

Grr buildChaseCam() is called in initGame() so surely this is created before update() runs?!?!



I'm lost lol  :lol:



Thanks for your help btw  :wink:

If your line for creating the ChaseCamGame really looks like above you're setting a variable thats local in the method, if you have a global variable in your class, remove the ChaseCamGame before the variable name to set that.

Ahh ok, I get it. I didn't realise there was a difference. Trouble is I am learning java in tandem with jME so all the java is lost on me until I get told it lolz.



Thank you very much for your patience and persistence  :lol:



Btw, it was initially the update thing that prevented the code from working before I made the first post. I commented it all out to test chaser was being created and it all worked at that point, adding update back in killed it again. I would have never realised that the problem was what is was though without your help. Thanks!