Isuue camera follow car

ok so im beginning to get the hang of jmonkey, ive been playing around with th TestFancyCar but the example only had a stationary camera, i wanted to try and get a camera to follow and turn as the car does from a third person view like in the old crash team racing game, i can get the camera to follow the car using camNode but the camera doesnt turn with the car no matter what i do?? the code ive added to make the camera is bellow, ive googled everywhere to no avail and gone over the documentation numerous times, is there something im missing??

[java]public void cam()
{

flyCam.setEnabled(false); // Disable the default flyby cam

CameraNode camNode; //create the camera Node
camNode = new CameraNode(“Camera Node”, cam);
camNode.setControlDir(ControlDirection.SpatialToCamera);
carNode.attachChild(camNode);
camNode.setLocalTranslation(new Vector3f(0, 4, 10));
camNode.lookAt(carNode.getLocalTranslation(), Vector3f.UNIT_Y);

}[/java]

What in your code do you think would make it turn? Theres nothing.

Well, he’s attached it into the car node…if the car node turns that should turn the cam node?

Does it behave the same way as this test?

http://jmonkeyengine.googlecode.com/svn/trunk/engine/src/test/jme3test/input/TestCameraNode.java

@zarch said: Well, he's attached it into the car node...if the car node turns that should turn the cam node?

Hm, right but that still leaves us with no code that turns anything at least in this thread :slight_smile: If this is the only code then the Node doesn’t turn, is it maybe a character? Or do you have a funny parent-child relation otherwise? Note the CameraControl applies the world rotation to the local rotation of the CameraNode so you best attach it to the RootNode.

We should actually change or deprecate CameraNode there @nehon.

Aren’t cameras always in world space?

@zarch said: Aren't cameras always in world space?

True, its the camera. So I’m afraid its all your fault @MyFreedomInArt ^^

Does it behave the same way as this test?
http://jmonkeyengine.googlecode.com/svn/trunk/engine/src/test/jme3test/input/TestCameraNode.java

@wezrule
ive checked the testcameranode and the way the camera works there is roughly the same as the way its working now, its just the testfancycar example ive been working on, i havent changed any of the nodes in it yet other than trying to get a camera working, ive gotten the camera to move but when the car turns left or right the camera doesnt turn with it, i need the camera to always stay behind the car, i originally thought the camnode always stayed behind though i guess i am mistaken so that was a misunderstanding of my own fault so i do apologise,

@normen ive changed this

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

to this

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

is this what u meant by attach it to the rootnode, it only makes the camera stationary but it does keep looking at the car, how do i get the camera to stay behind the car and always look in the direction the car is pointing?? this has me sadly stumped :’(

Controls are the thing to use in this case.
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/control/AbstractControl.html

You create a control-class that knows how to position your camera, then you attach the control to the car. jME will call a method called ‘controlUpdate’ and there you change the position (and rotation) of the camera by using the ‘spatial’ the controlled is attached to.

I do something like this, this will position the camera at ‘offset’ from the spatial it is following:

[java]public class FrozenAttachedCamera extends AbstractControl {

private final Vector3f offset;
private final Camera camera;

public FrozenAttachedCamera(
        final Vector3f offset,
        final Camera camera) {
    this.offset = offset;
    this.camera = camera;
}

@Override
protected void controlUpdate(float tpf) {
    final Quaternion worldRotation = this.spatial.getWorldRotation();
    final Vector3f position = this.spatial.getWorldTranslation().add(worldRotation.mult(this.offset));
    this.camera.setLocation(position);

// … add code to rotate the camera
[/java]

thanks for all the help, ive since tried this and it seems to work for what i wanted,

[java]public void cam()
{

flyCam.setEnabled(false);
//create the camera Node
camNode = new CameraNode(“Camera Node”, cam);
//This mode means that camera copies the movements of the target:
camNode.setControlDir(ControlDirection.SpatialToCamera);
//Move camNode, e.g. behind and above the target:
camNode.setLocalTranslation(new Vector3f(0, 4, 15));
//rotates the camera to face car
camNode.rotate(0, 22, 0);
//Attach the camNode to the target:
carNode.attachChild(camNode);

}[/java]

the camera now always faces where the car is facing and follows behind, is this a suitable way of achieving this??