Attaching Camera Correctly

[]Can anyone assist me i've been going through documents all morning and other classes and have been unable to figure out the issue. I'm attempting to make a third person camera for a tps. I can load the character and everything however when i move the mouse it refocuses directly infront of the character.



I create and initialize it in a controller

//creates camera Handler to be controlled by the mouse
      ac= new FirstPersonHandler(DisplaySystem.getDisplaySystem().getRenderer().getCamera(), 15.0f, 0.5f);


in which i initialize inside a init() class in gamestate

//initializes movement
      getRootNode().addController(new PlayerController(rootNode));   


which contains the code I recieved from the wiki

private static CameraNode camNode;
   public void setupCamera() {
      DisplaySystem display = DisplaySystem.getDisplaySystem();
      Camera cam = cam = display.getRenderer().getCamera()//.createCamera(display.getWidth(),
             ; //  display.getHeight());
      cam.setFrustum(1.0f, 1000.0f, -0.55f, 0.55f, 0.4125f, -0.4125f);
      
      //cam.setFrustum(50.0f, 100.0f, -0.5f, 0.2f, 0.2f, -0.1f);
      // cam.setParallelProjection(true);
      cam.update();

      camNode = new CameraNode("Camera Node", cam);
      camNode.setLocalTranslation(new Vector3f(0, 250, -20));
      camNode.updateWorldData(0);

      rootNode.attachChild(camNode);
      //camNode.attachChild(rootNode);
//test to see if its intitalized
      System.out.println("Camera initialized Succefully");
   }   



is this the correct way to use the code also should i be using the firstpersonhandler to get a thirdperson view??

yeah i'm using a camera node and i attached it to the root Node, would it traditionally make more sense to attach rootNode to the camera Node.



If I was to do this would I change KeyNodeBackwardsAction, KeyNodeForwardAction etc to  ThirdPersonBackwardAction, etc. or KeyBackwardAction, etc??



Also i've recently found ThirdPersonHandler would this be better to acheive my goals with if so I would implement this the same way chaseCamera was implemented in flagrush correct?



I had looked at the flagrush tut as well as a couple other examples prior to posting and was still running into troubles the main issue is that the the camera automatically refocuses to a set location when i move my mouse so it appears to jitter. I figured this ir probably because i attached the cameraNode to the rootNode instead of the other way around but i'm still unsure…

I tried adding this to initializeCamera

//creates camera Handler to be controlled by the mouse
        HashMap<String, Object> handlerProps = new HashMap<String, Object>();
        handlerProps.put(ThirdPersonHandler.PROP_DOGRADUAL, "true");
        handlerProps.put(ThirdPersonHandler.PROP_TURNSPEED, ""+(1.0f * FastMath.PI));
        handlerProps.put(ThirdPersonHandler.PROP_LOCKBACKWARDS, "false");
        handlerProps.put(ThirdPersonHandler.PROP_CAMERAALIGNEDMOVE, "true");
        handlerProps.put(ThirdPersonMouseLook.PROP_MAXROLLOUT, "6");
        handlerProps.put(ThirdPersonMouseLook.PROP_MINROLLOUT, "3");
        handlerProps.put(ThirdPersonMouseLook.PROP_MAXASCENT, ""+45 * FastMath.DEG_TO_RAD);
        ac = new ThirdPersonHandler(charNode, cam, handlerProps);


and add the initializeCamera code into playercontroller and initialize it in the main code as well however it doesn't attach the the charNode and used to return a runtime error if i click on either wsad even though i have ikjl mapped  for characternode movement can anyone assist; however, i seemed to of fixed this accidentally. I also edited out the cameraNode part as it seemed to be causing some issues.

P.S. is this even the right direction at least with the FirstPersonHandler i was able to move the view with the mouse, although it would refocus to the middle however with this the view is static

i' not sure if it makes sense to use both together.

if you want third person view, you should use a ChaseCamera (dynamic) or CameranNode (static).

see jmetest.renderer.TestCameraNode  or the jmetest.flagrushtut lessons on how to use them.

i think you try to do to many things at once :slight_smile:



Start small, just use the ThirdPersonHandler with your character and see if it works.

Take a look at jmetest.input.TestThirdPersonController.

Core-Dump said:

i think you try to do to many things at once :)

Start small, just use the ThirdPersonHandler with your character and see if it works.
Take a look at jmetest.input.TestThirdPersonController.


cameraNode was from another unsuccessful trial. My main goal is to set the camera in a third person view behind the character that I can use the mouse to view with like in traditional TPS. I was able to get a version of this compelted with the firstpersonHandler however i'm guessing that wasn't the right approch so i'm trying thirdpersonHandler but its not working out.

