cameraNode - CameraToSpatial

I’m experimenting with a cameraNode. I got it to work when I used the mode SpatialToCamera. However, I can’t get round to what I have to use for the mode CameraToSpatial.



c is the cameraNode.

x is a simple node attached to the rootNode.

player[] is an array containing two models.



[java]

c = new CameraNode(“camNode”,cam);

c.setControlDir(ControlDirection.CameraToSpatial);

rootNode.attachChild©;

player[1].setLocalTranslation(0, 0, 0);

player[0].setLocalTranslation(2, 0, 0);

x.attachChild(player[0]);

c.attachChild(player[1]);

c.lookAt(player[1].getLocalTranslation(), Vector3f.UNIT_Y);

c.setLocalTranslation(0, 0, -4);

[/java]



Update:



[java]

c.setLocalTranslation(c.getLocalTranslation().add(-0.1f*tpf, 0, 0)); c.lookAt(player[1].getLocalTranslation(), Vector3f.UNIT_Y); System.out.println(cam.getLocation());

[/java]



Any help on what I should do?

If you enable it you move the cam and the spatial moves along.

No luck despite enabling it.



I have this code now:



[java]c = new CameraNode("camNode",cam);

c.setControlDir(ControlDirection.CameraToSpatial);

rootNode.attachChild©;



player[0].setLocalTranslation(2, 0, 0);

x.attachChild(player[0]);

c.attachChild(player[1]);

player[1].setLocalTranslation(0, 0, -8);

c.lookAt(player[1].getLocalTranslation(), Vector3f.UNIT_Y);

c.setEnabled(true);[/java]



Two notes:


  • player[1] isn’t visible.
  • c doesn’t move when I use this code:



    [java]c.setLocalTranslation(c.getLocalTranslation().add(-0.1f*tpf, 0, 0));[/java]

you never attach the control to the spatial and again you move the cam when in this mode

@normen, what do you mean by “attach the control to the spatial”?



The cam now moves, but the model doesn’t. Here’s what I have:



[java]player[0].setLocalTranslation(2, 0, 0);

x.attachChild(player[0]);

x.attachChild(player[1]);

player[1].setLocalTranslation(0, 0, 0);

c.lookAt(player[1].getLocalTranslation(), Vector3f.UNIT_Y);

//c.setLocalTranslation(0, 0, -4);

c.setEnabled(true);

}[/java]



Update:



[java]@Override

public void simpleUpdate(float tpf){



cam.setLocation(cam.getLocation().add(-0.1f*tpf, 0, 0));

System.out.println(c.getLocalTranslation());[/java]

@memonick said:
[java]@Override
public void simpleUpdate(float tpf){

cam.setLocation(cam.getLocation().add(-0.1f*tpf, 0, 0));
System.out.println(c.getLocalTranslation());[/java]

This can't give you the result you expect, the localTranslation of c will be updated by the control in the updateLogicalState call just after the simpleUpdateCall.

You are not saying what you want to do, but only what you tried
so please be more precise

Sorry, I forgot to mention. I want to have something like an FPS weapon.

And who’s the player holding the weapon?



If it’s player[1] this geom has to be attached to the camNode.

Got it to work. Actually the weapon wasn’t visible because it was being positioned exactly on the camNode.

I am also having a similar issue when attempting to use CameraToSpatial in cojunction with the first-person fly cam. I cannot get the target spatial to move with the fly-cam despite trying several permutations over a couple of hours. I have tried disabling the default flycam and creating my own to use as well as just attempting to keep the default enabled and passing the cam instance (as per current code snippet below).



From the documentation stating usage pattern for flycam:


setControlDir(ControlDirection.CameraToSpatial)

User input steers the camera, and the target spatial follows the camera.
The camera's transformation is copied over the spatial's transformation. Use with first-person flyCam.


I may be missing something completely here but I am not sure exactly what. I am suspecting it is the way I am attaching the nodes to each other but I have tried several variations. My current code is as follows:

[java]
Node ballNode = new Node("Ball");
Geometry ball_geo = new Geometry("BallGeom", sphere);
ball_geo.setMaterial(sponge_mat);

rootNode.attachChild(ballNode);
ballNode.attachChild(ball_geo);

//create the camera Node
CameraNode camNode = new CameraNode("Camera Node", cam);
//This mode means that camera copies the movements of the target:
camNode.setControlDir(ControlDirection.CameraToSpatial);
//Attach the camNode to the target:
ballNode.attachChild(camNode);
//Move camNode, e.g. behind and above the target:
camNode.setLocalTranslation(new Vector3f(0, 5, -5));
//Rotate the camNode to look at the target:
camNode.lookAt(ballNode.getLocalTranslation(), Vector3f.UNIT_Y);

camNode.setEnabled(true);
[/java]

When I move the fly-cam, the ball spatial just sits happily in its starting position. Any ideas?

I think this should be inverted:



From:

[java]ballNode.attachChild(camNode);[/java]



To:

[java]camNode.attachChild(ballNode);[/java]

Thanks memonick, that did the trick. I actually tried the inverse previously and thought it hadn’t worked but it was 2am at the time and I probably wasn’t thinking too straight!

Glad I could help :slight_smile: