Affix camera to geometry location

I’m learning JME and want to toggle between different camera options.

I want to have a camera pointing forwards from a location on a geometry, while following that geometry. Picture a camera looking out from a plane’s cockpit.

After some experimentation, the closest approximation I’ve come up with is using a CameraNode like this:

    fixedCam.setControlDir(CameraControl.ControlDirection.SpatialToCamera);

Then affixing that camera to my geometry’s node and setting a translation vector to my desired position:

        geometryNode.attachChild(fixedCam);
        fixedCam.setLocalTranslation(locationOffsetVector);

This gets the camera in the correct place initially, facing forwards, and following the geometry, but I also want to be able to toggle back and forth between this and the default flyCam. If the orientation of the object changes later, the offset will be wrong. I think what I really want is figure out what the forward vector of the geometry is at the point of toggle, and then add a known offset to that. But how do I find the forward vector?

I also tried to use lookAt with various vectors, but didn’t really get anywhere. The camera would usually end up pointing downwards at an angle rather than forwards. I don’t know why.

So I’m looking for a good way of solving this.

Probably you want to write your own fly cam or adopt an existing implementation that is more flexible. There may be some hacks that can make it work the way you want but ultimately the built in flycam stuff is very limited and almost every real game will replace it at some point.

For example, whether you want to just use the SiO2 library directly or just borrow ideas, here is a more flexible camera control set of classes: SiO2/src/main/java/com/simsilica/input at master · Simsilica/SiO2 · GitHub

Using that you could have a movement target that applies movement to a node that is used for positioning the camera when player controlled… just like when it’s attached to the vehicle.

But there are many ways to solve this problem… but they will almost always involve getting rid of the build in fly cam.

1 Like

I guess that would be best.

I’ve been looking at the implementation of FlyByCamera and ChaseCamera to aid my own Camera implementation. Got it working as my own FlyCam with some custom behaviour, and I can set a spatial and reposition the camera to it at will. Still working on updating it correctly according to target’s movement. This gets a bit “heavier” than I expected (just look at all the interpolation etc. going on in ChaseCamera…)

I see ChaseCamera implements the Control interface and calls

target.addControl(this);

I’m not sure what this achieves, or if I need it in my implementation?

Also, since I’m currently extending SimpleApplication, would I need to do more than call flyCam.setEnabled(false) to completely disable it’s behaviour in my app?

Anyway, my plan for now is to try to clone the target’s movement in my custom camera’s update(tpf) call, mimicking the target movement.

Adding the Control to target causes the Control to be updated after each frame, assuming target is somewhere in the main scene graph.

I’ve not seen your implementation, but unless you use a different mechanism to execute your camera controller code, you likely need addControl().

For more info about how custom scene-graph controls work, see the wiki page.