if you just want the camera basically mounted behind the player, you could use a camera node, and a node handler for the player.



package input;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.input.InputHandler;
import com.jme.input.NodeHandler;
import com.jme.math.Vector3f;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;

public class TestThirdCamera extends SimpleGame {

   private NodeHandler inputHandler;

   @Override
   protected void simpleInitGame() {
      // remove SimpleGames FirstPerson handler
      input.removeAllFromAttachedHandlers();
      input = new InputHandler();
      
      // create some objects
      createScene();
      
      // create our player
      Box player = new Box("player", new Vector3f(), 0.5f, 0.5f, 0.5f);
      player.setModelBound(new BoundingBox());
      player.updateModelBound();
      // attach the player to the scene
      Node playerNode = new Node("player node");
      playerNode.attachChild(player);
      rootNode.attachChild(playerNode);
      
      // attach the CameraNode to the player node
      CameraNode camNode = new CameraNode("cam node", cam);
      playerNode.attachChild(camNode);
      // and move the camera a bit behind our player
      camNode.setLocalTranslation(0, 1, -3);
      
      // create a inputhandler to move to box around
      inputHandler = new NodeHandler(playerNode, 50, 1);
   }

   @Override
   protected void simpleUpdate() {
      inputHandler.update(tpf);
   }
   
   private void createScene() {
      Node scene = new Node("scene");
      rootNode.attachChild(scene);
      for (int x = 0; x < 5; x++) {
         for (int y = 0; y < 7; y++) {
            for (int z = 0; z < 5; z++) {
               Sphere s = new Sphere("", 7, 7, 0.2f);
               s.setModelBound(new BoundingBox());
               s.updateModelBound();
               s.setLocalTranslation(x*10, y*10, z*10);
               scene.attachChild(s);
            }            
         }
      }
      
      scene.setLocalTranslation(-20, -20, 10);
      
      scene.lock();
   }

   public static void main(String[] args) {
      TestThirdCamera game = new TestThirdCamera();
      game.setConfigShowMode(ConfigShowMode.AlwaysShow);
      game.start();
   }

}



Or do you want to be able to turn the camera independently from the player?
Core-Dump said:

Or do you want to be able to turn the camera independently from the player?


Its suppose to be a TPS so i guess i want it attached to the head...or would it be attached to the body. Should i make a track constraint if i did it the other way.

I think a possible easier way to go is to create a camera node an attach it to the character and then right all the controls for the character. By creating a camera node it won't have collision correct only the character so it can't get stuck on a corner or something...any insight into this

thx for the code btw its extremly helpful and the first working example i have so far  :D
[move]K.I.S.S[move] all the way[/move][/move]
[move][move]Keep It Simple Stupid[/move][/move]

Hello, I have similar question related with that.



Why after attaching Camera to camNode and that to rootNode when I changed Camera location/lookAt vector scene doesn't changes? I found calling updateFromCamera resolves my problem, but why it need to be called? I thought Camera is more important than CameraNode and it produces whole view…



Thanks for clarification,

akakus

after attaching Camera to camNode and that to rootNode

The point of a CameraNode is, that you attach the cameraNode to a moving object, like a player. So the Camera follows the player when the player is moved.

You say you attached the CameraNode to the RootNode (which is usually always located at 0,0,0), that makes no sense to me.
Core-Dump said:

after attaching Camera to camNode and that to rootNode

The point of a CameraNode is, that you attach the cameraNode to a moving object, like a player. So the Camera follows the player when the player is moved.

You say you attached the CameraNode to the RootNode (which is usually always located at 0,0,0), that makes no sense to me.


thx core dump a lot for the example and all but i pose an issue  For some odd reason the camera stays in one place if I attach it to my baseNode(which holds the physics and characterNode mesh etc) it only drops to the ground if i attach it to the physicsNode or the charNode(which holds the char geometry). My character is facing the (positive Z axis i propose this to be the proper front correct) when I attach the camera Node and set the translation the camera is facing the negative X axis at ground level and i'm unable to see him at all unless I was to multiply the translation by 10. I know i can easily fix it with the rotation but is the mesh facing the proper direction or is the camera actually the one facing the proper direction.
Should i calculate the Y translation using his bounding box values?


player.charNode.attachChild(gameSingleton.get().getCameraNode());
gameSingleton.get().getCameraNode().setLocalTranslation(-10, 70, -50);



EDIT

actually this is weird if i set the X translation of the camera to a positive number it brings me increases on positive Z but if i bring it to negative it increases the camera on negative Z