Cameranode (first person) follow an animate object on a path

Hi everyone.

In my eclipe project I have a cub who browse a path like this code:

http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/animation/TestMotionPath.java

It’s work with a chase camera but i want to change the chase camera by a cameraNode, and it don’t work.
whene the cube browse the path the cameranode dont move.

What should I do?

I haven’t find an equivalent for " chaser.registerWithInput(inputManager); "

thants.

Your code looks fine from here. (since we can’t see it)

package liveview;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.cinematic.MotionPath;
import com.jme3.cinematic.MotionPathListener;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.font.BitmapText;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.scene.shape.Quad;
import com.jme3.system.AppSettings;

public class game extends SimpleApplication  {


	 private Node metroNode;
	 CameraNode camNode,camNode2;
	 ChaseCamera chaser;
     private boolean playing = false;
	 private MotionPath path;
	 
	Spatial metro; 
	 MotionEvent motionControl ;
    public Spatial getMetro() {
		return metro;
	}
	public void setMetro(Spatial metro) {
		this.metro = metro;
	}

	public static void main(String[] args) {



    	gggg app = new game();

    	AppSettings gameSettings = null;
    	gameSettings = new AppSettings(false);
    	gameSettings.setResolution(500, 500);
    	gameSettings.setFullscreen(false);
    	gameSettings.setVSync(false);
    	gameSettings.setTitle("Liveview");
    	gameSettings.setUseInput(true);
    	gameSettings.setFrameRate(500);
    	gameSettings.setSamples(0);
    	gameSettings.setRenderer("LWJGL-OpenGL2");
    	app.settings = gameSettings;
    	app.setDisplayFps(false);
    	app.setDisplayStatView(false);
    	app.setShowSettings(false);
    	
    	
    	app.start();
    	
    }
 
    @Override
    public void simpleInitApp() {

    	 String assetPath = "D:\\Users\\hmiladi\\workspace\\Import3D\\assets";
    	this.assetManager.registerLocator(assetPath, FileLocator.class);
    	

    	metro = assetManager.loadModel("Models\\cube\\test4.j3o");
        metro.scale(1f, 1f, 1f);
        metro.rotate(0.0f, 0.0f, 0.0f);
        metro.setLocalTranslation(0.0f, 0.0f, 0.0f);
        
 
       metroNode = new Node("metronode");
       metroNode.attachChild(metro);
       rootNode.attachChild(metroNode);
        
        

       
       
       Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
       mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
       Geometry ground = new Geometry("ground", new Quad(150, 150));
       ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
       ground.setLocalTranslation(-50, 0, 50);
       ground.setMaterial(mat);
       rootNode.attachChild(ground);

// Light
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1f));
rootNode.addLight(al);

        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-1f, -1f, -1.0f));
        rootNode.addLight(sun);

//disable the default 1st-person flyCam
flyCam.setEnabled(false);

        path = new MotionPath();
        path.addWayPoint(new Vector3f(-50, 0, 0));
        path.addWayPoint(new Vector3f(-35, 0, 0));
        path.addWayPoint(new Vector3f(0, 0, 10));
        path.addWayPoint(new Vector3f(20, 0, -20));
        path.addWayPoint(new Vector3f(30, 0, -35));
        path.addWayPoint(new Vector3f(50, 0, -40));
        path.addWayPoint(new Vector3f(80, 0, -40));
        path.enableDebugShape(assetManager, rootNode);
        path.setCurveTension(0.5f);

// camera chase
/*
chaser = new ChaseCamera(cam, metro);
chaser.setUpVector(new Vector3f(0f, 1f, 0f));
//chaser.setMaxDistance(100);
// chaser.setEnabled(true);
chaser.registerWithInput(inputManager);
*/

	       camNode = new CameraNode("CamNode", cam);
	       camNode.setControlDir(ControlDirection.SpatialToCamera);
	       metroNode.attachChild(camNode);
	       camNode.setLocalTranslation(new Vector3f(0, 5, -10));
	  
	       camNode.lookAt(metroNode.getWorldTranslation(), new Vector3f(0, 1, 0));


        
        motionControl = new MotionEvent(metro,path);
        motionControl.setDirectionType(MotionEvent.Direction.PathAndRotation);
        motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
        motionControl.setInitialDuration(10f);
        motionControl.setSpeed(2f);      

        setupKeys();

    }

    
    private void setupKeys() {
        inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_UP));
        inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addMapping("Camera", new KeyTrigger(KeyInput.KEY_C));
        inputManager.addMapping("play_stop", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("question", new KeyTrigger(KeyInput.KEY_P));
        inputManager.addListener(actionListener, new String[]{"play_stop"});
        inputManager.addListener(analogListener, new String[]{"Ups","Rights","Lefts","Downs","question"});
    }
    
    private ActionListener actionListener = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
        	 if (name.equals("play_stop") && keyPressed) {
                    if (playing) {
                        playing = false;
                        motionControl.stop();
                    } else {
                        playing = true;
                        motionControl.play();
                    }
                }
        }
      };
    
    private AnalogListener analogListener = new AnalogListener() {
    
        public void onAnalog(String name, float value, float tpf) {
            if (name.equals("Ups")) {
            	metroNode.move(-0.05f, 0f, 0f);
            }
            if (name.equals("Downs")) {
            }
            if (name.equals("Rights")) {
            	metroNode.rotate(0, 0.01f, 0);
            }
            if (name.equals("Lefts")) {
            	metroNode.rotate(0, -0.01f, 0);
            }	           
        }
    };

}

in MotionEvent you have to use setDirectionType(MotionEvent.LookAt) and then use the setLookAt method.
Note that this method use the REFERENCE of the vector you pass in so any modification to the vector will make the spatial on the track face it.

hello
I haven’t success to make it work like that but I success by adding a MotionEvent like this code

            motionControlcam = new MotionEvent(camNode,path);
        motionControlcam.setDirectionType(MotionEvent.Direction.PathAndRotation);
        motionControlcam.setInitialDuration(10f);
        motionControlcam.setSpeed(2f);      

thank you :slight_smile: