Attach child to camera node

After reading this:
http://wiki.jmonkeyengine.org/jme3/advanced/making_the_camera_follow_a_character.html#camera-node
and trying it out, it didn’t work. I tried it with Camera to spatial option. I checked the source and it looks like camera control only moves the camera node, not the object it is attached to. So I said no big deal and attached the target to camera node. Now the target is not showing up. Any ideas?

Code:

//Add background
        Quad quad = new Quad(30, 30);
        Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setTexture("ColorMap", app.getAssetManager().loadTexture("Textures/background.png"));
        geo = new Geometry("backdrop", quad);
        geo.setMaterial(mat);
        geo.setLocalTranslation(-15, -15, -7f);
        CameraNode camNode = new CameraNode("Camera node", app.getCamera());
        camNode.setControlDir(CameraControl.ControlDirection.CameraToSpatial);
        camNode.attachChild(geo);
        this.app.getRootNode().attachChild(camNode);

Could you please give us your code, that would be great!

1 Like

But actually you want the camera follow the player (here quad), right?

No. I want the quad to move with the camera.

Unfortunatly I can’t see the problem in your code, yet!
But another option (maybe even easier) would be to create a cam node which has the transformation of the camera and then create another node, call it “quad_node” and attach this one to the camera node. You can of course set your local translation and stuff like that in the quad node.

1 Like

Okay I got something for you here:

  1. Define the camera node globally:

     private Node cameraNode;
    
  2. In an init method call this:

     cameraNode = new Node("CamNode");
     rootNode.attachChild(cameraNode);
     
     Node quadNode = new Node("QuadNode");
     quadNode.setLocalTranslation(-15, -15, -7f);    
     cameraNode.attachChild(quadNode);
    
     // INIT YOUR QUAD HERE... after that add it 
     // btw. you can keep the transformation of the quad (0,0,0)
    
     quadNode.attachChild(quadGeometry);
    
  3. In an update method refresh the camera transformation

     public void update(float tpf) {        
         cameraNode.setLocalTranslation(cam.getLocation());
         cameraNode.setLocalRotation(cam.getRotation());
     }
1 Like

A word of advice: I think you will end up driving yourself crazy by trying to manage your game objects through odd visualizaiton-layer links in this way.

Almost always, it turns out to be way cleaner to have the use input directly control the game object and then let the visualization follow that. Maybe I misread what you are describing, but it sounds like you are doing it the other way around?

Um no, I’m having a chase camera and that quad as a backdrop. So when I rotate the camera the quad should move as well.
I know I could do that using viewports, but I don’t think that’s good performance-wise for a mobile app.

Curious, what do you think the extra processing is for a viewport with no clear flags and only one object in it? (Hint: basically one extra loop iteration)

2 Likes

I don’t know. I have never created new viewports before so I don’t really know what it does. I have just by the way read somewhere on the forum that you can do backgrounds using viewport. I haven’t even looked at the code yet.
Thanks for the info, I’ll try that way :wink: